“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:1>;=0 defaultTableModel Java
我想在JTable中保存和显示数据库中的数据,并尝试使用defaultTableModel。然而,我得到:“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:1>;=0 defaultTableModel Java,java,swing,jtable,indexoutofboundsexception,defaulttablemodel,Java,Swing,Jtable,Indexoutofboundsexception,Defaulttablemodel,我想在JTable中保存和显示数据库中的数据,并尝试使用defaultTableModel。然而,我得到: ArrayIndexOutOfBoundsException at com.mycompany.bazadanych.main.main(main.java:40) (line with ** **). 是否有一种方法可以预定义JTable中的列/行计数,并将数据传递给它们 public static void main(String[] args) { bazaDanych
ArrayIndexOutOfBoundsException at com.mycompany.bazadanych.main.main(main.java:40) (line with ** **).
是否有一种方法可以预定义JTable中的列/行计数,并将数据传递给它们
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();
}
}
publicstaticvoidmain(字符串[]args){
bazaDanych bazaDanych=新的bazaDanych();
DefaultTableModel tableModel=新的DefaultTableModel();
JTable table=新的JTable(tableModel);
tableModel.setColumnCount(3);
试一试{
//BazaDanych.wstawDane(“STUDENCI”、“Kowalski”、“Jan”);
//BazaDanych.wstawDane(“STUDENCI”、“Wiśniewski”、“Piotr”);
//BazaDanych.wstawDane(“STUDENCI”、“Nowak”、“Michał”);
BazaDanych.Usundan(“STUDENCI”、“Nowak”、“Michał”);
}catch(SQLException-ex){
例如printStackTrace();
}
列表a=BazaDanych.pobierzDane(“研究”);
BazaDanych.zamknijPolaczenie();
lista.forEach(s->{
**table.setValueAt(s.getId(),s.getId(),0)**
表.setValueAt(s.getNazwisko(),s.getId(),1);
表.setValueAt(s.getImie(),s.getId(),2);
//addRow(新对象[]{s.getId(),s.getNazwisko(),s.getImie()});
System.out.println(s.getId()+“”+s.getNazwisko()+“”+s.getImie());
});
table.updateUI();
//stable.setModel(tableModel);
JFrame=新JFrame(“JFrame演示程序”);
frame.setLayout(空);
框架。添加(表);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(空);
frame.pack();
}
}
是否有一种方法可以预定义JTable中的列/行计数,并将数据传递给它们
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();
}
}
您如何知道表的预定义大小
您的数据包含在学生列表中。更好的办法是:
StudentTableModel
。那么就不需要forEach循环了。您只需使用列表创建模型。有关如何创建自定义模型的逐步示例,请参见
DefaultTableModel
的addRow(…)
方法,如代码中所示。这意味着您需要去掉setValueAt(…)
语句