禁用 JTable 上的弹出菜单

发布于 2024-10-16 20:13:54 字数 681 浏览 3 评论 0原文

我正在创建一个像这样的带有弹出菜单的表,

JTable table = new Table()

table.addMouseListener( new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

我希望能够通过调用启用/禁用此菜单,我尝试了以下操作:

table.setEnabled(false),

但菜单仍然弹出。我应该做什么调用?

I am creating a table like this with a popop menu

JTable table = new Table()

table.addMouseListener( new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

I want to be able to enable/disable this menu with a call, I tried this:

table.setEnabled(false)

but the menu still pops up.. What call should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-10-23 20:13:54

您可以在创建名为 tableMenuEnabled 的表的类中使用布尔值。您应该能够在计划调用 table.setEnabled(false) 的地方将 tableMenuEnabled 设置为 true 或 false。从那里你的代码将更改为:

table.addMouseListener( new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (tableMenuEnabled && e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

You could have a boolean inside of the class that is creating the table called tableMenuEnabled. You should be able to set tableMenuEnabled to true or false where you were planning on calling table.setEnabled(false). From there your code would change to:

table.addMouseListener( new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (tableMenuEnabled && e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});
多像笑话 2024-10-23 20:13:54

好吧,您可能必须将其作为侦听器删除。因此,将其保留为类中的成员变量,如下所示:

private MouseAdapter adapt;

您的其他代码将变为:

adapt = new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

table.addMouseListener(adapt);

然后,您的调用将如下所示:

public void disableTableMenu() {
    table.removeMouseListener(adapt);
}

另外,要将其添加回来,您可以具有以下内容:

public void enableTableMenu() {
    table.addMouseListener(adapt);
}

Well, you'd probably have to remove it as a listener. So, keep it as a member variable in your class, like this:

private MouseAdapter adapt;

your other code would become:

adapt = new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

table.addMouseListener(adapt);

then, your call would look like:

public void disableTableMenu() {
    table.removeMouseListener(adapt);
}

also, to add it back in, you could have the following:

public void enableTableMenu() {
    table.addMouseListener(adapt);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文