Solved

Context menu on JTable

Posted on 2004-09-30
8
2,072 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why my table column Id is not passed to java object? 4 46
throw exception 21 67
batch vs regular insert in spring DAO 2 22
Coding for the first time 9 65
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

756 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