Advertisement
Guest User

Untitled

a guest
Mar 27th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package testjavafx;
  2.  
  3. import javafx.application.Application;
  4. import javafx.beans.value.ChangeListener;
  5. import javafx.beans.value.ObservableValue;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Tab;
  8. import javafx.scene.control.TabPane;
  9. import javafx.scene.control.TabPane.TabClosingPolicy;
  10. import javafx.scene.layout.StackPane;
  11. import javafx.stage.Stage;
  12.  
  13. public class TestJavaFx extends Application {
  14.  
  15.     private TabPane tabPane;
  16.     private Tab addTab;
  17.     private Tab currentTab;
  18.  
  19.     @Override
  20.     public void start(Stage primaryStage) {
  21.  
  22.         //Create the tab pane and the 'addTab' for adding new tabs.
  23.         tabPane = new TabPane();
  24.         tabPane.setTabClosingPolicy(TabClosingPolicy.SELECTED_TAB);
  25.  
  26.         addTab = new Tab("+");
  27.         addTab.setClosable(false);
  28.         tabPane.getTabs().add(addTab);
  29.  
  30.         //Add a listener to listen for changes to tab selection.
  31.         tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
  32.             @Override
  33.             public void changed(ObservableValue<? extends Tab> observable, Tab oldSelectedTab, Tab newSelectedTab) {
  34.  
  35.                 //If we change to the addTab create a
  36.                 //new tab and change selection.
  37.                 if (newSelectedTab == addTab) {
  38.                     //Create the new tab.
  39.                     createNewTab();
  40.                 } else {
  41.                     currentTab = newSelectedTab;
  42.                 }
  43.             }
  44.         });
  45.         //Create a new tab for initial load of the app
  46.         createNewTab();
  47.  
  48.         StackPane root = new StackPane();
  49.         root.getChildren().add(tabPane);
  50.  
  51.         Scene scene = new Scene(root, 500, 500);
  52.  
  53.         primaryStage.setTitle("Tab Test");
  54.         primaryStage.setScene(scene);
  55.         primaryStage.show();
  56.     }
  57.  
  58.     /**
  59.      * @param args the command line arguments
  60.      */
  61.     public static void main(String[] args) {
  62.         launch(args);
  63.     }
  64.  
  65.     private Tab createNewTab() {
  66.         Tab newTab = new Tab("New Tab");
  67.         newTab.setClosable(true);
  68.         tabPane.getTabs().add(tabPane.getTabs().size() - 1, newTab);
  69.         tabPane.getSelectionModel().select(newTab);
  70.         return newTab;
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement