7 Replies Latest reply on Apr 9, 2013 6:27 PM by csh

    How to clear a combo box

      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 :
      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>() {
                  public void changed(ObservableValue<? extends Toggle> ov,
                          Toggle old_toggle, Toggle new_toggle) {
                      if (templateToggle.getSelectedToggle() != null) {
                          final String tmplType = ((RadioButton) templateToggle.getSelectedToggle()).getText();
                          Platform.runLater(new Runnable() {
                              public void run() {
                                  category.getEditor().setText("");  << was getting desparate
                                  ListManager.getInstance().setTemplateCategoryList(TemplateDao.getInstance().findCategories(tmplType));   << template list is the list behind the combo

      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
          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
            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 {
              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>() {
                  public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                final HBox buttons = new HBox(5);
                final ToggleGroup toggleGroup = new ToggleGroup();
                for (String type : comboData.keySet()) {
                  final RadioButton button = new RadioButton(type);
                    new ChangeListener<Toggle>() {
                      public void changed(ObservableValue<? extends Toggle> obs,
                          Toggle oldToggle, Toggle newToggle) {
                        final ObservableList<String> items = comboData.get(((RadioButton) newToggle).getText());
                combo.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
                      public void changed(ObservableValue<? extends Number> obs,
                          Number oldValue, Number newValue) {
                combo.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
                      public void changed(ObservableValue<? extends String> obs,
                          String oldValue, String newValue) {
                VBox root = new VBox(15);
                root.getChildren().addAll(buttons, combo);
                primaryStage.setScene(new Scene(root, 200, 400));
              public static void main(String[] args) {
            • 3. Re: How to clear a combo box
              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
                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
                  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
                    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
                      Found it:


                      Did ComboBox change in the recent JavaFX releases?