Java 如何使用JTable获取单元格单击的id?

Java 如何使用JTable获取单元格单击的id?,java,jtable,Java,Jtable,我正在使用JTable显示数据库中的数据。我想要发生的是,当单击一行时,它会打开另一个窗口 我的代码 public class JFrametest extends javax.swing.JFrame { private static Connection connection; private static Statement stmt; static { // standard code to open a connection and sta

我正在使用JTable显示数据库中的数据。我想要发生的是,当单击一行时,它会打开另一个窗口

我的代码

public class JFrametest extends javax.swing.JFrame {
      private static Connection connection;
    private static Statement stmt;

    static {
        // standard code to open a connection and statement to Java Derby database
        try {
            NetworkServerControl server = new NetworkServerControl();
            server.start(null);
            // Load JDBC driver
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            //Establish a connection
            String sourceURL = "jdbc:derby://localhost:1527/"
                    + new File("EmailsDB").getAbsolutePath() + ";";
            connection = DriverManager.getConnection(sourceURL, "student", "student");
            stmt = connection.createStatement();
        } // The following exceptions must be caught
        catch (ClassNotFoundException cnfe) {
            out.println(cnfe);
        } catch (SQLException sqle) {
            out.println(sqle);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
try {
     String query = "select * from messages";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs)); 
}catch (Exception e) {
    e.printStackTrace();
}
与数据库的连接

public class JFrametest extends javax.swing.JFrame {
      private static Connection connection;
    private static Statement stmt;

    static {
        // standard code to open a connection and statement to Java Derby database
        try {
            NetworkServerControl server = new NetworkServerControl();
            server.start(null);
            // Load JDBC driver
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            //Establish a connection
            String sourceURL = "jdbc:derby://localhost:1527/"
                    + new File("EmailsDB").getAbsolutePath() + ";";
            connection = DriverManager.getConnection(sourceURL, "student", "student");
            stmt = connection.createStatement();
        } // The following exceptions must be caught
        catch (ClassNotFoundException cnfe) {
            out.println(cnfe);
        } catch (SQLException sqle) {
            out.println(sqle);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
try {
     String query = "select * from messages";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs)); 
}catch (Exception e) {
    e.printStackTrace();
}
显示数据库中的数据

public class JFrametest extends javax.swing.JFrame {
      private static Connection connection;
    private static Statement stmt;

    static {
        // standard code to open a connection and statement to Java Derby database
        try {
            NetworkServerControl server = new NetworkServerControl();
            server.start(null);
            // Load JDBC driver
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            //Establish a connection
            String sourceURL = "jdbc:derby://localhost:1527/"
                    + new File("EmailsDB").getAbsolutePath() + ";";
            connection = DriverManager.getConnection(sourceURL, "student", "student");
            stmt = connection.createStatement();
        } // The following exceptions must be caught
        catch (ClassNotFoundException cnfe) {
            out.println(cnfe);
        } catch (SQLException sqle) {
            out.println(sqle);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
try {
     String query = "select * from messages";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs)); 
}catch (Exception e) {
    e.printStackTrace();
}

有人知道我能做什么吗?这可能吗?

您可以使用鼠标单击的操作侦听器来执行此操作。

您可以使用
鼠标侦听器来执行此操作,下面是一个简单的示例:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestFrame extends JFrame {

    public static void main(String... s) {
        new TestFrame();
    }

    private JTable t;

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        DefaultTableModel model = new DefaultTableModel(0,2);
        for(int i=0;i<10;i++){
            model.addRow(new Object[]{i,"other info "+i});
        }
        model.setColumnIdentifiers(new Object[]{"id","info"});
        t = new JTable(model);
        t.addMouseListener(getListener());
        add(new JScrollPane(t));
    }

    protected void showDialog(int rowAtPoint) {
        Object valueAt = t.getValueAt(rowAtPoint, 0);
        // other operations
        JDialog d = new JDialog();
        d.setTitle("id="+valueAt);
        d.setModal(true);
        d.setAlwaysOnTop(true);
        d.setLocationRelativeTo(null);
        d.pack();
        d.setVisible(true);
    }

    private MouseListener getListener() {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(e.getClickCount() >= 1){
                    int rowAtPoint = t.rowAtPoint(e.getPoint());
                    if(rowAtPoint != -1)
                        showDialog(rowAtPoint);
                }
            }
        };
    }

}
import java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入javax.swing.JDialog;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.table.DefaultTableModel;
公共类TestFrame扩展了JFrame{
公共静态无效主(字符串…s){
新的TestFrame();
}
专用JTT表;
公共测试框架(){
init();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
包装();
setLocationRelativeTo(空);
setVisible(真);
}
私有void init(){
DefaultTableModel=新的DefaultTableModel(0,2);
对于(int i=0;i=1){
int rowAtPoint=t.rowAtPoint(e.getPoint());
如果(rowAtPoint!=-1)
显示对话框(rowAtPoint);
}
}
};
}
}

您将使用什么代码来检索用户单击的行的id?对于id,您可以使用tablename.getSelectedRow()