I observed one thing about my right click on the JTable.
If I right click on a row. it shows my popUpMenu item.
But now if I right click on another row, I must
right click 2 times on the other row before my Jpopup appears.
Is that any way to make it smoother as in I only need to right click one time on
another row?

table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == java.awt.event.MouseEvent.BUTTON3) {
                    System.out.println("Right Click");
                    int r = table.rowAtPoint(e.getPoint());
                    if (r >= 0 && r < table.getRowCount()) {
                        table.setRowSelectionInterval(r, r);

                    } else {
                        table.clearSelection();
                    }

                    int rowindex = table.getSelectedRow();
                    if (rowindex < 0)
                        return;
                    if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
                        item = popupMenu.add("itemOne");
                        table.setComponentPopupMenu(popupMenu);
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    }
                }
            }
        }); 

Recommended Answers

All 2 Replies

My guess would be the line where you reset/clear the current selected row but not set the new selected row again. As a result, the first click goes into Line 8. The next click on another row goes into Line 11 which in turn is returned to the caller in Line 16. Just a guess from the behavior you explained...

read Oracle tutorial - How to use Tables, part Specifying Tool Tips for Cells

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.