2015-12-30 47 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

的示例代碼展示瞭如何通過標籤按日誌按鈕迭代。