如果单击外部可见JPopupMenu,则强制选择JTable行

时间:2016-10-26 22:55:29

标签: java swing jtable jpopupmenu

我已将Debug::show($this->QuizQuestion()->QuestionNumber)添加到JPopupMenu JTable。问题是,当setComponentPopupMenu打开/可见时,当我左键单击弹出菜单外的一行时,菜单会关闭,但行未被选中,所以我必须再次单击它以突出显示它。有没有办法解决这个问题?

修改

我添加了示例代码。

顺便说一下,此行为仅在Windows LaF中发生。我刚刚测试了它,看起来默认的Java LaF允许在JPopupMenu打开时左键单击行选择。

JPopupMenu

1 个答案:

答案 0 :(得分:2)

这是一个LAF问题。

当我使用默认的LAF时它适用于我,但是当我使用平台LAF时它不起作用,对我来说这是Windows。

Windows上的潜在解决方案是使用MouseListener来选择该行。请注意,代码已添加到mouseReleased事件中。由于某种原因,即使根据mousePressed表格是AWTEventListener事件的来源,该表也不会收到mousePressed事件。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TablePopupListener extends JPanel
{
    public TablePopupListener()
    {
        JTable table = new JTable(10, 5);
        add( new JScrollPane( table ) );

        JPopupMenu popup = new JPopupMenu();
        popup.add( new JMenuItem("Do Something1") );
        popup.add( new JMenuItem("Do Something2") );

        table.setComponentPopupMenu( popup );

        table.addMouseListener( new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                System.out.println("Pressed JTable");
            }

            public void mouseReleased(MouseEvent e)
            {
                System.out.println("Released JTable");

                int row = table.rowAtPoint( e.getPoint() );

                if (row != -1
                && !table.isRowSelected(row))
                {
                   table.setRowSelectionInterval(row, row);
                }
            }
        });
    }

    private static void createAndShowGUI()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception ex) { System.out.println(ex); }

        JFrame frame = new JFrame("TablePopupListener");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TablePopupListener());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );

        Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
        {
            public void eventDispatched(AWTEvent e)
            {
                String event = null;

                switch (e.getID())
                {
                    case MouseEvent.MOUSE_PRESSED:  event = "Pressed: " ; break;
                    case MouseEvent.MOUSE_RELEASED:  event = "Released: " ; break;
                    case MouseEvent.MOUSE_ENTERED:  event = "Entered: " ; break;
                    case MouseEvent.MOUSE_EXITED:  event = "Exited: " ; break;
                    default: event = null; break;
                }

                if (event != null)
                {
                    System.out.println();
                    System.out.println(event + e.getSource().getClass());
                }
            }
        },  AWTEvent.MOUSE_EVENT_MASK);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}