Advertisement
Guest User

MVP Javafx

a guest
Jul 17th, 2014
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. //MAIN PRESENTER CLASS//
  2.  
  3. public class MainPresenter implements Observer{
  4.  
  5.     FxmlController mainView;
  6.  
  7.     private SettingsPresenter settingsPresenter;
  8.  
  9.     public MainPresenter(){
  10.  
  11.         try
  12.         {
  13.             FXMLLoader loader = new FXMLLoader();
  14.             loader.load(getClass().getResourceAsStream("main.fxml"));
  15.             mainView = loader.getController();
  16.         }
  17.         catch (IOException e)
  18.         {
  19.             throw new RuntimeException("Unable to load Main.fxml", e);
  20.         }
  21.  
  22.         mainView.addObserver(this);
  23.     }
  24.  
  25.     public Parent getView(){
  26.         return mainView.getView();
  27.     }
  28.  
  29.     public void showSettings(){
  30.         System.out.println("PROVA");
  31.     }
  32.  
  33.     @Override
  34.     public void update(Observable o, Object arg) {
  35.  
  36.         String action = arg.toString();
  37.  
  38.         switch (action){
  39.             case "show_settings_button":
  40.                 showSettings();
  41.                 break;
  42.         }
  43.     }
  44. }
  45.  
  46. //FXML CONTROLLER//
  47.  
  48. public class FxmlController extends Observable{
  49.    
  50.     @FXML private Parent root;
  51.  
  52.     public Parent getView(){
  53.         return root;
  54.     }
  55.  
  56.     @FXML private void handleShowSettingsButton(){
  57.  
  58.         setChanged();
  59.         notifyObservers("show_settings_button");
  60.  
  61.     }
  62.  
  63.  
  64. }
  65.  
  66. //main.fxml//
  67.  
  68. <?xml version="1.0" encoding="UTF-8"?>
  69.  
  70. <?import java.lang.*?>
  71. <?import javafx.geometry.*?>
  72. <?import javafx.geometry.Insets?>
  73. <?import javafx.scene.control.*?>
  74. <?import javafx.scene.layout.*?>
  75.  
  76. <BorderPane fx:id="root" minWidth="-1.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="cotl.MenuWnd.Main.FxmlController">
  77.   <center>
  78.     <Button mnemonicParsing="false" onMouseClicked="#handleShowSettingsButton" text="Button">
  79.       <BorderPane.margin>
  80.         <Insets bottom="100.0" left="100.0" right="100.0" top="100.0" />
  81.       </BorderPane.margin>
  82.     </Button>
  83.   </center>
  84. </BorderPane>
  85.  
  86. //APP CLASS//
  87.  
  88. public class App extends Application {
  89.  
  90.     @Override
  91.     public void start(Stage stage) throws Exception{
  92.  
  93.         MainPresenter presenter = new MainPresenter();
  94.         Scene scene = new Scene(presenter.getView(), 800, 600);
  95.         stage.setScene(scene);
  96.         stage.setTitle("PROVA");
  97.         stage.show();
  98.     }
  99.  
  100.     public static void main(String[] args) {
  101.         launch(args);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement