This discussion is archived
1 Reply Latest reply: Jan 3, 2013 9:13 AM by James_D RSS

ChoiceBox not "refreshing the selection"

966626 Newbie
Currently Being Moderated
I have the following code:
chbTipoJuris.getSelectionModel().selectFirst();
chbTipoJuris.layout();
The event is fired up and the values received by the event handler are correct, the function does everything right but the choicebox is still showing to the user the same item selected than before the code was issued.
Any help will be appreciated.
  • 1. Re: ChoiceBox not "refreshing the selection"
    James_D Guru
    Currently Being Moderated
    This works fine for me:
    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ChoiceBox;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    
    public class ChoiceBoxSelectionTest extends Application {
    
      @Override
      public void start(Stage primaryStage) {
        final BorderPane root = new BorderPane();
        final ChoiceBox<String> choiceBox = new ChoiceBox<>(
            FXCollections.observableArrayList("Apples", "Oranges", "Pears",
                "Bananas"));
        final Button button = new Button("Select First");
        button.setOnAction(new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent event) {
            choiceBox.getSelectionModel().selectFirst();
          }
        });
        root.setTop(choiceBox);
        root.setBottom(button);
        Scene scene = new Scene(root, 200, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    }
    Can you post a short executable example which demonstrates the problem?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points