I'm using Netbeans and I've designed a window with JTable and added MouseEvent listener on JTable component and added this code:
private void productsTableMousePressed(java.awt.event.MouseEvent evt) {
if(evt.isPopupTrigger()) {
tablePopupMenu.setLocation(evt.getXOnScreen(), evt.getYOnScreen());
tablePopupMenu.setVisible(true);
System.out.println("Fired!");
}
}
private void productsTableMouseReleased(java.awt.event.MouseEvent evt) {
if(evt.isPopupTrigger()) {
tablePopupMenu.setLocation(evt.getXOnScreen(), evt.getYOnScreen());
tablePopupMenu.setVisible(true);
}
}
But it works only when I click on some cells. I want to get it working on whole JTable area. How?
Assuming your table is inside a JScrollPane, it may not cover the entire viewport. To ensure the whole area of your viewport is covered, call setFillsViewportHeight(true)
on your table.
But it works only when I click on some cells, but i want to get it working on whole JTable area
The MouseListener will work on all cells. I don't know if you should be using the setLocation(...) method.
See Bringing Up a Popup Menu for example code.
Or a better approach is to use:
table.setComponentPopupMenu(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With