Java fx combobox reset issue


Question

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" >
1
3
3/6/2014 9:02:35 AM

Accepted Answer

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.

1
3/6/2014 1:34:16 PM

I solved the problem using changeListener.

public class ComboController implements Initializable
{
    @FXML
    ComboBox firstcombobox=new ComboBox();
    @FXML
    ComboBox secondcombobox=new ComboBox();
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) 
    {
        // adding elements to comboboxes
        firstcombobox.getItems().add("firabc");
        firstcombobox.getItems().add("firbcd");

        secondcombobox.getItems().add("seccde");
        secondcombobox.getItems().add("secdef");

        // adding action to first combobox
        firstcombobox.getSelectionModel().selectedItemProperty()
                .addListener(new firstcomboboxAction());

        // adding action to second combobox
        secondcombobox.getSelectionModel().selectedItemProperty()
                .addListener(new secondcomboboxAction());

    }

class firstcomboboxAction implements ChangeListener<String>
{
    @Override
    public void changed(ObservableValue<? extends String> observable,String oldValue, String newValue) {

         if(newValue!=null)
         {
                  secondcombobox.getSelectionModel().clearSelection();
                  secondcombobox.getItems().clear();
                  System.out.println(firstcombobox.getSelectionModel().getSelectedItem());
         }
}


class secondcomboboxAction implements ChangeListener<String>
{
    @Override
    public void changed(ObservableValue<? extends String> observable,String oldValue, String newValue) 
    {
        System.out.println(secondcombobox.getSelectionModel().getSelectedItem());
        System.out.println("my name is vinay");
    }
}
}

FXML:

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"
fx:controller="application.ComboController">
<ComboBox fx:id="firstcombobox"  >
<ComboBox fx:id="secondcombobox"  >

Output:

seccde
my name is vinay

firabc  [with an empty firstcombobox]

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon