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.
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.
public void set_tab_val(boolean x,int r,int c)
{
pref_table.setValueAt(true,r,c);
}
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.