无法正确设置可见组件

时间:2013-06-26 06:40:59

标签: javafx-2 javafx javafx-8

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

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!");
        }
    }); 

我只有在选中了复选框后才能显示这个tabpane:

public static boolean renderTab;

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。

1 个答案:

答案 0 :(得分:1)

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

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

只有使用此行,您的tabpane才会在检查时显示