Solved

Context menu on JTable

Posted on 2004-09-30
8
2,071 Views
Last Modified: 2008-01-09

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
Comment
Question by:adg
  • 3
  • 3
  • 2
8 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12187774
Add a mouseListener to your jTable and react on mouseClicked()
0
 
LVL 92

Expert Comment

by:objects
ID: 12187775
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
 
LVL 37

Accepted Solution

by:
zzynx earned 150 total points
ID: 12187834
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:adg
ID: 12187905
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
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 12187912
0
 
LVL 3

Author Comment

by:adg
ID: 12193966

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
 
LVL 37

Expert Comment

by:zzynx
ID: 12194014
Thanks for accepting
0
 
LVL 92

Expert Comment

by:objects
ID: 12194473
(:
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
java stored proc example 9 45
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
Setup GlassFish 4 22
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question