开发者

Disable popup menu on a JTable

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 wan开发者_如何学Got 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?


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());
        }
    }
});


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜