+ 1

How can I show jpopup menu on jtable when right clicking even if there's no row on jtable

I just want to show the jpopup menu after right clicking the jtable component even there's no row on it. how can i do that in netbeans?

30th May 2023, 10:03 AM
Totski
Totski - avatar
5 Réponses
+ 1
To show a JPopupMenu when right-clicking on a JTable, even if there are no rows present, you can add a MouseListener to the JTable and listen for the right mouse button event. When the event occurs, you can display the JPopupMenu at the mouse's location. Here's an example of how you can achieve this in NetBeans: Open your NetBeans project and navigate to the JFrame containing your JTable. Right-click on the JTable component and select "Events" -> "Mouse" -> "Mouse Pressed" to generate a mousePressed event handler. Inside the generated mousePressed event handler, add the following code: private void jTable1MousePressed(java.awt.event.MouseEvent evt) { if (SwingUtilities.isRightMouseButton(evt)) { JPopupMenu popupMenu = new JPopupMenu(); // Add menu items to the popupMenu int x = evt.getX(); int y = evt.getY(); popupMenu.show(jTable1, x, y); } } Customize the JPopupMenu by adding the desired menu items using the popupMenu.add() method. Now, when you right-click on the JTable, the JPopupMenu will be displayed at the mouse's location, even if there are no rows present. Remember to replace "jTable1" with the actual name of your JTable component.
31st May 2023, 2:49 AM
Jared
Jared - avatar
0
It doesn't work
31st May 2023, 8:02 AM
Totski
Totski - avatar
0
Inside generated mousePressed event here's the code If(SwingUtilities.isRightMouseButton(evt)){ popupMenuMedicine.show(tblMedicine, evt.getX(), evt.getY()) Note: I already add a menu item here }
31st May 2023, 8:11 AM
Totski
Totski - avatar
0
Is there any other way?
31st May 2023, 11:09 PM
Totski
Totski - avatar
0
up
1st Jun 2023, 5:51 PM
Mohamed Essam Eskander
Mohamed Essam Eskander - avatar