Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8090069

Button.setDefaultButton(true) does not work when Enter is pressed inside TitledPane

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • 9
    • 7u6
    • javafx

      I have a default button (setDefaultButton(true)), but it won't trigger, when I press Enter on a TextField, which is inside a TitledPane.

      In my example there is a normal TextField on the first tab, to show, that it works usually.

      On the second tab is also a TextField but this time inside a TitlePane. Pressing Enter here does not trigger the button's action.



      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.geometry.Insets;
      import javafx.geometry.Pos;
      import javafx.scene.Scene;
      import javafx.scene.control.*;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.FlowPane;
      import javafx.scene.layout.HBoxBuilder;
      import javafx.scene.layout.VBoxBuilder;
      import javafx.stage.Modality;
      import javafx.stage.Stage;

      public class MainApplication extends Application {

          @Override
          public void start(Stage stage) throws Exception {
              Scene scene = new Scene(new MainApplicationView(), 800, 600);
              stage.setScene(scene);

              stage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }

          private class MainApplicationView extends BorderPane {

              FlowPane buttons = new FlowPane();
              Button btnOk = new Button("OK");

              TabPane tabPane = new TabPane();
              TextField txt1 = new TextField();
              TextField txt2 = new TextField();
              TitledPane titlePane = new TitledPane();

              Stage dialogStage;

              public MainApplicationView() {

                  Tab tab1 = new Tab("Example 1");
                  tab1.setContent(HBoxBuilder.create().children(txt1).padding(new Insets(15, 15, 15, 15)).build());

                  Tab tab2 = new Tab("Example 2");
                  titlePane.setText(" TitlePane ");
                  tab2.setContent(titlePane);
                  titlePane.setContent(HBoxBuilder.create().children(txt2).padding(new Insets(15, 15, 15, 15)).build());

                  tabPane.getTabs().addAll(tab1, tab2);
                  this.setCenter(tabPane);

                  buttons.getChildren().add(btnOk);
                  buttons.setAlignment(Pos.CENTER_RIGHT);
                  this.setBottom(buttons);

                  btnOk.setDefaultButton(true);
                  btnOk.setOnAction(new EventHandler<ActionEvent>() {

                      @Override
                      public void handle(ActionEvent arg0) {
                          createSimpleDialog();
                      }
                  });

              }

              private void createSimpleDialog() {
                  if (dialogStage == null) {
                      dialogStage = new Stage();
                      dialogStage.initModality(Modality.WINDOW_MODAL);

                      Label lbl = new Label("Button clicked");
                      Button btnOk = new Button("Ok");
                      btnOk.setOnAction(new EventHandler<ActionEvent>() {

                          @Override
                          public void handle(ActionEvent arg0) {
                              dialogStage.close();
                          }
                      });

                      dialogStage.setScene(new Scene(VBoxBuilder.create().children(lbl, btnOk).alignment(
                              Pos.CENTER).padding(new Insets(5)).build()));

                  }
                  dialogStage.show();
              }
          }
      }

            jgiles Jonathan Giles
            cschudtjfx Christian Schudt (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: