2 Replies Latest reply: Jun 18, 2013 12:20 PM by acepsut RSS

    How to change text for tabs?

    acepsut

      This simple code adds tab(s) to a tab pane, with setText as "New Workspace".

       

      @FXML
          private void addNewWorkspaceTab(ActionEvent event) {
                      
              Tab workspaceTab = new Tab();
              workspaceTab.setText("New Workspace");
              tabpaneWorkspace.getTabs().addAll(workspaceTab);
              tabpaneWorkspace.setTabClosingPolicy(TabPane.TabClosingPolicy.SELECTED_TAB);
            
          }
      

       

      By double left mouse click I would like to delete the text and type a new text from the keyboard.

       

      Any help really appreciated.

        • 1. Re: How to change text for tabs?
          James_D

          Actually easier than I thought

           

           

           

          import 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.Label;
          import javafx.scene.control.Tab;
          import javafx.scene.control.TabPane;
          import javafx.scene.control.TextField;
          import javafx.scene.input.MouseEvent;
          import javafx.scene.layout.BorderPane;
          import javafx.stage.Stage;
          
          
          public class EditableTabTest extends Application {
          
          
            @Override
            public void start(Stage primaryStage) {
            BorderPane root = new BorderPane();
            TabPane tabPane = new TabPane();
              tabPane.getTabs().add(createEditableTab("New Workspace"));
              tabPane.getTabs().add(createEditableTab("Another tab"));
              root.setCenter(tabPane);
              primaryStage.setScene(new Scene(root, 600, 400));
              primaryStage.show();
            }
          
            private Tab createEditableTab(String text) {
             final Label label = new Label(text);
             final Tab tab = new Tab();
             tab.setGraphic(label);
             final TextField textField = new TextField();
             label.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                  if (event.getClickCount()==2) {
                    textField.setText(label.getText());
                    tab.setGraphic(textField);
                    textField.selectAll();
                    textField.requestFocus();
                  }
                }
              });
             textField.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                  label.setText(textField.getText());
                  tab.setGraphic(label);
                }
              });
             textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
          
          
                @Override
                public void changed(ObservableValue<? extends Boolean> observable,
                    Boolean oldValue, Boolean newValue) {
                  if (! newValue) {
                    label.setText(textField.getText());
                    tab.setGraphic(label);          
                  }
                }
              });
             return tab ;
            }
          
          
            public static void main(String[] args) {
            launch(args);
            }
          }
          
          • 2. Re: How to change text for tabs?
            acepsut

            Thank you very much!