Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 788
Mikołaj Stolarski Avatar asked Jan 17 '12 23:01

Mikołaj Stolarski


2 Answers

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.

like image 133
jackrabbit Avatar answered Nov 04 '22 11:11

jackrabbit


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(...);
like image 32
camickr Avatar answered Nov 04 '22 10:11

camickr