Last active
November 3, 2020 04:16
-
-
Save sedj601/bae1f33c6bfa47e56882dc79c3dda60b to your computer and use it in GitHub Desktop.
StackOverflow question https://stackoverflow.com/questions/64650162/javafx-combobox-remove-selected-item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.ComboBox; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
/** | |
* JavaFX App | |
*/ | |
public class App extends Application | |
{ | |
@Override | |
public void start(Stage primaryStage) | |
{ | |
ComboBox<String> comboBox = new ComboBox(); | |
comboBox.getItems().add("Choice 1"); | |
comboBox.getItems().add("Choice 2"); | |
comboBox.getItems().add("Choice 3"); | |
//Remove old on new selection. | |
comboBox.getSelectionModel().selectedItemProperty().addListener((ov, t, t1) -> { | |
if (t != null) { | |
comboBox.getItems().remove(t); | |
} | |
}); | |
//Remove selected item on button press. | |
Button btn = new Button("remove"); | |
btn.setOnAction(t -> { | |
String selectedItem = comboBox.getSelectionModel().getSelectedItem(); | |
comboBox.setValue(null); | |
comboBox.getItems().remove(selectedItem); | |
}); | |
VBox vBox = new VBox(comboBox, btn); | |
Scene scene = new Scene(vBox); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) | |
{ | |
launch(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment