I am working on an application with a TabPane where each Tab header has a context menu. It is possible to drag and drop the tabs to rearrange them. After repositioning a tab, the context menu no longer appears. Here is a minimal program that reproduces the issue (by programmatically moving one tab) for me on 8u40 b06 (uncomment the lines indicated to see the issue):
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;
public class TabPaneTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
final TabPane tabPane = new TabPane();
Tab t1 = new Tab("T1");
t1.setContextMenu(createContextMenu("T1"));
Tab t2 = new Tab("T2");
t2.setContextMenu(createContextMenu("T2"));
Tab t3 = new Tab("T3");
t3.setContextMenu(createContextMenu("T3"));
Tab t4 = new Tab("T4");
t4.setContextMenu(createContextMenu("T4"));
Tab t5 = new Tab("T5");
t5.setContextMenu(createContextMenu("T5"));
tabPane.getTabs().add(t1);
tabPane.getTabs().add(t2);
tabPane.getTabs().add(t3);
tabPane.getTabs().add(t4);
tabPane.getTabs().add(t5);
stage.setScene(new Scene(tabPane));
stage.show();
// uncomment these lines and the context menu on tab 4 is lost
//tabPane.getTabs().remove(t4);
//tabPane.getTabs().add(t4);
//System.out.println(t4.getContextMenu()); // although the menu doesn't appear, it is not null on the Tab
}
private static ContextMenu createContextMenu(String prefix) {
final ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem(prefix + ": About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item2 = new MenuItem(prefix + ": Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
contextMenu.getItems().addAll(item1, item2);
return contextMenu;
}
}
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;
public class TabPaneTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
final TabPane tabPane = new TabPane();
Tab t1 = new Tab("T1");
t1.setContextMenu(createContextMenu("T1"));
Tab t2 = new Tab("T2");
t2.setContextMenu(createContextMenu("T2"));
Tab t3 = new Tab("T3");
t3.setContextMenu(createContextMenu("T3"));
Tab t4 = new Tab("T4");
t4.setContextMenu(createContextMenu("T4"));
Tab t5 = new Tab("T5");
t5.setContextMenu(createContextMenu("T5"));
tabPane.getTabs().add(t1);
tabPane.getTabs().add(t2);
tabPane.getTabs().add(t3);
tabPane.getTabs().add(t4);
tabPane.getTabs().add(t5);
stage.setScene(new Scene(tabPane));
stage.show();
// uncomment these lines and the context menu on tab 4 is lost
//tabPane.getTabs().remove(t4);
//tabPane.getTabs().add(t4);
//System.out.println(t4.getContextMenu()); // although the menu doesn't appear, it is not null on the Tab
}
private static ContextMenu createContextMenu(String prefix) {
final ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem(prefix + ": About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item2 = new MenuItem(prefix + ": Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
contextMenu.getItems().addAll(item1, item2);
return contextMenu;
}
}