我正在编写一个数据库记录器程序.在用户单击新行之后,我想指向该行.这样,当用鼠标右键点击打开弹出菜单时,用户就可以知道是排在哪行了.

这是我的程序的图形用户界面:

https://i.stack.imgur.com/HvXK4.png

我try 了以下代码:

tbl_patika_list.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                Point point = e.getPoint();
                int clickedRow = tbl_patika_list.rowAtPoint(point);
                tbl_patika_list.clearSelection();
                tbl_patika_list.setRowSelectionInterval(clickedRow,clickedRow);
            }
        });

推荐答案

使用鼠标右键单击打开弹出菜单.

老方法是使用鼠标侦听器来显示弹出窗口.

较新的方法是使用setComponentPopup(...)方法:

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

public class TableRightClick extends JPanel implements ActionListener
{
    JPopupMenu popup;

    public TableRightClick()
    {
        popup = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem("Click Me");
        menuItem.addActionListener( this );
        popup.add( menuItem );

        JTable table = new JTable(10, 5);
        table.setComponentPopupMenu( popup );
        popup.addPopupMenuListener(new PopupMenuListener()
        {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e)
            {
                SwingUtilities.invokeLater(() ->
                {
                    Point point = SwingUtilities.convertPoint(popup, new Point(0, 0), table);
                    int currentRow = table.rowAtPoint(point);
                    int currentColumn = table.columnAtPoint(point);
                    table.changeSelection(currentRow, currentColumn, false, false);
                });
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {}
        });
/*
        //  When using a MouseListener to show popup menu
        table.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                if (e.isPopupTrigger())
                {
                    JTable source = (JTable)e.getSource();
                    int row = source.rowAtPoint( e.getPoint() );
                    int column = source.columnAtPoint( e.getPoint() );

                    source.changeSelection(row, column, false, false);

                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
*/

        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        add( new JScrollPane(table) );
    }

    public void actionPerformed(ActionEvent e)
    {
        Component c = (Component)e.getSource();
        JPopupMenu popup = (JPopupMenu)c.getParent();
        JTable table = (JTable)popup.getInvoker();
        System.out.println(table.getSelectedRow() + " : " + table.getSelectedColumn());
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Table Right Click");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TableRightClick());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}

Java相关问答推荐

获取拦截器内部的IP地址

我应该避免在Android中创建类并在运行时编译它们吗?

多个Java线程和TreeMap.put()的非预期行为

JUnit—如何模拟局部变量对象方法调用

如何在带有Micronaut的YAML中使用包含特殊字符的字符串作为键

不推荐使用的Environment.getExternalStorageDirectory().getAbsolutePath()返回的值不同于新的getExternalFilesDir(空)?

我可以在MacOS上使用什么Java函数来在适当的设备上以适当的音量播放适当的alert 声音?

如果按钮符合某些期望,如何修改它的文本?

try 使用Spring集成和MySQL实现发件箱模式时,锁定等待超时

如何在太阳系模拟器中添加月球?

在Spring Boot应用程序中,server.port=0的默认端口范围是多少?

Java创建带有扩展通配符的抽象处理器

X=x*0.90;产生有损转换误差.X*=0.90;不是.为什么?

如何根据配置动态创建N个bean

为什么相同的数据条码在视觉上看起来不同?

无法使用Open WebStart Java 8运行jnlp

JPA无手术同品种器械可能吗?

在WHILE()循环初始化部分中声明和初始化变量的Java语法?

Java编译器是否进行了持续的折叠优化,以及如何进行判断?

如何在更改分辨率时将鼠标坐标计算为世界坐标