rugby82
rugby82

Reputation: 699

JavaFX: FXML script to disable button if another object is active

i am using JavaFX with jdk 1.8.

It is possible to disable a button when another button is disabled, using a script on the FXML file?

Upvotes: 1

Views: 1134

Answers (1)

fabian
fabian

Reputation: 82451

You can use expression binding in the fxml.

Assuming you want to bind the Button.disable property of one button to the Button.disabled property of another button here:

<CheckBox fx:id="cb" text="button 1 disabled"/>
<Button fx:id="b1" text="button 1" disable="${cb.selected}"/> <!-- disable button iff the checkbox is checked -->
<Button text="button 2" disable="${b1.disabled}"/> <!-- disable this button iff b1 is disabled -->

If the b1 needs to be added to the scene at a later point, you need to use <fx:define> before b2 to create the button and use <fx:reference> to add it to the scene at a later point.

<fx:define>
     <Button fx:id="b1" text="button 1" disable="${cb.selected}"/>
</fx:define>
<Button text="button 2" disable="${b1.disabled}"/>
<fx:reference source="b1" />

Upvotes: 5

Related Questions