Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between fx:id and id: in JavaFX?

Maybe a really newbie's question....

I'm starting learning JavaFX in a FMXL Application using the Scene Builder, by reading this tutorials:

http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm

So once i applied some changes, an issue with this 2 IDs came up... I might have missed or confused something about them...

Can anyone tell me in which cases they are used one or another?

like image 615
Analyst Avatar asked May 15 '14 18:05

Analyst


People also ask

What is FX ID in JavaFX?

The fx:id attribute is a unique identifier of a component, which can be used to reference the component from other parts of our FXML and even our Controller.

What is controller in JavaFX?

JavaFX controller works based on MVC(Model-View-Controller) JavaFX MVC can be achieved by FXML (EFF-ects eXtended Markup Language). FXML is an XML based language used to develop the graphical user interfaces for JavaFX applications as in the HTML.

How do I connect an fxml controller?

The controller is binded in you fxml file or where you call the main view for the first time. So you can use the fx:controller attribute in the xml or the FXMLLoader#setController() method from your launcher.


4 Answers

id you use to set a CSS ID to your Component, for example <Text id="welcome-text" .../> and in your stylesheet you have something like #welcome-text { font-size: 16pt; } so this will be applied to your Text.

fx:id you use if you want to work with your Components in your Controller class, where you annotate them with @FXML Text myWelcomeText.

like image 180
Patrick Avatar answered Oct 19 '22 04:10

Patrick


The fx:id is the identity associated to component in fxml to build a controller, and the id is used for css.

like image 38
Lucas Z. Avatar answered Oct 19 '22 03:10

Lucas Z.


I took a look at an FXML document generated using the JavaFX Scene Builder. You access controls from Java Controller with the fx:id. (edit) I stand corrected, the id does matter.

You can apply css from the FXML document like this:

<Slider id="css_id" fx:id="myslider" styleClass="style_name" .../>

(Replace slider with any control)

And Java controller interaction:

@FXML
Slider myslider;
like image 29
Cobbles Avatar answered Oct 19 '22 05:10

Cobbles


In JavaFX id is used to set a CSS ID to a component. And fx:id is used for accessing that component in code (i.e. in a controller class). fx:id works like a components name.

like image 43
Amita Patil Avatar answered Oct 19 '22 05:10

Amita Patil