JTable 右键单击​​鼠标 ActionListener 没有 return 正确的行和列值

JTable right click mouse ActionListener doesn't return correct row and column values

我有这段代码可以检测对 JTable 的右键单击:

jt.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON3) {
            JTable target = (JTable)e.getSource();
            int row = target.getSelectedRow();
            int column = target.getSelectedColumn();
            System.out.println(row + column);
            System.out.println(column);
            System.out.println("right click!");
        }
    }
});

我需要系统的行值和列值来确定被单击的单元格的内容,以便我可以 运行 函数和东西,但是对于我当前的 table jt:

当我右键单击行中的任何单元格时,returns“右键单击!” (这是正确的)和行和列值的“-2 -1”。鉴于它们在table的不同列中,行值和列值应该是正数,列值应该是不同的。我不明白这里发生了什么导致这段代码不起作用。

注意:之前我有 if (e.getClickCount() == 2) { do stuff },然后将其更改为右键单击功能,并且工作正常 - 返回了正确的行和列。

右键单击 table 不会 select 任何东西。

如果我在 GUI 出现后立即右键单击 table 中的任意位置,就会发生这种情况。

而这是我左键单击第一行中间列后看到的内容,然后右键单击 table 中的 任意位置:

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

public class TableSelectionWithMouse {

    private JComponent ui = null;
    String[] headers = {"First Name", "Last Name", "Occupation"};
    String[][] data = {
        {"Yohan", "Jones", "Mouse Wrangler"},
        {"Lucy", "Yang", "Emeritus Professor of Phlogiston"}
    };
    JLabel output = new JLabel("Right click to do .. stuff");

    TableSelectionWithMouse() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        final JTable table = new JTable(data, headers);
        ui.add(new JScrollPane(table));
        ui.add(output, BorderLayout.PAGE_END);
        MouseListener mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println(".mouseClicked()" + e);
                if (e.getButton() == MouseEvent.BUTTON3) {
                    JTable target = (JTable) e.getSource();
                    int row = target.getSelectedRow();
                    int column = target.getSelectedColumn();
                    output.setText(String.format("Row: %1s Col: %1s", row, column));
                }
            }
        };
        table.addMouseListener(mouseListener);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                TableSelectionWithMouse o = new TableSelectionWithMouse();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}