Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1.  
  2. package corseworkerino;
  3.  
  4.  
  5. import java.io.IOException;
  6. import java.util.function.Consumer;
  7.  
  8. import javafx.application.Application;
  9. import javafx.beans.value.ChangeListener;
  10. import javafx.beans.value.ObservableValue;
  11. import javafx.event.ActionEvent;
  12. import javafx.event.EventHandler;
  13. import javafx.fxml.FXML;
  14. import javafx.fxml.FXMLLoader;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.Label;
  18. import javafx.scene.control.RadioButton;
  19. import javafx.scene.control.TextField;
  20. import javafx.scene.control.Toggle;
  21. import javafx.scene.control.ToggleGroup;
  22. import javafx.scene.layout.GridPane;
  23. import javafx.stage.Stage;
  24.  
  25. public class CalcView extends Application implements ViewInterface {
  26.  
  27.       @FXML
  28.       // fx:id="calcButton"
  29.       private Button calcButton; // Value injected by FXMLLoader
  30.  
  31.       @FXML
  32.       // fx:id="infixButton"
  33.       private RadioButton infixButton; // Value injected by FXMLLoader
  34.  
  35.       @FXML
  36.       // fx:id="type"
  37.       private ToggleGroup type; // Value injected by FXMLLoader
  38.  
  39.       @FXML
  40.       // fx:id="postfixButton"
  41.       private RadioButton postfixButton; // Value injected by FXMLLoader
  42.  
  43.       @FXML
  44.       // fx:id="question"
  45.       private TextField question; // Value injected by FXMLLoader
  46.  
  47.       @FXML
  48.       // fx:id="answer"
  49.       private Label answer; // Value injected by FXMLLoader
  50.       /////////////////////////////////////////////////////////////////////////////////
  51.       // Block for creating an instance variable for others to use.
  52.       //
  53.       // Make it a JavaFX singleton.  Instance is set by the javafx "initialize" method
  54.       private volatile static CalcView instance = null;
  55.  
  56.       @FXML
  57.       void initialize() {
  58.         instance = this;
  59.       }
  60.  
  61.       public synchronized static CalcView getInstance() {
  62.         if (instance == null) {
  63.           new Thread(() -> Application.launch(CalcView.class)).start();
  64.           // Wait until the instance is ready since initialize has executed.
  65.           while (instance == null) {// empty body
  66.           }
  67.         }
  68.  
  69.         return instance;
  70.       }
  71.       // End of special block
  72.       /////////////////////////////////////////////////////////////////////////////////
  73.      
  74.       @Override
  75.       public void start(Stage primaryStage) throws IOException {
  76.         GridPane page = (GridPane) FXMLLoader.load(CalcView.class
  77.             .getResource("CalcView.fxml"));
  78.         Scene scene = new Scene(page);
  79.         scene.getStylesheets().add(
  80.             getClass().getResource("application.css").toExternalForm());
  81.         primaryStage.setScene(scene);
  82.         primaryStage.setTitle("MVC with JavaFX and FXML");
  83.         primaryStage.show();
  84.       }
  85.      
  86.      
  87.  
  88.  
  89.     @Override
  90.     public void addCalcObserver(Observer f) {
  91.         calcButton.setOnAction(event -> f.tell());
  92.        
  93.     }
  94.  
  95.     @Override
  96.     public void addTypeObserver(Observer f) {
  97.         infixButton.setOnAction(event -> f.tell());
  98.         postfixButton.setOnAction(event -> f.tell());
  99.  
  100.     }
  101.     @Override
  102.     public OpType getType() {
  103.         if (postfixButton.isSelected()) {
  104.             return OpType.POSTFIX;
  105.            
  106.         }else {
  107.             return OpType.INFIX;
  108.         }
  109.     }
  110.  
  111.     @Override
  112.     public String getQuestion() {
  113.         return question.getText();
  114.     }
  115.  
  116.     @Override
  117.     public void setAnswer(String a) {
  118.    
  119.                 answer.setText(a);
  120.  
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement