Asked  7 Months ago    Answers:  5   Viewed   46 times

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?

 Answers

57

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.

Tuesday, June 1, 2021
 
clean_coding
answered 7 Months ago
98

you refer to Android resources , which are already defined in Android system, with @android:id/.. while to access resources that you have defined/created in your project, you use @id/..

More Info

As per your clarifications in the chat, you said you have a problem like this :

If we use android:id="@id/layout_item_id" it doesn't work. Instead @+id/ works so what's the difference here? And that was my original question.

Well, it depends on the context, when you're using the XML attribute of android:id, then you're specifying a new id, and are instructing the parser (or call it the builder) to create a new entry in R.java, thus you have to include a + sign.

While in the other case, like android:layout_below="@id/myTextView" , you're referring to an id that has already been created, so parser links this to the already created id in R.java.

More Info Again

As you said in your chat, note that android:layout_below="@id/myTextView" won't recognize an element with id myTextViewif it is written after the element you're using it in.

Tuesday, June 1, 2021
 
Crontab
answered 7 Months ago
34

I did some mock testing to record the difference between Save() and Persist().

Sounds like both these methods behaves same when dealing with Transient Entity but differ when dealing with Detached Entity.

For the below example , take EmployeeVehicle as an Entity with PK as vehicleId which is a generated value and vehicleName as one of its property .

Example 1 : Dealing with Transient Object

                 Session session = factory.openSession();
                 session.beginTransaction();
                 EmployeeVehicle entity = new EmployeeVehicle();
                    entity.setVehicleName("Honda");
                 session.save(entity);
                 // session.persist(entity);
                session.getTransaction().commit();
                session.close();

Result : select nextval ('hibernate_sequence') // This is for vehicle Id generated : 36

insert into Employee_Vehicle ( Vehicle_Name, Vehicle_Id) values ( Honda, 36)

Repeat the same with using persist(entity) and will result the same with new Id ( say 37 , honda ) ;

Example 2 : Dealing with Detached Object

// Session 1 
            // Get the previously saved Vehicle Entity 
           Session session = factory.openSession();
            session.beginTransaction();
            EmployeeVehicle entity = (EmployeeVehicle)session.get(EmployeeVehicle.class, 36);
           session.close();

           // Session 2
           // Here in Session 2 , vehicle entity obtained in previous session is a detached object and now we will try to save / persist it 
         (i) Using Save() to persist a detached object 
           Session session2 = factory.openSession();
            session2.beginTransaction();
                    entity.setVehicleName("Toyota");
            session2.save(entity);
            session2.getTransaction().commit();
            session2.close();

Result : You might be expecting the Vehicle with id : 36 obtained in previous session is updated with name as "Toyota" . But what happens is that a new entity is saved in the DB with new Id generated for and Name as "Toyota"

         select nextval ('hibernate_sequence')
         insert into Employee_Vehicle ( Vehicle_Name, Vehicle_Id) values ( Toyota, 39)

         (ii) Using Persist()  to persist a detached object 

            // Session 1 
            Session session = factory.openSession();
    session.beginTransaction();
    EmployeeVehicle entity = EmployeeVehicle)session.get(EmployeeVehicle.class, 36);
    session.close();

// Session 2 // Here in Session 2 , vehicle entity obtained in previous session is a detached object and now we will try to save / persist it (i) Using persist() to persist a detached object

            Session session2 = factory.openSession();
    session2.beginTransaction();
            entity.setVehicleName("Toyota");
    session2.persist(entity);
    session2.getTransaction().commit();
    session2.close();

Result : Exception being thrown : detached entity passed to persist

So, it is always better to use Persist() rather than Save() as save has to be carefully used when dealing with session and transcation .

Tuesday, July 20, 2021
 
Fredy
answered 5 Months ago
67

<fx:root> specifies a "dynamic root" for your FXML file; this means the root of the FXML file is an object that is set on the loader prior to loading the file. This is typically used for custom controls, where you want the control to be a subclass of Node that can be instantiated using regular Java code, but want to define its layout using FXML. Proper use of <fx:root> (or at least an example of how it can be used) is shown in the standard documentation. In particular, if you use <fx:root> you must:

  1. Create an FXMLLoader instance, instead of using the static convenience FXMLLoader.load(URL) method
  2. Call setRoot(...) on that instance, and pass in the object that is to be the root of the FXML.

For standard FXML use, you just use a regular instance declaration as the root. Almost every example available works this way: probably the best place to start is the official tutorial. In your case, since you want a VBox, you probably just need

<VBox xmlns="javafx.com/javafx/8"; xmlns:fx="javafx.com/fxml/1"; fx:controller="divergex.DivergexGUIController">
<!-- ... -->
</VBox>

Edit If Netbeans is giving you issues, I recommend using Eclipse with the e(fx)clipse plugin. There's a very barebones, but pretty much all you need, tutorial.

Saturday, August 14, 2021
 
Aniket Thakur
answered 4 Months ago
78

After some Trial and Error I found at least a way to do it directly via the .fxml-file:

You can assign the Button elements with ButtonBar.buttonData attributes and then assign a value to them.

<ButtonBar>
  <buttons>
    <Button text="New" ButtonBar.buttonData="LEFT" />
    <Button text="Save" ButtonBar.buttonData="RIGHT" />
  </buttons>
</ButtonBar>

The docs for the ButtonBar.ButtonData enum are pretty straighforward. So I found the solution to my problem with the "LEFT" and "RIGHT" enum values.

STILL

If anyone knows/finds out how to do this directly in SceneBuilder, I would be grateful!

Tuesday, September 28, 2021
 
njeoo
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share