1

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?

2 Answers 2

1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.