2015-12-30 31 views
0

我做了它使用tabpane的应用程序。我可以动态设置每个选项卡的工具提示和标题。但是,如何动态设置其内容。我相信,我能保持Node对象的列表和循环过程中将其添加到标签,但我觉得还有其他的方法来做到这一点。这是我迄今为止所做的。JavaFX的setTabcontent动态

public class Index extends Application { 
    public static void main(String[] args) { 
     launch(args); 
    } 

    final String[] tabContent={"title1" 
      ,"title2" 
      ,"title3" 
      ,"title4" 
      ,"title5"}; 
    final String[] tabToolTip={"tooltip1" 
      ,"tooltip2" 
      ,"tooltip3" 
      ,"tooltip4" 
      ,"tooltip5"}; 


    @Override 
    public void start(Stage primaryStage) { 
     primaryStage.setTitle("Ipas Utility"); 
      Group root = new Group(); 
      Scene scene = new Scene(root, 1000, 600, Color.ALICEBLUE); 
      TabPane tabPane = new TabPane(); 
      tabPane.setTooltip(new Tooltip("Hover on each tab for an overview")); 
      BorderPane borderPane = new BorderPane(); 
      for (int i = 0; i < 5; i++) { 
       Tab tab = new Tab(); 
       tab.setText(tabContent[i]); 
       tab.setClosable(false); 
       tab.setTooltip(new Tooltip(tabToolTip[i])); 
       HBox hbox = new HBox(); 
       hbox.getChildren().add(new Label(tabContent[i])); 
       hbox.setAlignment(Pos.CENTER); 
       tab.setContent(hbox);; 
       tabPane.getTabs().add(tab); 
      } 
      // bind to take available space 
      borderPane.prefHeightProperty().bind(scene.heightProperty()); 
      borderPane.prefWidthProperty().bind(scene.widthProperty()); 

      borderPane.setCenter(tabPane); 
      root.getChildren().add(borderPane); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
    } 

} 

我怎么能在外面start块列表/阵列保持tabcontent?

回答

0

这是相当好,你做了什么。这是你的代码的修改版本,它允许你在点击按钮添加一个标签:

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.Tab; 
import javafx.scene.control.TabPane; 
import javafx.scene.control.Tooltip; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

public class Main extends Application { 

    private static int tabCount = 0; 

    @Override 
    public void start(Stage primaryStage) { 

     TabPane tabPane = new TabPane(); 

     tabPane.getTabs().add(createTab()); 
     tabPane.getTabs().add(createTab()); 
     tabPane.getTabs().add(createTab()); 

     Button addTabButton = new Button("Add Tab"); 
     addTabButton.setOnAction(e -> { 
      tabPane.getTabs().add(createTab()); 
     }); 

     Button logButton = new Button("Log"); 
     logButton.setOnAction(e -> { 
      for(Tab tab: tabPane.getTabs()) { 
       System.out.println("Tab " + tab.getText() + " has content " + tab.getContent()); 
      } 
     }); 

     HBox toolbar = new HBox(); 
     HBox.setMargin(addTabButton, new Insets(5,5,5,5)); 
     HBox.setMargin(logButton, new Insets(5,5,5,5)); 
     toolbar.getChildren().addAll(addTabButton, logButton); 

     BorderPane root = new BorderPane(); 
     root.setTop(toolbar); 
     root.setCenter(tabPane); 

     Scene scene = new Scene(root, 640, 480); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 

    } 

    public static Tab createTab() { 

     tabCount++; 

     Tab tab = new Tab(); 
     tab.setText("Tab " + tabCount); 
     tab.setTooltip(new Tooltip("Tooltip Tab " + tabCount)); 

     Node content = new Label("Content Tab " + tabCount); 
     tab.setContent(content); 

     return tab; 

    } 

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

要访问您可以使用getTabs的标签。为了改变内容,我。即表示内容的节点,你可以使用setContent

的示例代码展示了如何通过标签按日志按钮迭代。