How to remove all the values in a ComboBox?

10,029

Solution 1

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

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

Solution 2

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

Share:
10,029
TomJ
Author by

TomJ

Am friendly...

Updated on July 10, 2022

Comments

  • TomJ
    TomJ almost 2 years
            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 ?