Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java fx combobox reset issue

When I try to clear a combobox using following code:

code :

public class ComboController implements Initializable
{

@FXML
ComboBox firstcombobox=new ComboBox();
@FXML
ComboBox secondcombobox=new ComboBox();
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    // TODO Auto-generated method stub
    firstcombobox.getItems().add("firabc");
    firstcombobox.getItems().add("firbcd");

    secondcombobox.getItems().add("seccde");
    secondcombobox.getItems().add("secdef");
}
@FXML
public void firstcomboboxAction()
{
    secondcombobox.getSelectionModel().clearSelection();
    secondcombobox.getItems().clear();
    System.out.println(firstcombobox.getSelectionModel().getSelectedItem());
}

public void secondcomboboxAction()
{
    System.out.println(secondcombobox.getSelectionModel().getSelectedItem());
    System.out.println("my name is vinay");
}

}

It automatically calls action event

first i selected "seccde" from second combobox then i selected "firabc" from first combobox

output i received is:

seccde
my name is vinay
null
my name is vinay
firabc

but it should have been :

seccde
my name is vinay

firabc  [with an empty firstcombobox]

Fxml :

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.ComboController">
<ComboBox fx:id="firstcombobox" onAction="#firstcomboboxAction"  >
<ComboBox fx:id="secondcombobox" onAction="#secondcomboboxAction" >
like image 547
vinay Avatar asked May 19 '26 07:05

vinay


1 Answers

The output is exactly what you should expect.

First you select something in the second combo box. This invokes the handler for the second combo box, giving the output

seccde
my name is vinay

Then you select an item in the first combo box. This invokes the handler for the first combo box. The handler for that combo box first clears all items from the second combo box, which as a side effect sets the value property of the second combo box to null. This causes the action handler for the second combo box to be invoked, generating the output

null
my name is vinay

The handler for the second combo box then generates the output

firabc

and of course at this point the second combo box will have no items in it.

This is all in accordance with the Javadocs for the ComboBox. In particular, the docs for the onAction handler describe the handler as follows:

The ComboBox action, which is invoked whenever the ComboBox value property is changed. This may be due to the value property being programmatically changed, when the user selects an item in a popup list or dialog, or, in the case of editable ComboBoxes, it may be when the user provides their own input (be that via a TextField or some other input mechanism.

like image 102
James_D Avatar answered May 20 '26 21:05

James_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!