Вопрос

        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 ?

Это было полезно?

Решение

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

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

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top