我试图在netbeans中使用setValueAt()设置jtable列的值,但它不起作用。以下是我使用"自定义代码"选项设置的内容。显示为空的列是布尔类型的,即可以选中或不选中它们。我想从数据库中读取值,并相应地设置列值。
pref_table = new javax.swing.JTable();
pref_table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"MONDAY", null, null, null, null},
{"TUESDAY", null, null, null, null},
{"WEDNESDAY", null, null, null, null},
{"THURSDAY", null, null, null, null},
{"FRIDAY", null, null, null, null},
{"SATURDAY", null, null, null, null}
},
new String [] {
"DAY", "9 A.M-11 A.M", "11 A.M-1 P.M", "1 P.M-3 P.M", "3 P.M-5 P.M"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
当包含jtable(pref_table)的框架被初始化时,通过调用以下函数将列值设置为true或false,但它似乎不起作用。
public void set_tab_val(boolean x,int r,int c)
{
pref_table.setValueAt(true,r,c);
}
我已经在框架中添加了一个按钮,并调用了您编写的方法,它对我来说工作得很好。
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableSetEx
{
static JTable pref_table;
public static void main(String[] args) {
pref_table = new javax.swing.JTable();
pref_table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"MONDAY", null, null, null, null},
{"TUESDAY", null, null, null, null},
{"WEDNESDAY", null, null, null, null},
{"THURSDAY", null, null, null, null},
{"FRIDAY", null, null, null, null},
{"SATURDAY", null, null, null, null}
},
new String [] {
"DAY", "9 A.M-11 A.M", "11 A.M-1 P.M", "1 P.M-3 P.M", "3 P.M-5 P.M"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
JButton button = new JButton("Click");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Some hardceded cell.
set_tab_val(true,2,3);
}
});
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(pref_table), BorderLayout.NORTH);
frame.add(button, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void set_tab_val(boolean x,int r,int c)
{
pref_table.setValueAt(true,r,c);
}
}