Skip to content

Instantly share code, notes, and snippets.

@sedj601
Last active November 3, 2020 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sedj601/bae1f33c6bfa47e56882dc79c3dda60b to your computer and use it in GitHub Desktop.
Save sedj601/bae1f33c6bfa47e56882dc79c3dda60b to your computer and use it in GitHub Desktop.
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