繁体   English   中英

通过右键单击使用JPopupMenu在JTable中进行行选择

[英]Row Selection in JTable with JPopupMenu via Right Mouse Click

下面的代码创建一个简单的JFrame ,其中包括一个带有JPopupMenu JTable 右键单击以选择单击的行并显示弹出窗口。

问题是:

当用户Maximize JFrame到屏幕并右键单击最后1-2-3-4-5或第6行时,由于弹出高度与屏幕高度相对应,因此Popup出现在错误的位置并选择错误的行。

我怎样才能做到这一点?

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();
            }
        });
    }

}

我认为这是您应该如何使用/创建jpopupmenu的一个很好的例子。 链接

看看SwingUtilities.convertPoint()doc

因此,请更正您的convertPoint()

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

我解决了问题。 首先感谢@clamp 这个问题。

- -解决了 - -

我从代码中删除了以下几行;

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);

并将此MouseListener添加到表中;

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);
                }
            }
});

感谢所有对此问题感兴趣的人。 也许此解决方案将来会对某人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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