0

我正在尝试使用设置为显示或不显示组件的选择框来实现菜单。我有这个复选框:

final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
    toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent e)
        {

            DataTabs.renderTab = toolbarSubMenuNavigation.isSelected();

            // call here the getter setter and send boolean flag
            System.out.println("subsystem1 #1 Enabled!");
        }
    }); 

我有这个选项卡,只有在我选择了复选框时才想渲染它:

公共静态布尔渲染选项卡;

public DataTabs()
{
}

public boolean isRenderTab()
{
    return renderTab;
}

public void setRenderTab(boolean renderTab)
{
    this.renderTab = renderTab;
}

// below this code
tabPane.setVisible(renderTab);

当我运行代码时,它不起作用。我也测试了这个:

DataTabs tabs = new DataTabs(); // instantiate first
tabs.setRenderTab(toolbarSubMenuNavigation.isSelected());

 public static boolean renderTab;

    TabPane tabPane = new TabPane();


    public DataTabs()
    {
    }

    public boolean isRenderTab()
    {
        return renderTab;
    }

    public void setRenderTab(boolean renderTab)
    {
        tabPane.setVisible(renderTab);
    }

但是当我运行代码并选中或取消选中该复选框时,再次没有结果。这是完整的源代码:

http://pastebin.com/tkj4Fby1

也许我需要添加侦听器或其他我缺少的东西?

编辑测试 3

我还测试了这段代码:

final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
        toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {

                DataTabs.toolbarSubMenuNavigation = toolbarSubMenuNavigation;

                // call here the getter setter and send boolean flag
                System.out.println("subsystem1 #1 Enabled!");
            }
        });

// class with tabs

public static CheckMenuItem toolbarSubMenuNavigation;

    public static CheckMenuItem getToolbarSubMenuNavigation()
    {
        return toolbarSubMenuNavigation;
    }

    public static void setToolbarSubMenuNavigation(CheckMenuItem toolbarSubMenuNavigation)
    {
        DataTabs.toolbarSubMenuNavigation = toolbarSubMenuNavigation;
    }

// below
abPane.visibleProperty().bind(toolbarSubMenuNavigation.selectedProperty());

当我运行代码时,我得到了 NPE。

4

1 回答 1

1

当您选中一行中的框时,您可以轻松地告诉您的标签可见

yourTab.visibleProperty().bind(yourCheckBox.selectedProperty());

仅使用此行,您的 tabpane 将仅在选中时可见

于 2013-06-26T07:05:09.973 回答