通过鼠标右键单击

时间:2016-03-29 15:38:15

标签: java popup jtable point

在代码下方创建一个简单的JFrame,其中包含一个JTableJPopupMenu。右键单击选择单击的行并显示弹出窗口。

问题是:

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

}

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

查看SwingUtilities.convertPoint() doc

因此,请更正您的convertPoint()

$targetDom.html(...).scope is not a function

答案 2 :(得分:0)

我解决了这个问题。首先感谢this问题中的@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);
                }
            }
});

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