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

时间:2015-02-19 14:14:21

标签: 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 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();
}

任何人都对我能做什么有任何想法?这有可能吗?

2 个答案:

答案 0 :(得分:0)

您可以使用mouseClicked Action Listener来执行此操作。

答案 1 :(得分:0)

您可以使用MouseListener,这是一个简单的例子:

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);
                }
            }
        };
    }

}