7

我有一个简单的代码来将选项卡添加到 tabPane

@FXML
private void addNewWorkspaceTab(ActionEvent event) {

    Tab workspaceTab = new Tab();
    workspaceTab.setText("New Workspace");
    tabpaneWorkspace.getTabs().addAll(workspaceTab);
    tabpaneWorkspace.setTabClosingPolicy(TabPane.TabClosingPolicy.SELECTED_TAB);

}

通过鼠标左键双击一个选项卡,我想重命名(通过输入新文本)选定的选项卡:我该怎么做?

4

1 回答 1

2

这是我的问题的解决方案:

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;

/**
*
* @author utente
*/
public class TabSetText {

public 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 ;  
}  

}
于 2014-10-26T03:37:38.713 回答