Partha Sarathi
Partha Sarathi

Reputation: 15

how to disable all the button using an on_off button in javafx?

enter image description here

it is a simple calculator made by using javaFx. My problem is that i want to use the on_off button to get power and to get inactive the calculator. How to do so??? Thanks in advance.

Upvotes: 1

Views: 4770

Answers (2)

fabian
fabian

Reputation: 82451

You cannot do this using SceneBuilder alone, however it could be done by editing the fxml yourself. Just use a ToggleButton for the on/off button and bind the disable properties to the selected property of the ToggleButton or do this in the initialize method of the controller (requires all Buttons to be injected to the controller via fx:id).

binding in fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>

<VBox xmlns:fx="http://javafx.com/fxml/1" spacing="10">
    <padding>
        <Insets left="10" right="10" bottom="10" top="10" />
    </padding>
    <children>
        <fx:define>
            <!-- create ToggleButton to be used with the disable properties -->
            <ToggleButton fx:id="on" text="On"/>
        </fx:define>
        <!-- create buttons and bind the disable property to the negated 
             selected property of the on button -->
        <Button text="Button 1" disable="${!on.selected}" />
        <Button text="Button 2" disable="${!on.selected}" />
        <Button text="Button 3" disable="${!on.selected}" />
        <Button text="Button 4" disable="${!on.selected}" />
        <Button text="Button 5" disable="${!on.selected}" />
        <!-- add on button to scene -->
        <fx:reference source="on"/>
    </children>
</VBox>

binding in controller

@FXML
private Button button1;

@FXML
private Button button2;

@FXML
private Button button3;
...

@FXML
private ToggleButton on;

@FXML
private void initialize() {
    BooleanBinding disable = on.selectedProperty().not();
    button1.disableProperty().bind(disable);
    button2.disableProperty().bind(disable);
    button3.disableProperty().bind(disable);
    ...
}

Upvotes: 2

Maglioni Lorenzo
Maglioni Lorenzo

Reputation: 378

to make this quickly i advice you to add all buttons, except for the on/off one to a container like an hbox v box or what you want, then to disable the container(parent) which contains your buttons e.g.

vBoxMain.getChildren().addAll(/*every button except on/off*/);
//or generate dynamically the buttons and add them to the vBoxMain in a for cycle
buttonOnOff.setOnAction((ActionEvent e) -> {

    if(vBoxMain.isDidable()){
        vBoxMain.setDisable(false);
    }else{
        vBoxMain.setDisable(true);
    }

});

This is not for your specific layout but to give you an idea, i hope it would be helpful to you.

Upvotes: 0

Related Questions