Description
See https://community.oracle.com/message/12346284
java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.TabPaneSkin$TabHeaderSkin.access$1400(TabPaneSkin.java:1026)
at com.sun.javafx.scene.control.skin.TabPaneSkin.removeTab(TabPaneSkin.java:386)
at com.sun.javafx.scene.control.skin.TabPaneSkin.handleClosedTab(TabPaneSkin.java:263)
at com.sun.javafx.scene.control.skin.TabPaneSkin.access$400(TabPaneSkin.java:92)
at com.sun.javafx.scene.control.skin.TabPaneSkin$3.handle(TabPaneSkin.java:251)
at com.sun.javafx.scene.control.skin.TabPaneSkin$3.handle(TabPaneSkin.java:249)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:231)
package testjavafx;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TabPane.TabClosingPolicy;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestJavaFx extends Application {
private TabPane tabPane;
private Tab addTab;
private Tab currentTab;
@Override
public void start(Stage primaryStage) {
//Create the tab pane and the 'addTab' for adding new tabs.
tabPane = new TabPane();
tabPane.setTabClosingPolicy(TabClosingPolicy.SELECTED_TAB);
addTab = new Tab("+");
addTab.setClosable(false);
tabPane.getTabs().add(addTab);
//Add a listener to listen for changes to tab selection.
tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
@Override
public void changed(ObservableValue<? extends Tab> observable, Tab oldSelectedTab, Tab newSelectedTab) {
//If we change to the addTab create a
//new tab and change selection.
if (newSelectedTab == addTab) {
//Create the new tab.
createNewTab();
} else {
currentTab = newSelectedTab;
}
}
});
//Create a new tab for initial load of the app
createNewTab();
StackPane root = new StackPane();
root.getChildren().add(tabPane);
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Tab Test");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private Tab createNewTab() {
Tab newTab = new Tab("New Tab");
newTab.setClosable(true);
tabPane.getTabs().add(tabPane.getTabs().size() - 1, newTab);
tabPane.getSelectionModel().select(newTab);
return newTab;
}
}
java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.TabPaneSkin$TabHeaderSkin.access$1400(TabPaneSkin.java:1026)
at com.sun.javafx.scene.control.skin.TabPaneSkin.removeTab(TabPaneSkin.java:386)
at com.sun.javafx.scene.control.skin.TabPaneSkin.handleClosedTab(TabPaneSkin.java:263)
at com.sun.javafx.scene.control.skin.TabPaneSkin.access$400(TabPaneSkin.java:92)
at com.sun.javafx.scene.control.skin.TabPaneSkin$3.handle(TabPaneSkin.java:251)
at com.sun.javafx.scene.control.skin.TabPaneSkin$3.handle(TabPaneSkin.java:249)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:231)
package testjavafx;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TabPane.TabClosingPolicy;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestJavaFx extends Application {
private TabPane tabPane;
private Tab addTab;
private Tab currentTab;
@Override
public void start(Stage primaryStage) {
//Create the tab pane and the 'addTab' for adding new tabs.
tabPane = new TabPane();
tabPane.setTabClosingPolicy(TabClosingPolicy.SELECTED_TAB);
addTab = new Tab("+");
addTab.setClosable(false);
tabPane.getTabs().add(addTab);
//Add a listener to listen for changes to tab selection.
tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
@Override
public void changed(ObservableValue<? extends Tab> observable, Tab oldSelectedTab, Tab newSelectedTab) {
//If we change to the addTab create a
//new tab and change selection.
if (newSelectedTab == addTab) {
//Create the new tab.
createNewTab();
} else {
currentTab = newSelectedTab;
}
}
});
//Create a new tab for initial load of the app
createNewTab();
StackPane root = new StackPane();
root.getChildren().add(tabPane);
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Tab Test");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private Tab createNewTab() {
Tab newTab = new Tab("New Tab");
newTab.setClosable(true);
tabPane.getTabs().add(tabPane.getTabs().size() - 1, newTab);
tabPane.getSelectionModel().select(newTab);
return newTab;
}
}