Home java How JavaFx Controller

How JavaFx Controller

Author

Date

Category

Tell me where to read, or better explain what the magic of the controller is. I first thought that JavaFX friendly Wednesday to create a GUI, sort of like in SceneBuilder, made an interface and assigned the FX: ID nodes, then created the controller class, this interface was loaded to it and write the logic, referring to the interface elements by FX: ID. But it turns out that everything is not so rosy. For a week, I’m looking for and finding only fragmentary rumors, I do not add up in the overall picture. Namely, that the controller should be in itself, without logic in it, it seems to be the FXML load, it is determined by an initialize method, everything else should be in a separate class or classes. OK, it would be even more convenient, but I do not understand how this initialize method is filling than? How to use the window that the controller creates and shove logic to it?

Here I have a task:
There is an interface made on FXML. It defines anchorpane, vbox, button and other components. All of them have their own FX: ID.
And I have a separate class, which is called a separate window by pressing the button from the main interface, there without FXML (on Java) a small window into which the text is simply entered.
I want to press the button, which via FXML is defined to call this window written in a separate class, enter text in it and by pressing in the same input window called the button defined in it so that the text is embedded in the VBOX by a separate panel (new Node Vbox. getchildren (). Add ()).

I saw it this:
In the controller, I appeal to the button on the FX: ID and using the SETONMouseClick method, I call the second modal window, I enter the text in it, click on it and it sends text to VBOX on its FX: ID (FX: ID = “NOTELIST” .Getchildren () .add (text entered in a modal window). But I grab nullpoInterException still on the compilation, as I understood because it is impossible to refer to the FX: ID in the controller, because it is inherited from Application, and for some kind of magic in It does not initialize the fields to which I have FX: ID is assigned.
I most likely not clear the question described, but I want to understand how I interact from the code with the elements of the GUI on FX: ID, how can I describe the controller, how can I contact and do I need to contact him, turning out of the code to the GUI FXML?
For example, I have FXML

& lt; vbox xmlns = "http://javafx.com/javafx" xmlns: fx = "http: / /javafx.com/fxml "
   prefheight = "200" prefwidth = "300" alignment = "center" FX: Controller = "App" & gt;
 & lt; vbox fx: id = "notelist" & gt;
 & lt; / vbox & gt;
  & lt; Button FX: ID = "MainButton" text = "greeting" / & gt;
& lt; / vbox & gt;

I do not point in it to an appeal to any method in Java code.
I just turned to the FX: ID:

Public Class App Extends Application IMPLEMENTS INITIALIZABLE {
  @Fxml
  VBOX NOTELIS; // Container for storing my new NODE (notes in my case)
  Stage Notestage; // Stage modal window to close it in the right place
  // (SETNOTE Returns your Stage)
  SETNOTE SETNOTE = NEW SETNOTE (); // class, which describes the little Textarea,
  // in which I enter the text and in it the button,
  // Using FX: ID = "NOTELIST" to add getchildren (). Add (this.textarea)
  @Fxml
  Button Addbutton;
  // --------------------------
    Public Void Start (Stage PrimaryStage) Thrown Exception {
    Parent root = fxmlloader.load (getClass (). GetResource ("app.fxml"));
    // --------------------------
    Textarea Note = New Textarea ("Pref Text \ N Halo \ N PP");
    Button DoneBTN = New Button ();
    AddButton.SetonAction (ACT - & GT; {
      Try {
        notestage = setNote.ShowWindow (Note, PrimaryStage, DoneBTN);
      } Catch (FileNotFoundException E) {
        E.PrintStackTrace ();
      }
    });
    // Add Note On Main Window (Note List)
      DoneBTN.SetonAction (E - & GT; {
      notestage.close ();
      Anchorpane Anchorpane = New Anchorpane ();
      Label titlelabel = new label (titletext);
      Label PreviewLabel = New Label (PreviewText); 
note.settext ("");
      titlelabel.setstyle ("- FX-Font-Size: 15; -fx-font-weight: bold");
      // Set Positions of Text and Buttons in Note
      Anchorpane.Setbottomanchor (PreviewLabel, 1.0);
      Anchorpane.Setleftanchor (PreviewLabel, 5.0);
      AnchorPane.Setleftanchor (Titlelabel, 5.0);
      AnchorPane.Settopanchor (Titlelabel, 0.0);
      Button Button = New Button ("Remove");
      Button.SetonAction (EVT - & gt; content.getchildren (). Remove (Anchorpane));
      Anchorpane.Setrightanchor (Button, 5.0);
      Anchorpane.Settopanchor (Button, 5.0);
      Anchorpane.SetBotTomanchor (Button, 5.0);
      Anchorpane.getchildren (). AddALL (Titlelabel, Button, PreviewLabel);
      // Add a Note in Notes List in 0 index
      content.Getchildren (). Add (0, AnchorPane);
    }
  });
   @Override
   Public Void Initialize (URL Location, Resourcebundle Resources) {
   }
   Public Static Void Main (String [] Args) {
    Launch (Args);
   }
  }

How to figure it out with the controller? At pure Java code without FXML, I would have just wrote entirely the application entirely, without any problems. But they say, it is necessary to use it to split the logic and the GUI. And how to fasten to the FXML code, so that they can use, no one says, there is not a single manual, in which this one, the most important point will be affected. But all manuals say how FX: Id use in the methods, referring to them through FXML. But it does not fit …


Answer 1

was difficult, but I am …
In addition to jokes. I found the answer. Nowhere writes about it, but the MVC model is assumed when using JavaFX. If briefly:
The controller should not ship the FXML file. Maybe but not worth it.
FXML must be shipped by a class that meets only for the launch of the application. And this file is only responsible for the external skeleton of the application.
Logic lives in two classes, a class with data and their processing, this is a class class.
The display class that takes data from the model, through the controller gets access to FXML FX: ID and displays data. The controller is engaged in initialization, it must IMPLEMENTS INITIALIZABLE and in the implementable method, the links from FXML acquire the flesh. But the performed method must have arguments! Otherwise there will be nullpoInterException.
Everything named above is not necessary. You can do everything in one class. You can even make an application without FXML. BUT! About his subsequent expansion, with this approach, you can forget. It all depends on the needs. And yes, to see a class, seeing the display, see your FX: ID, you need (further clean magic that does not give you way to logic) – inherit the controller from class view. Then view will see your FX: ID.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions