+ 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?
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.
0
It doesn't work
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
}
0
Is there any other way?
0
up