java 使用 setValueAt() 设置 jtable 列的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15332585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set value of jtable column using setValueAt()
提问by user1748910
I am trying to set the value of a jtable column using setValueAt() in netbeans and it is not working. following is what i have set using 'customize code' option. The columns showing null are of type boolean ie they can be checked and unchecked. I want to read values from the database and set the column values accordingly.
我正在尝试在 netbeans 中使用 setValueAt() 设置 jtable 列的值,但它不起作用。以下是我使用“自定义代码”选项设置的内容。显示 null 的列是布尔类型,即它们可以被选中和取消选中。我想从数据库中读取值并相应地设置列值。
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];
}
});
As the frame containing the jtable(pref_table) is initialised,the column values are either set to true or false by calling the following function but it does not seem to work.
随着包含 jtable(pref_table) 的框架被初始化,通过调用以下函数将列值设置为 true 或 false,但它似乎不起作用。
public void set_tab_val(boolean x,int r,int c)
{
pref_table.setValueAt(true,r,c);
}
采纳答案by Amarnath
I have added a button to the frame and called the method which you wrote and it worked fine for me.
我在框架中添加了一个按钮并调用了您编写的方法,它对我来说效果很好。
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);
}
}