Combobox clearing value issue

前端 未结 8 2589
逝去的感伤
逝去的感伤 2020-12-15 06:00

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 (w
相关标签:
8条回答
  • 2020-12-15 06:08

    I use reflection with direct manipulation of buttonCell field in ComboBox skin:

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static <T> void resetComboBox(ComboBox<T> combo) {
        Skin<?> skin = combo.getSkin();
        if(skin==null){
            return;
        }
        combo.setValue(null);
        Field buttonCellField;
        try {
            buttonCellField = skin.getClass().getDeclaredField("buttonCell");
            buttonCellField.setAccessible(true);
            ListCell buttonCell = (ListCell) buttonCellField.get(skin);
            if(buttonCell!=null){
                StringProperty text = buttonCell.textProperty();
                text.set("");
                buttonCell.setItem(null);
            }
        } catch (NoSuchFieldException 
                | SecurityException 
                | IllegalArgumentException 
                | IllegalAccessException e) {
            e.printStackTrace();
        }
    
    }
    

    I think it's also possible by providing your own buttonCell implementation through buttonCellFactory property

    0 讨论(0)
  • 2020-12-15 06:12

    To clear SelectionModel I found nothing better than creating a new instance of Combobox (previous answers update):

    myParentNode.getChildren().remove(myCombobox);
    myCombobox = new ComboBox();
    myParentNode.add(myCombobox);
    

    But this solution evolves other problems: if you use fxml, this combobox will be placed in the wrong place and with wrong parameters. Some fxml parameters are hardly reproduced directly from your controller class code and this is awful to do it every time you need to clear the combobox.

    The solution is using custom components instead of creating instances directly in main controller class code, even if these components are standard. This also helps to free some lines in your main controller class by moving component related event methods and other methods to a separate class file, where you use a reference to your main controller class.

    How to create custom components in JavaFX FXML Application can be found in http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm , but note that CustomControlExample class is not needed for every custom component in your application, if it already has an entry point class with start(Satge stage) method.

    How to resolve possible errors with reference from custom component controller class to main controller class can be found in JavaFx: how to reference main Controller class instance from CustomComponentController class?

    0 讨论(0)
  • 2020-12-15 06:16

    You can retrieve the items and have them all removed:

    cboVet.getItems().removeAll(cboVet.getItems());
    
    0 讨论(0)
  • 2020-12-15 06:23

    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.

    0 讨论(0)
  • 2020-12-15 06:25

    I need to clear selection of the combo box. And this code worked for me:

     List<Object> list = new ArrayList<>(comboBox.getItems());
     comboBox.getItems().removeAll(list);
     comboBox.getItems().addAll(list);
    
    0 讨论(0)
  • 2020-12-15 06:28

    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

    0 讨论(0)
提交回复
热议问题