我想向已经初始化的 JTable 添加行。除了其他元素之外,我还有以下(相关)代码:
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
class sscce extends JFrame {
private static final long serialVersionUID = 1L; // Serial ID...
// Interface-Elemente erzeugen
Container content = getContentPane();
DefaultTableModel myAbstractTableModel = new DefaultTableModel () {
private static final long serialVersionUID = 1L; // whatever
public String[] columnNames = {"AuftragNr", "Datum & Uhrzeit", "Von", "Nach", "erledigt?"};
public Object[][] data = {{"156", "31.12.2012 - 10:39:31", "5/5", "205/39", new Boolean(false)}};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col != 4) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
};
JTable auftragTable = new JTable(myAbstractTableModel);
JScrollPane tableScrollPane = new JScrollPane(auftragTable);
JButton auftragAenderungSpeichern = new JButton("speichern");
public sscce() {
setTitle("Auftragsverwaltung");
setSize(700, 500);
setLocation(500, 200);
setLayout(null);
setResizable(false);
tableScrollPane.setBounds(50, 50, 500, 200);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
JFrame f = new sscce();
f.show();
}
}
每当我尝试运行程序时,我都会收到NullPointerException以及
at javax.swing.table.DefaultTableModel.setDataVector(Unknown Source)
at javax.swing.table.DefaultTableModel.<init>(Unknown Source)
at javax.swing.table.DefaultTableModel.<init>(Unknown Source)
at javax.swing.table.DefaultTableModel.<init>(Unknown Source)
对于以下代码行:
public int getRowCount() {
return data.length;
}
这是为什么?我的代码有什么问题?程序不应该能够“找到”数据吗?
编辑:第二种方法
我尝试使用 ArrayList 作为存储数据的地方......但是下面的评论中标记了“cols-rows-issue”......找不到数组值(因为我现在使用 ArrayList )。我该如何解决?
import java.awt.Container;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
class sscce extends JFrame {
private static final long serialVersionUID = 1L; // Serial ID...
Container content = getContentPane();
AbstractTableModel myAbstractTableModel = new AbstractTableModel () {
private static final long serialVersionUID = 1L; // whatever
private String[] columnNames = {"AuftragNr", "Datum & Uhrzeit", "Von", "Nach", "erledigt?"};
private ArrayList<Object> data = new ArrayList<Object>();
public void addRow(List rowData) {
data.add(rowData);
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
// can be solved via .size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
// no idea, how to solve that?!
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col == 4) {
return true;
}
return false;
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
// same issue here...
fireTableCellUpdated(row, col);
}
};
JTable auftragTable = new JTable(myAbstractTableModel);
JScrollPane tableScrollPane = new JScrollPane(auftragTable);
JButton auftragAenderungSpeichern = new JButton("speichern");
public sscce() {
setTitle("Auftragsverwaltung");
setSize(700, 500);
setLocation(500, 200);
setLayout(null);
setResizable(false);
tableScrollPane.setBounds(50, 50, 500, 200);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
JFrame f = new sscce();
f.show();
}
}
编辑:第三种方法
sscce 类:
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
class sscce extends JFrame {
private static final long serialVersionUID = 1L; // Serial ID...
Container content = getContentPane();
AbstractTableModel myAbstractTableModel = new AbstractTableModel() {
private static final long serialVersionUID = 1L; // whatever
private String[] columnNames = {"AuftragNr", "Datum & Uhrzeit", "Von", "Nach", "erledigt?"};
private ArrayList<DataStore> data = new ArrayList<DataStore>();
public void addRow(DataStore rowData) {
data.add(rowData);
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size(); // length
// can be solved via .size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
// change made here
DataStore rowElement = data.get(row);
Object value = rowElement.getItemOnPosition(col);
return value;
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col == 4) {
return true;
}
return false;
}
public void setValueAt(Object value, int row, int col) {
// change made here
DataStore rowElement = data.get(row);
rowElement.setItemOnPosition(col, value);
fireTableCellUpdated(row, col);
}
};
JTable auftragTable = new JTable(myAbstractTableModel);
JScrollPane tableScrollPane = new JScrollPane(auftragTable);
JButton auftragAenderungSpeichern = new JButton("speichern");
public sscce() {
setTitle("Auftragsverwaltung");
setSize(700, 500);
setLocation(500, 200);
setLayout(null);
setResizable(false);
tableScrollPane.setBounds(50, 50, 500, 200);
content.add(tableScrollPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
JFrame f = new sscce();
f.show();
}
}
数据存储类:
public class DataStore {
Integer auftragNr;
String datumUhrzeit;
String von;
String nach;
Boolean status;
public DataStore(Integer a, String b, String c, String d, Boolean e) {
auftragNr = a;
datumUhrzeit = b;
von = c;
nach = d;
status = e;
}
public Object getItemOnPosition(int pos) {
if(pos == 1) {
return (Integer) auftragNr;
}
if(pos == 2) {
return datumUhrzeit;
}
if(pos == 3) {
return von;
}
if(pos == 4) {
return nach;
}
if(pos == 5) {
return (Boolean) status;
}
return null;
}
public Object setItemOnPosition(int pos, Object newValue) {
if(pos == 1) {
auftragNr = (Integer) newValue;
}
if(pos == 2) {
datumUhrzeit = (String) newValue;
}
if(pos == 3) {
von = (String) newValue;
}
if(pos == 4) {
nach = (String) newValue;
}
if(pos == 5) {
status = (Boolean) newValue;
}
return null;
}
}