Issue
I am working on a project where I have ComboBox in a pane, which has "Games", "Albums", and "Movies".
I want to open another Pane when one of these is selected.
How can I do that?
Here is my code:
GridPane select = new GridPane();
select.setAlignment(Pos.CENTER);
select.setHgap(6.5);
select.setVgap(6.5);
select.add(new Label("Media Type"), 0, 1)
ComboBox<String> comboBox = new ComboBox<String>();
comboBox.getItems().add("Movies");
comboBox.getItems().add("Albums");
comboBox.getItems().add("Games");
HBox hbox = new HBox(comboBox);
Scene thirdScene = new Scene(hbox, 500, 500);
Stage thirdStage = new Stage();
thirdStage.setScene(thirdScene);
thirdStage.setTitle("Media Menu");
thirdStage.show();
Solution
I strongly suggest you to go through the API (java doc) of controls you use to have a quick idea of what they are capable of.
You can either listen to the valueProperty or selectedItemProperty or you can set an action.
comboBox.setOnAction(e->{
String value= comboBox.getValue();
... // Do the stuff with value
});
or
comboBox.valueProperty().addListener((obs,old,value)->{
... // Do the stuff with value
});
or
comboBox.getSelectionModel().selectedItemProperty().addListener((obs,old,value)->{
... // Do the stuff with value
});
Answered By - Sai Dandem
Answer Checked By - Mary Flores (JavaFixing Volunteer)