简体   繁体   中英

Row Selection in JTable with JPopupMenu via Right Mouse Click

Below code create a simple JFrame which including one JTable with JPopupMenu . Right Clicks select the clicked row and show the popup.

Problem Is:

When user Maximize the JFrame to screen and right click to last 1-2-3-4-5 or 6th row, Popup appear in wrong location and select wrong row because of popup height correspond to screen height.

How can i achieve this?

MCVE;

package popuptestapp;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.DefaultTableModel;



public class PopupTestApp {

    public static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {

        if (!(pane.getLayout() instanceof BorderLayout)) {
            pane.add(new JLabel("Container doesn't use BorderLayout!"));
            return;
        }

        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(
                    java.awt.ComponentOrientation.RIGHT_TO_LEFT);
        }


        JScrollPane jScrollPane = new JScrollPane();
        JTable table = new JTable();
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"", "", "", "", "", "", "", ""}
            },
            new String [] {
                "Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        DefaultTableModel model1 = (DefaultTableModel) table.getModel();
        model1.setRowCount(0);

        //Fill the table with rows
        for (Integer i = 0; i<=250; i++){
            model1.addRow(new String[]{"", "", "", "", "", "", "", ""});
        }

        jScrollPane.setViewportView(table);     
        pane.add(jScrollPane, BorderLayout.PAGE_END);

        //creating JPopupMenu
        JPopupMenu popupForTable = new JPopupMenu();
        JMenuItem menuItem1;
        JMenuItem menuItem2;
        JMenuItem menuItem3;
        JMenuItem menuItem4;
        JMenuItem menuItem5;
        JMenuItem menuItem6;
        JMenuItem menuItem7;
        JMenuItem menuItem8;
        JMenuItem menuItem9;
        JMenuItem menuItem10;

        popupForTable.add(menuItem1 = new JMenuItem ("menuItem1"));
        popupForTable.add(menuItem2 = new JMenuItem ("menuItem2"));
        popupForTable.add(menuItem3 = new JMenuItem ("menuItem3"));
        popupForTable.add(menuItem4 = new JMenuItem ("menuItem4"));
        popupForTable.add(menuItem5 = new JMenuItem ("menuItem5"));
        popupForTable.add(menuItem6 = new JMenuItem ("menuItem6"));
        popupForTable.add(menuItem7 = new JMenuItem ("menuItem7"));
        popupForTable.add(menuItem8 = new JMenuItem ("menuItem8"));
        popupForTable.add(menuItem9 = new JMenuItem ("menuItem9"));
        popupForTable.add(menuItem10 = new JMenuItem ("menuItem10"));

        popupForTable.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        Point mousePoint = new Point();
                        mousePoint = MouseInfo.getPointerInfo().getLocation();

                        int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupForTable, new Point(0, 0), table));

                        if (rowAtPoint > -1) {
                            table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
                        }


                    }
                });
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                // TODO Auto-generated method stub

            }

        });

        table.setComponentPopupMenu(popupForTable);

    }


    private static void createAndShowGUI() {

        //Create and set up the window.
        JFrame frame = new JFrame("BorderLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500, 500));

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());
        //Use the content pane's default BorderLayout. No need for
        //setLayout(new BorderLayout());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

I think this is a pretty good example for how you should use/create jpopupmenu. Link

Take a look at SwingUtilities.convertPoint() doc

So, correct your convertPoint()

int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(null, mousePoint, table));

I solve the problem. Firstly thanks to @clamp in this question.

---SOLVED---

I have removed below lines from code;

popupForTable.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        Point mousePoint = new Point();
                        mousePoint = MouseInfo.getPointerInfo().getLocation();

                        int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupForTable, new Point(0, 0), table));

                        if (rowAtPoint > -1) {
                            table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
                        }

                    }
                });
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                // TODO Auto-generated method stub

            }

        });

table.setComponentPopupMenu(popupForTable);

And added this MouseListener to table;

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

                int rowindex = table.getSelectedRow();
                if (rowindex < 0)
                    return;
                if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
                    JPopupMenu popup = popupForTable;
                    popup.show(e.getComponent(), e.getX(), e.getY());
                    table.setRowSelectionInterval(r, r);
                }
            }
});

Thanks all who interested with this question. Maybe this solution help someones in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM