如何将mouseClicked事件添加到swing表?

1 java swing

我是Swing的一个新的非常绿色的用户.我设法使用java.sun教程中的示例创建了一个表类,并设法将数据动态加载到其中.我希望能够通过显示一个对话框来对行上的单击作出反应.如何添加将识别所选行号的事件处理程序?

主要功能代码:

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                //Create and set up the content pane.
                createAndShowGUI();
                //...
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

private static void createAndShowGUI() {
    //Create and set up the window.

    JFrame frame = new JFrame("Data Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up data of the content pane.
    TableClass mainTable = new TableClass(fh.getColNames(), fh.getTableContent());

    mainTable.setOpaque(true);
    frame.setContentPane(mainTable);


    //Display the window.
    frame.pack();
    frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Boz*_*sov 5

       table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
                    int selectedRowIndex = table.getSelectedRow();
                    //show your dialog with the selected row's contents
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)