JavaFX's Fxcontroller detailed

Source: Internet
Author: User

In JavaFX's UI development, Fxcontroller is a very important thing, mainly for the UI layer and event layer separation.

In fact, JavaFX uses Fxml to develop the UI interface, there are many forms to listen to our events, let's take a closer look.

1. Handling events via Controller class

First we create a simple interface that contains a button and a label.

Such as:

The label's fx:id is set to Mlabel,button Fx:id is set to Mbutton, and the button's onaction is set to OnButtonClick.

As shown in the following:


Then we create a Maincontroller class and write the following code:

Import Javafx.fxml.fxml;import Javafx.scene.control.button;import Javafx.event.actionevent;import Javafx.scene.control.label;public class Mainlayoutcontroller {@FXMLprivate Button Mbutton; @FXMLprivate Label mlabel;@ fxmlpublic void OnButtonClick (ActionEvent event) {mlabel.settext ("HelloWorld");}}

Remember, we need to add Fx:controller = "" To Your Maincontroller class (with package name) at the top of Fxml.

Our main class is as follows:

Import Javafx.application.application;import Javafx.fxml.fxmlloader;import Javafx.stage.stage;import Javafx.scene.parent;import Javafx.scene.scene;public class Main extends application {@Overridepublic void start (Stage Primarystage) {try {Parent parent = Fxmlloader.load (GetClass (). GetResource ("Mainlayout.fxml")); Scene scene = new Scene (parent,300,200); Scene.getstylesheets (). Add (GetClass (). GetResource ("Application.css"). Toexternalform ());p rimarystage.setscene (Scene);p rimarystage.show (); catch (Exception e) {e.printstacktrace ();}} public static void Main (string[] args) {launch (args);}}

Load the Fxml through Fxmlloader and add it to the scene.

The results are as follows:


When we click on the button, the text content becomes HelloWorld.

This is the way I used to talk about events in my previous article.


2. Handling events like Android

Next, let's take a look at another way of handling events.

In fact, JavaFX provides some Android-like methods that we can use Fx:id to find the specified control and implement our events through code.

Let's change the main method above as follows:

Import Javafx.application.application;import Javafx.fxml.fxmlloader;import Javafx.stage.stage;import Javafx.scene.parent;import Javafx.scene.scene;import Javafx.scene.control.button;import Javafx.scene.control.label;public class Main extends application {@Overridepublic void start (Stage primarystage) {try { Parent parent = Fxmlloader.load (GetClass (). GetResource ("Mainlayout.fxml")); Label label = (label) parent.lookup ("#mLabel"); Button button = (Button) parent.lookup ("#mButton"); Button.setonaction (E->{label.settext ("HelloWorld JavaFX");}); Scene scene = new Scene (parent,300,200); Scene.getstylesheets (). Add (GetClass (). GetResource ("Application.css"). Toexternalform ());p rimarystage.setscene (Scene);p rimarystage.show (); catch (Exception e) {e.printstacktrace ();}} public static void Main (string[] args) {launch (args);}}

The results are as follows:


As you can see clearly, after we find the control through lookup, the added event overrides the event in Fxcontroller.

This is another Android-like lookup control-the mode of adding events that can be tailored to your needs.

This article is personal original, all rights reserved, reprint please indicate source:http://blog.csdn.net/ml3947. In addition to my personal blog:http://www.wjfxgame.com.  


JavaFX's Fxcontroller detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.