如何使用 Class 在 Java 中构建 TableModel
How to use Class construct a TableModel in Java
我想用class构造一个TableModel
。我知道如何使用方法 return a TableModel
。
但是我想知道如何用class构造一个TableModel
。
到目前为止我失败了。 dataset
没有通过数据获取所有的值。所以 dataset
为空。同样 columnNamesCount
也为空。
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "this.columnNamesCount" is null
at MyTableModel.getColumnCount(castToModel.java:57)
at java.desktop/javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1297)
at java.desktop/javax.swing.JTable.tableChanged(JTable.java:4391)
at java.desktop/javax.swing.JTable.setModel(JTable.java:3693)
at java.desktop/javax.swing.JTable.<init>(JTable.java:647)
at java.desktop/javax.swing.JTable.<init>(JTable.java:588)
at castToModel.<init>(castToModel.java:14)
at Main.main(Main.java:54)
在while loop
之后数据存储了所有的值。
class MyTableModel extends AbstractTableModel {
public Object[][] dataset;
public int columnCount;
public String[] columnNames;
public String[] columnNamesCount;
String query = "select * from emp";
void MyTableModel() throws SQLException {
Statement stmt = Main.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
//PreparedStatement stmt = Main.connection.prepareStatement(query);
ResultSet resultSet = stmt.executeQuery(query);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
columnCount = resultSetMetaData.getColumnCount();
columnNames = new String[columnCount];
for (int ii = 1; ii <= columnCount; ii++) {
columnNames[ii-1] = resultSetMetaData.getColumnName(ii);
}
resultSet.last();
int rowCount = resultSet.getRow();
resultSet.beforeFirst();
Object[][] data = new Object[rowCount][columnCount];
int currentRow = 0;
while (resultSet.next()) {
for (int currentColumn = 1; currentColumn <= columnCount; currentColumn++) {
data[currentRow][currentColumn - 1] = resultSet.getObject(currentColumn);
}
currentRow++;
}
dataset = data;
columnNamesCount=columnNames;
}
@Override
public int getRowCount() {return dataset.length;}
@Override
public int getColumnCount() {return columnNamesCount.length;}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {return dataset[rowIndex][columnIndex];}
public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); }
public boolean isCellEditable(int row, int col) {return true;}
}
Edit1:我的 casttoModel 代码:
public class castToModel {
public castToModel(){
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new MyTableModel();
JTable table = new JTable(model);
frame.add(table);
frame.pack();
frame.setVisible(true);}
}
来自您问题中的代码:
void MyTableModel() throws SQLException {
这是不是构造函数。
这是一个构造函数:
public MyTableModel() throws SQLException {
java 编译器将 void MyTableModel()
视为没有 return 值且具有包访问权限的常规方法。这意味着 class MyTableModel
没有声明构造函数。在这种情况下,编译器将添加一个空的构造函数,如下所示:
public MyTableModel() {
super();
}
您需要删除单词 void
并将其替换为适当的 access modifier。
此外,在您问题的代码中,您从未初始化 columnNamesCount
。因此它为空,因此方法 getColumnCount()
抛出 NullPointerException
。我觉得方法getColumnCount()
应该是这样的:
@Override
public int getColumnCount() {
return columnNames.length;
}
我想用class构造一个TableModel
。我知道如何使用方法 return a TableModel
。
但是我想知道如何用class构造一个TableModel
。
到目前为止我失败了。 dataset
没有通过数据获取所有的值。所以 dataset
为空。同样 columnNamesCount
也为空。
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "this.columnNamesCount" is null
at MyTableModel.getColumnCount(castToModel.java:57)
at java.desktop/javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1297)
at java.desktop/javax.swing.JTable.tableChanged(JTable.java:4391)
at java.desktop/javax.swing.JTable.setModel(JTable.java:3693)
at java.desktop/javax.swing.JTable.<init>(JTable.java:647)
at java.desktop/javax.swing.JTable.<init>(JTable.java:588)
at castToModel.<init>(castToModel.java:14)
at Main.main(Main.java:54)
在while loop
之后数据存储了所有的值。
class MyTableModel extends AbstractTableModel {
public Object[][] dataset;
public int columnCount;
public String[] columnNames;
public String[] columnNamesCount;
String query = "select * from emp";
void MyTableModel() throws SQLException {
Statement stmt = Main.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
//PreparedStatement stmt = Main.connection.prepareStatement(query);
ResultSet resultSet = stmt.executeQuery(query);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
columnCount = resultSetMetaData.getColumnCount();
columnNames = new String[columnCount];
for (int ii = 1; ii <= columnCount; ii++) {
columnNames[ii-1] = resultSetMetaData.getColumnName(ii);
}
resultSet.last();
int rowCount = resultSet.getRow();
resultSet.beforeFirst();
Object[][] data = new Object[rowCount][columnCount];
int currentRow = 0;
while (resultSet.next()) {
for (int currentColumn = 1; currentColumn <= columnCount; currentColumn++) {
data[currentRow][currentColumn - 1] = resultSet.getObject(currentColumn);
}
currentRow++;
}
dataset = data;
columnNamesCount=columnNames;
}
@Override
public int getRowCount() {return dataset.length;}
@Override
public int getColumnCount() {return columnNamesCount.length;}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {return dataset[rowIndex][columnIndex];}
public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); }
public boolean isCellEditable(int row, int col) {return true;}
}
Edit1:我的 casttoModel 代码:
public class castToModel {
public castToModel(){
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new MyTableModel();
JTable table = new JTable(model);
frame.add(table);
frame.pack();
frame.setVisible(true);}
}
来自您问题中的代码:
void MyTableModel() throws SQLException {
这是不是构造函数。
这是一个构造函数:
public MyTableModel() throws SQLException {
java 编译器将 void MyTableModel()
视为没有 return 值且具有包访问权限的常规方法。这意味着 class MyTableModel
没有声明构造函数。在这种情况下,编译器将添加一个空的构造函数,如下所示:
public MyTableModel() {
super();
}
您需要删除单词 void
并将其替换为适当的 access modifier。
此外,在您问题的代码中,您从未初始化 columnNamesCount
。因此它为空,因此方法 getColumnCount()
抛出 NullPointerException
。我觉得方法getColumnCount()
应该是这样的:
@Override
public int getColumnCount() {
return columnNames.length;
}