How to remove all the values in a ComboBox?


Question

        rs = statement.executeQuery("select * from user");

        while (rs.next()) {
            String username = rs.getString("staffname");
            options1.add(username);    // ObservableList<String> options1 = FXCollections.observableArrayList();
        }

        cb.setItems(options1);   // cb is ComboBox object
        cb.setPromptText("Select Your Account");
        cb.setPrefSize(280, 30);

        Button bt = new Button("Sign In");
        bt.setFont(Font.font("Calibri", FontWeight.NORMAL, 17));
        bt.setStyle(" -fx-base: #333333;");
        bt.setTextFill(Color.WHITE);

        bt.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                try {
                    setCenter(userSignin());

                } catch (ClassNotFoundException | SQLException ex) {
                    Logger.getLogger(FrontPage.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

This is my code to read some values from a database and display it in a ComboBox. Now I need to remove all the values in the ComboBox when the Button is pressed. I want to remove all at one click. How can I do it ?

1
0
4/18/2014 3:47:00 PM

Accepted Answer

cb.getItems().clear() should remove everything in the ComboBox.

Edited: Corrected to call the right container. Sorry, used to the children in the panes.

6
1/28/2019 1:57:07 PM

I have tried. cb.getItems().removeAll(); But it is not worked properly. the right method to delete all data inside the comboBox is as follow cb.getItems().clear();

cb- variable name of comboBox


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