0

I have written a program, which displays a table inside of a JScrollPane. The reason for this is that, if the table gets to long the user should be able to use the scrollbar. Further I want the user to be able to open a popup menu on right click to edit or delete data. At first I thought my implementation works as shown in this image:

enter image description here

Later I realized that if I use the scollbar something gets wrong with the mouseposition and the popup is at the wrong location as shown here:

enter image description here

The mouse is located at the highlighted entry.

Here is my code for this:

table.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()){
                rightClickPopUpOnTable(tablePanel.getComponentAt(e.getX(), e.getY()), e.getX(), e.getY());
                //rightClickPopUpOnTable(tablePanel.getComponentAt(e.getXOnScreen(), e.getYOnScreen()), e.getXOnScreen(), e.getYOnScreen());
            }

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            int r = table.rowAtPoint(e.getPoint());
            if (r >= 0 && r < table.getRowCount()) {
                table.setRowSelectionInterval(r, r);
            } else {
                table.clearSelection();
            }


            if (e.isPopupTrigger()){
                rightClickPopUpOnTable(tablePanel.getComponentAt(e.getX(), e.getY()), e.getX(), e.getY());
               //rightClickPopUpOnTable(tablePanel.getComponentAt(e.getXOnScreen(), e.getYOnScreen()), e.getXOnScreen(), e.getYOnScreen());
            }

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    });

 public void rightClickPopUpOnTable(Component component, int x, int y){
    JPopupMenu pop = new JPopupMenu();
    pop.setVisible(true);
    pop.setFocusable(false);

    JMenuItem item = new JMenuItem("Edit");
    item.addActionListener(new ActionListener() {
        @java.lang.Override
        public void actionPerformed(ActionEvent e) {
            handler.getData().showData(table.getValueAt(table.getSelectedRow(),0).toString());
        }
    });
    JMenuItem item2 = new JMenuItem("Delete");
    item2.addActionListener(new ActionListener() {
        @java.lang.Override
        public void actionPerformed(ActionEvent e) {
            int dialogResult = JOptionPane.showConfirmDialog (null, "Are you sure you want to delete "+table.getValueAt(table.getSelectedRow(), 0).toString()+"?","Delete Confirmation",JOptionPane.YES_NO_OPTION);
            if(dialogResult == JOptionPane.YES_OPTION){
                //DO STUFF
            }
        }
    });

    pop.add(item);
    pop.add(item2);
    pop.show(component, x, y);
}

I found a way to "fix" the problem partially by using e.getXOnScreen() as you can see in the code . Now the popup is displayed at the right location, but it won't highlight the items anymore AND it wont close as shown here:

enter image description here

I would be nice if someone knowns a solution for this.

Patrick P.
  • 25
  • 6
  • Unless you have a very particular reason for doing this manually, I'd simply use [`JTable#setComponentPopupMenu`](https://docs.oracle.com/javase/10/docs/api/javax/swing/JComponent.html#setComponentPopupMenu(javax.swing.JPopupMenu)) instead. I'm also unsure of why you want to use `tablePanel.getComponentAt`, this really isn't the appropriate method to use, I'd just be passing in a reference of the `JTable` itself. Also `pop.setVisible(true);` is probably a bad idea – MadProgrammer Feb 09 '19 at 21:59
  • A [`MouseListener` based example](https://stackoverflow.com/questions/22622973/jtable-copy-and-paste-using-clipboard-and-abstractaction/22623240#22623240); a [`ComponentPopupMenu` example](https://stackoverflow.com/questions/23505541/swing-mouse-click-event-for-jmenu-and-jpopupmenu/23509199#23509199) – MadProgrammer Feb 09 '19 at 22:05
  • The reason I am doing it that way is because I also have a JGraphX Component in my application where I am using a popup menu in graph nodes. I wanted to use kind of similar implementations for this. I will try the MouseListener based example tomorrow and will give feedback if this has solved my problem. – Patrick P. Feb 09 '19 at 22:25
  • 1
    Then avoid using `getComponentAt` and either use `MouseEvent#getComponent` or a reference of the component directly – MadProgrammer Feb 09 '19 at 22:29

0 Answers0