I have JTable and I fill it with SQL data, here is my method:
private void updateTable() {
try {
String sql = "SELECT number as Numver, name as Available, type FROM available_t ORDER BY number asc";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
tableAvail.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
So far so good. The data shows correct. But, I want to put one more column with checkbox. When I try to create manually using wizard in Eclipse (button 'model') I can't add new column, maybe because the table and columns is create by method. So, How I can add new column, that will have checkboxes. Then after I select some checkbox - when I press OK button, to check which checkboxes are checked and update it in db in column TYPE with YES or NO.
Edit:*** Here is my code, I just want result true/false to be converted in checkboxes in column:
private JPanel contentPane;
private final JPanel panel = new JPanel();
private final JScrollPane scrollPane = new JScrollPane();
private final JTable table = new JTable();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableExample frame = new TableExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TableExample() {
initGUI();
}
private void initGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel.setBounds(31, 11, 238, 209);
contentPane.add(panel);
panel.setLayout(null);
scrollPane.setBounds(10, 11, 218, 187);
panel.add(scrollPane);
table.setModel(new DefaultTableModel(
new Object[][] {
{"1", "Test1", "20", "false"},
{"2", "Test2", "10", "false"},
{"3", "Test3", "20", "true"},
},
new String[] {
"Id", "Test", "Rate", "T/F"
}
));
scrollPane.setViewportView(table);
}