Search code examples
javaswingjtablemouselistenerjpopupmenu

Right-click MouseListener on whole JTable component


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?


Solution

  • 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.