I would like to save and display data from database in JTable, and tried to use defaultTableModel for that. However, I'm getting:
ArrayIndexOutOfBoundsException at com.mycompany.bazadanych.main.main(main.java:40) (line with ** **).
Is there a method to predefine count of columns/rows in JTable and simply pass data to them?
public static void main(String[] args) {
bazaDanych BazaDanych = new bazaDanych();
DefaultTableModel tableModel = new DefaultTableModel();
JTable table = new JTable(tableModel);
tableModel.setColumnCount(3);
try {
//BazaDanych.wstawDane("STUDENCI", "Kowalski", "Jan");
//BazaDanych.wstawDane("STUDENCI", "Wiśniewski", "Piotr");
//BazaDanych.wstawDane("STUDENCI", "Nowak", "Michał");
BazaDanych.usunDane("STUDENCI", "Nowak", "Michał");
} catch (SQLException ex) {
ex.printStackTrace();
}
List<Student> lista = BazaDanych.pobierzDane("STUDENCI");
BazaDanych.zamknijPolaczenie();
lista.forEach(s -> {
**table.setValueAt(s.getId(), s.getId(), 0);**
table.setValueAt(s.getNazwisko(), s.getId(), 1);
table.setValueAt(s.getImie(), s.getId(), 2);
//tableModel.addRow(new Object[] {s.getId(), s.getNazwisko(), s.getImie()});
System.out.println(s.getId() + " " + s.getNazwisko() + " " + s.getImie());
});
table.updateUI();
//stable.setModel(tableModel);
JFrame frame = new JFrame("Demo program for JFrame");
frame.setLayout(null);
frame.add(table);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
}
}