使用NetBeans时向JTable添加数据

7
我该如何在使用Netbeans时向JTable添加数据。Netbeans在后台代码中是这样实现的:
jTable1 = new javax.swing.JTable();
jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null},
            {null, null},
            {null, null},
            {null, null}
        },
        new String [] {
            "Name", "Branch"
        }
    ) {
        boolean[] canEdit = new boolean [] {
            false, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    }); // THIS IS THE SNIPPET GENERATED BY NETBEANS
    //( I have already created a table using the drag and drop fetaure of netbeans and this is the back snippet generated)

2-D 对象数组和 String 数组具有局部访问权限,所以当程序运行到中间时,我不能使用它们进行填充。(在一些函数中) 就像上面的表格那样,我将在某些函数中添加名称和分支。但是我该怎么做呢?
请问有没有人可以告诉我一个方法,这样我就可以向 JTable 添加数据了?
4个回答

20
jTable1.getModel().setValueAt(value, row, column);

3

jTable1.getModel().setValueAt(value, row, column);

这行代码用于设置 JTable 中指定单元格的值。其中,value是需要设置的值,rowcolumn分别表示要设置的单元格的行和列。

1
请在您的答案中添加任何评论。仅有代码的答案并不具有价值,也不受欢迎。谢谢。 - trejder

1

你的问题对我来说不是很清楚,但是这里有关于JTable的基础教程,还有很多例子这里,这里有一个例子,展示如何在运行时将值添加到TableCell


0
try {
    pst = con.prepareStatement("select * from emp"); 
    ResultSet rs = pst.executeQuery();
    int i = 0; 
    if (rs.next()) { 
    String  uname = rs.getString("contact_id"); 
    String  email = rs.getString("first_name");
    String  pass = rs.getString("last_name");
    String cou = rs.getString("phone"); 
    model.addRow(new Object[]{uname, email, pass, cou});
    i++;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接