Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVAFX event triggered when selecting a check box

Tags:

javafx

My JavaFx FXML application has an issue.

When I select a checkbox on a form, I want to execute a method based on the checkbox that was clicked. Is there any way that I can pass the name of the checkbox through to the method so I can perform some conditional work on it?

I have two checkboxes and only one can be selected. When I click on one, the other should be de-selected and vice versa. Obviously the code below will not work so I am looking to pass the name of the object that was clicked.

Any help would be appreciated, many thanks.

@FXML private void updateRcs(){

    if (chkRcsuri.isSelected()){
        chkRcsuri2.setSelected(false);
    }

    if (chkRcsuri2.isSelected()){
        chkRcsuri.setSelected(false);
    }

}
like image 700
user1673554 Avatar asked Dec 05 '12 15:12

user1673554


People also ask

How do you test if a check box is selected JavaFX?

you could use . isSelected() to find out if the checkbox is Ticked. Save this answer.

How do checkboxes work in JavaFX?

When checked the CheckBox is typically rendered with a "check" or "tick" mark. A CheckBox is in this state if selected is true and indeterminate is false. A CheckBox is unchecked if selected is false and indeterminate is false. A CheckBox is undefined if indeterminate is true, regardless of the state of selected.

How do I add a click event in JavaFX?

In JavaFX, a button event should be defined either by invoking the setOnAction() convenience method of a Button object, or by defining the onAction attribute of the Button object in an FXML document and linking it to a method that accepts an ActionEvent object as a parameter.

How do you create a check box in JavaFX?

Creating a CheckBox You create a JavaFX CheckBox control via the CheckBox constructor. Here is a JavaFX CheckBox instantiation example: CheckBox checkBox1 = new CheckBox("Green"); The String passed to the CheckBox constructor is displayed next to the CheckBox control.


1 Answers

You can use change tracking or use Event handling mechanism of JavaFX.
With checkboxes like this,

final CheckBox chk1 = new CheckBox("chk 1");
final CheckBox chk2 = new CheckBox("chk 2");

Change tracking

chk1.selectedProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        chk2.setSelected(!newValue);
    }
});

chk2.selectedProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        chk1.setSelected(!newValue);
    }
});

Using event handling

EventHandler eh = new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        if (event.getSource() instanceof CheckBox) {
            CheckBox chk = (CheckBox) event.getSource();
            System.out.println("Action performed on checkbox " + chk.getText());
            if ("chk 1".equals(chk.getText())) {
                chk2.setSelected(!chk1.isSelected());
            } else if ("chk 2".equals(chk.getText())) {
                chk1.setSelected(!chk2.isSelected());
            }
        }
    }
};

chk1.setOnAction(eh);
chk2.setOnAction(eh);
like image 68
Uluk Biy Avatar answered Oct 13 '22 14:10

Uluk Biy