Solved

Context menu on JTable

Posted on 2004-09-30
8
2,064 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now