Right-click MouseListener on the whole JTable component

I use Netbeans and I designed a window with JTable and added a MouseEvent listener to the 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 only works when I click on some cells. I want him to work on the entire JTable area. How?

+4
source share
3 answers

Assuming your table is inside a JScrollPane, it may not cover the entire viewport. To provide coverage for the entire viewport, call setFillsViewportHeight(true) in your table.

+2
source

But it only works when I click on some cells, but I want it to work on the entire JTable area

MouseListener will work in all cells. I do not know whether to use the setLocation (...) method.

See Bringing a pop-up menu , for example, code.

Or better to use:

 table.setComponentPopupMenu(...); 
+3
source

I found that in my JTable (which is located in the JScrollPane nested in the JInternalFrame), I might have problems scrolling and resizing when the JTable is larger than the JScrollPane.

Basically, if the frame is on my left monitor, but I have scrolled the table all the way to the right, a pop-up window appears on my right monitor.

I looked at the results of four different options: getMousePositions () for both the frame and the scroll pane, plus the getX and getXOnScreen () mouse events.

The only one that gave me the results I wanted was getMousePositions () for the frame. Everything else was offset by my own inner view of the world, which makes sense to me.

So, I think I'm saying, be careful when you get the coordinates of your mouse.

0
source

Source: https://habr.com/ru/post/1391554/


All Articles