JavaFX:TabPane上下文菜单,具体取决于所选选项卡 [英] JavaFX: TabPane context menu, depending on selected tab

查看:157
本文介绍了JavaFX:TabPane上下文菜单,具体取决于所选选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个带有多个标签的TabPane,每个标签的上下文菜单都不同。

I want to have a TabPane with multiple tabs, with a context menu different for every tab.

在TabPane上设置ContextMenu显然会导致使用相同的ContextMenu,无论选择的选项卡如何。我尝试在不同的选项卡上设置不同的ContextMenus,但这有两个不需要的效果:

Setting the ContextMenu on the TabPane obviously results in the same ContextMenu used regardless of selected tab. I tried setting different ContextMenus on different Tabs, but this has two unwanted effects:


  1. 上下文菜单仅在正确时打开单击选项卡标题(而在TabPane上设置它允许右键单击选项卡内的任何位置)。我希望用户能够从选项卡内的任何位置访问上下文菜单,而不仅仅是标题。

  1. The context menu is only opened when right-clicking on the tab header (whereas setting it on the TabPane allows right-clicking anywhere inside the tab). I want the user to be able to access the Context Menu from anywhere inside the tab, not just the header.

右键单击一个标题页,而另一个标题页选中后仍会打开单击选项卡的上下文菜单 - 我希望上下文菜单取决于当前所选的选项卡。

Right-clicking on one tab header while another is selected still opens the context menu of the tab clicked on - I want the context menu to depend on the tab currently selected.

我能想到如何做的唯一方法是捕获onContextMenuRequested,查看当前选中的哪个选项卡,并设置各种MenuItem可见/不可见,但这看起来很傻。有没有更好的方法呢?

The only way I could think of how to do it is to catch onContextMenuRequested, see which tab is currently selected, and set various MenuItems visible/invisible, but that seems pretty silly. Is there a better way of doing this?

编辑:Calrification - 选项卡内容是Panes(VBox,GridPane等),因此直接在内容上设置ContextMenu是可悲的是不可能。

Calrification - the tab contents are Panes (VBox, GridPane, etc.) so setting the ContextMenu directly on the content is sadly impossible.

推荐答案

我可以看到两个解决方案。一种是在选项卡窗格上设置单个上下文菜单。使用选定的选项卡注册一个监听器,并在选择更改时重新填充上下文菜单。

I can see two solutions to this. One is to set a single context menu on the tab pane. Register a listener with the selected tab, and repopulate the context menu when the selection changes.

另一个解决方案是在选项卡的内容上设置上下文菜单。请注意,您可以通过注册 contextMenuRequested 事件的处理程序在任何节点上设置上下文菜单,并显示上下文菜单。您可以在选项卡上设置相同的上下文菜单。

The other solution is just to set the context menu on the content of the tabs. Note that you can set a context menu on any node, by registering a handler for the contextMenuRequested event, and show the context menu. You can set the same context menu on the tab.

此示例演示了这两种技巧:

This example demonstrates both techniques:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javafx.application.Application;
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.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class TabPanesWithContextMenu extends Application {

    @Override
    public void start(Stage primaryStage) {
        TabPane tabPane1 = new TabPane();
        ContextMenu contextMenu = new ContextMenu();
        tabPane1.setContextMenu(contextMenu);

        List<MenuItem> tab1Items = new ArrayList<>();
        tab1Items.add(new MenuItem("Choice 1"));
        tab1Items.add(new MenuItem("Choice 2"));

        List<MenuItem> tab2Items = new ArrayList<>();
        tab2Items.add(new MenuItem("Choice 3"));
        tab2Items.add(new MenuItem("Choice 4"));

        Tab tab1 = new Tab("Tab 1");
        tab1.setContent(new Pane());
        Tab tab2 = new Tab("Tab 2");
        tab2.setContent(new Pane());
        tabPane1.getTabs().addAll(tab1, tab2);

        Map<Tab, List<MenuItem>> itemsByTab = new HashMap<>();
        itemsByTab.put(tab1, tab1Items);
        itemsByTab.put(tab2, tab2Items);

        tabPane1.getSelectionModel().selectedItemProperty().addListener((obs, oldTab, newTab) -> 
           contextMenu.getItems().setAll(itemsByTab.get(newTab)) );

        contextMenu.getItems().addAll(tab1Items);

        TabPane tabPane2 = new TabPane();

        Tab tab3 =  createTabWithContextMenu("Tab 3", new MenuItem("Choice 5"), new MenuItem("Choice 6"));
        Tab tab4 =  createTabWithContextMenu("Tab 4", new MenuItem("Choice 7"), new MenuItem("Choice 8"));

        tabPane2.getTabs().addAll(tab3, tab4);

        HBox root = new HBox(10, tabPane1, tabPane2);

        primaryStage.setScene(new Scene(root, 600, 600));
        primaryStage.show();
    }

    private Tab createTabWithContextMenu(String title, MenuItem... items) {
        Tab tab = new Tab(title);
        ContextMenu contextMenu = new ContextMenu(items);
        tab.setContextMenu(contextMenu);

        Pane content = new Pane();
        content.setOnContextMenuRequested(e -> 
            contextMenu.show(content, e.getScreenX(), e.getScreenY()));
        tab.setContent(content);

        return tab ;
    }

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

这篇关于JavaFX:TabPane上下文菜单,具体取决于所选选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆