• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2079
  • Last Modified:

Context menu on JTable


Hi, I want to have a context menu so when the user right-clicks on my jtable I can offer a menu of choices. When a choice is selected it will operate on the row that was clicked on so I need to pick up both the menu choice and the row that was right-clicked on.  Can you tell me how to do this?

Thanks!
0
adg
Asked:
adg
  • 3
  • 3
  • 2
2 Solutions
 
zzynxSoftware engineerCommented:
Add a mouseListener to your jTable and react on mouseClicked()
0
 
objectsCommented:
Add a MouseListener to your table and have it get the row clicked on according to the click coords, and then popup a JPopupMenu.
0
 
zzynxSoftware engineerCommented:
1) Add mouselistener

        jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableMouseClicked(evt);
            }
        });

2) React

    private void jTableMouseClicked(java.awt.event.MouseEvent evt) {
        if ( SwingUtilities.isRightMouseButton(evt) ) {     // Was it a right-mouse click ?

            // a. Select the table item where one right-clicked
            //     if the right click is not on (one of) the selected item(s)
            int row = jTable.rowAtPoint(evt.getPoint());
            if (row!=-1) {
                int selected[] = jTable.getSelectedRows();
                boolean clickOnSelectedOne = false;
                for (int i=0; i<selected.length; i++) {
                    if (row==selected[i]) {
                        clickOnSelectedOne = true;
                        break;
                    }
                }
                if (!clickOnSelectedOne)
                    jTable.setRowSelectionInterval(row, row);
            }

            // b. Create the right menu
            popupMenu = new JPopupMenu();
            // Here your add the menu items you want...

            // c. show it
            popupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }

Remark:
You can omit part a) if not needed.
Part a) just makes sure that right-clicking an item also selects that item. Maybe that's not needed in your app.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
adgAuthor Commented:
Thanks for the quick response!

>> just makes sure that right-clicking an item also selects that item. Maybe that's not needed in your app.

I do need that! I'll give it a try and get back to you - probably in around 10 hours.

 

0
 
objectsCommented:
0
 
adgAuthor Commented:

Of course, JPopupMenu! I should have known. I gotta read more about Swing. :*)

Thanks both for the exellent examples.  I'm splitting the points, appreciate the help!
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
 
objectsCommented:
(:
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now