Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Context menu on JTable

Posted on 2004-09-30
8
Medium Priority
?
2,078 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 600 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
Independent Software Vendors: 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 400 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

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.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

618 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