Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox clearing value issue

Tags:

I've stumbled on an issue with Comboboxes in javafx2.2. This is the scenario:

  • Users click on the 'editFile' button.
  • Another pane becomes visible (with the setVisible method).

This pane contains 6 comboboxes. Three of them have fixed items: cboReport, cboSales, cboSend. Three of them get their data from a db (ObservableList) and get populated when the pane becomes visible: cboFile, cboCustomer, cboVet

  • The user selects a file number from the cboFile. The rest of the comboboxes are beeing set with the correct values.
  • The user presses the save button, the file gets saved as intended.
  • Next the user presses a close button.

When the window closes, the data on the pane gets resetted through a resetGUI_editFilePane() method. There is have lines like:

... cboReport.getSelectionModel().clearSelection(); cboSales.getSelectionModel().clearSelection(); cboSend.getSelectionModel().clearSelection(); cboFile.getSelectionModel().clearSelection(); cboCustomer.getSelectionModel().clearSelection(); cboVet.getSelectionModel().clearSelection();  cboFile.getItems().clear(); cboCustomer.getItems().clear(); cboVet.getItems.clear(); ... 

When the user opens the pane again by pressing the 'editFile' button I notice that only the 'fixed item' comboboxes have cleared their selection, the dynamicly filled comboboxes show the last selected item although the value from the selection itself is null. This looks like a graphics bug to me or am I doing something wrong?

Is there any way around this issue or what is the best method to reset a combobox?

EDIT 2014/08/27:
This is officially not a bug(clearSelection() does not clear value):
https://bugs.openjdk.java.net/browse/JDK-8097244

The official "workaround" is to clear the value of the ComboBox after clearing selection.

cb.getSelectionModel().clearSelection(); // Clear value of ComboBox because clearSelection() does not do it cb.setValue(null); 
like image 344
Perneel Avatar asked Aug 27 '12 13:08

Perneel


People also ask

How do I clear a combobox after selection?

The easiest way to clear the second combobox is to just use Reset(). That will clear all the current selections and reset the combobox to its default selection.


2 Answers

It is very simple. You just need to work with the value property of ComboBox. here you go ....

ComboBox c; c.valueProperty().set(null); 

I hope this works for you :-D

like image 195
Himanshu ARORA Avatar answered Oct 07 '22 23:10

Himanshu ARORA


I ran into nearly the exact same situation and came across your question while looking for a solution. Fortunately, I came up with a workaround that forces the ComboBoxes to reset. When you reset the data on your pane, instead of doing something like:

cboVet.getSelectionModel().clearSelection(); cboVet.getItems.clear(); 

do something like this...

parentNode.getChildren().remove(cboVet); cboVet = new ComboBox();  // do whatever else you need to format your ComboBox parentNode.add(cboVet); 

You'll also need to do a setItems() again on your ComboBox so the new one will be populated. This is not an ideal solution but it seems to be working as I expect the provided clearSelection() method would.

like image 27
Brendan Avatar answered Oct 07 '22 23:10

Brendan