2 Replies Latest reply: Apr 26, 2013 3:27 AM by 943372 RSS

    Bring control "to front"

    943372
      Hi,

      I determine a control by its id. After I have found it, I have to make it visible. This means if the control is in a TabPane on page 2 but page 1 is shown, the page 2 should be selected.
      Is there a generic way to do this or is it necessary to analyse the ui tree and activate tab 2 by my own?

      Regards
      Oliver
        • 1. Re: Bring control "to front"
          shakir.gusaroff
          You can do it adding a change listener to the visible property of the tab content:
             recRed.visibleProperty().addListener(new ChangeListener<Boolean>() {
                      public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                          if(newValue.booleanValue())
                             red.getTabPane().getSelectionModel().select(red);
                          }
                  } );
           
          Herei s an example:
          mport javafx.application.Application;
          import javafx.beans.value.ChangeListener;
          import javafx.beans.value.ObservableValue;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.control.Tab;
          import javafx.scene.control.TabPane;
          import javafx.scene.layout.StackPane;
          import javafx.scene.layout.VBox;
          import javafx.scene.paint.Color;
          import javafx.scene.shape.Rectangle;
          import javafx.stage.Stage;
          
          
          public class TabPaneTest extends Application {
          
              @Override
              public void start(Stage primaryStage) {
                  TabPane tabPane = new TabPane();
                final  Tab red = new Tab();
                  red.setText("Red");
                final  Rectangle recRed = new Rectangle(200, 200, Color.RED);
                  recRed.visibleProperty().addListener(new ChangeListener<Boolean>() {
                      public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                          if(newValue.booleanValue())
                             red.getTabPane().getSelectionModel().select(red);
                          }
                  } );
           
                  
                  red.setContent(recRed);
          
          
              final    Tab black = new Tab();
                  black.setText("Black");
                final  Rectangle recBlack = new Rectangle(200, 200, Color.BLACK);
                  recBlack.visibleProperty().addListener(new ChangeListener<Boolean>() {
                      public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                          if(newValue.booleanValue())
                             red.getTabPane().getSelectionModel().select(black);
                          }
                  } );
                  
                  
                  black.setContent(recBlack);
                  tabPane.getTabs().addAll(red, black);
          
                  Button btnBlack = new Button();
                  btnBlack.setText("Set Black");
                  btnBlack.setOnAction(new EventHandler<ActionEvent>() {
                      @Override
                      public void handle(ActionEvent event) {
                         recBlack.setVisible(true);
                         
                      }
                  });
                  
                  
                  Button btnRed = new Button();
                  btnRed.setText("Set Red  ");
                  btnRed.setOnAction(new EventHandler<ActionEvent>() {
                      @Override
                      public void handle(ActionEvent event) {
                         recRed.setVisible(true);
                         
                      }
                  });
                         
          
                  VBox root = new VBox(5);
                  root.getChildren().addAll( btnBlack,  btnRed,tabPane);
                  Scene scene = new Scene(root, 300, 250);
                  primaryStage.setScene(scene);
                  primaryStage.show();
              }
          
              public static void main(String[] args) {
                  launch(args);
              }
          }
          • 2. Re: Bring control "to front"
            943372
            Ok thanks. In my use case I get a fxml and a id of a control (for instance a text field). After loading of the fxml I lookup the control by its id. Then I have to ensure that the control is visible and in the case of the text field, it is getting focus.
            Probably I have to check all parents of the text field. I think it only can work if 1st all parents of the text field are made visible and afterward the focus request will work?