This discussion is archived
7 Replies Latest reply: Apr 9, 2013 11:27 AM by csh RSS

How to clear a combo box

edward17 Newbie
Currently Being Moderated
OK, been a while, here goes:

I have a non-edittable combobox and 3 radio buttons.

When I select a radio button, I reset the items in the list, and execute :
    category.getSelectionModel().clearSelection();
     category.setValue(null);
The intent is a new list, nothing preselected, and the prompt showing inthe text(?) box. And it seems to work at first.

However, when I select the original radio button, the first list is restored, and the combo box shows my original selection!

I would like the prompt to be displayed as with the other radio buttons. I can't even find where the originally select value is stored! It LOOKS like the selection is cleared, as is the value, but the renedered control still shows a selection, even though with the previous list it showed the prompt
     templateToggle.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> ov,
                    Toggle old_toggle, Toggle new_toggle) {

                if (templateToggle.getSelectedToggle() != null) {
                    final String tmplType = ((RadioButton) templateToggle.getSelectedToggle()).getText();
                    Preferences.userNodeForPackage(Evidentia.class).put(Const.O_TMPL_TYPE,
                            tmplType);

                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            category.getSelectionModel().clearSelection();
                            category.setValue(null);
                            category.getEditor().setText("");  << was getting desparate
                            ListManager.getInstance().setTemplateCategoryList(TemplateDao.getInstance().findCategories(tmplType));   << template list is the list behind the combo
                        }
                    });

                }
            }
        });
Thoughts?

Edited by: edward17 on Apr 8, 2013 11:20 AM

Edited by: edward17 on Apr 8, 2013 5:11 PM

Edited by: edward17 on Apr 8, 2013 5:12 PM
  • 1. Re: How to clear a combo box
    jsmith Guru
    Currently Being Moderated
    edward, please format the code with code tags, thx!

    https://forums.oracle.com/forums/ann.jspa?annID=1622 "Announcement: How to post code"
  • 2. Re: How to clear a combo box
    James_D Guru
    Currently Being Moderated
    That looks like a bug: you should file a JIRA for it.

    As you mentioned, the state seems to be updated correctly: both getSelectionModel().getSelectedIndex() and getSelectionModel.getSelectedItem() return the correct value.

    The only workaround I can find is to set an explicit buttonCell on the combo box, and to call setText(null) on the buttonCell to clear the displayed selection:
    import java.util.HashMap;
    import java.util.Map;
    
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.Scene;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.RadioButton;
    import javafx.scene.control.Toggle;
    import javafx.scene.control.ToggleGroup;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class SwitchableCombo extends Application {
    
      @Override
      public void start(Stage primaryStage) {
        final Map<String, ObservableList<String>> comboData = new HashMap<>();
        comboData.put("Beer", FXCollections.observableArrayList("IPA", "Stout", "Porter", "Dubbel"));
        comboData.put("Wine", FXCollections.observableArrayList("Cabernet Sauvingnon", "Zinfandel", "Merlot", "Malbec", "Pinoit Noir"));
    
        final ComboBox<String> combo = new ComboBox<>();
        final ListCell<String> buttonCell = new ListCell<String>() {
          @Override
          public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            setText(item);
          }
        };
        combo.setButtonCell(buttonCell);
        final HBox buttons = new HBox(5);
        final ToggleGroup toggleGroup = new ToggleGroup();
        for (String type : comboData.keySet()) {
          final RadioButton button = new RadioButton(type);
          buttons.getChildren().add(button);
          toggleGroup.getToggles().add(button);
        }
        toggleGroup.selectedToggleProperty().addListener(
            new ChangeListener<Toggle>() {
              @Override
              public void changed(ObservableValue<? extends Toggle> obs,
                  Toggle oldToggle, Toggle newToggle) {
                final ObservableList<String> items = comboData.get(((RadioButton) newToggle).getText());
                combo.setItems(items);
                combo.getSelectionModel().clearSelection();
                buttonCell.setText(null);
              }
            });
    
        combo.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
              @Override
              public void changed(ObservableValue<? extends Number> obs,
                  Number oldValue, Number newValue) {
                System.out.println(newValue);
              }
            });
    
        combo.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
              @Override
              public void changed(ObservableValue<? extends String> obs,
                  String oldValue, String newValue) {
                System.out.println(newValue);
              }
            });
    
        VBox root = new VBox(15);
        root.getChildren().addAll(buttons, combo);
        primaryStage.setScene(new Scene(root, 200, 400));
        primaryStage.show();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    }
  • 3. Re: How to clear a combo box
    edward17 Newbie
    Currently Being Moderated
    Yeah, I had tried clearing the "default" buttonCell, but there isn't one? getButtonCell() returned null, so nothing to clear.
  • 4. Re: How to clear a combo box
    James_D Guru
    Currently Being Moderated
    Yes, that's what I discovered too. I had to explicitly create one as in the example above.
  • 5. Re: How to clear a combo box
    edward17 Newbie
    Currently Being Moderated
    Setting it to null didn't work for me, but setting it to the prompt text I desired worked great.

    Thanks for the work around.
  • 6. Re: How to clear a combo box
    csh Journeyer
    Currently Being Moderated
    I think this is bug, which I also discovered a while ago.

    I just searched in Jira for a bug but didn't found any.
  • 7. Re: How to clear a combo box
    csh Journeyer
    Currently Being Moderated
    Found it:

    http://javafx-jira.kenai.com/browse/RT-27654

    Did ComboBox change in the recent JavaFX releases?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points