gpt4 book ai didi

java - JTable AbstractTableModel 未触发动态数据列表

转载 作者:行者123 更新时间:2023-12-01 18:28:10 25 4
gpt4 key购买 nike

我设法潜伏并找到一个可以替代的好类(class) JTable(Object[][] data, Object[] columnNames)RowTableModel(List<T> modelData, List<String> columnNames)相反,这很棒,因为它允许我直接从 Students.ser 文件动态添加我的学生数据。

不知过了多久,我还是没能解决一个奇怪的问题。该表正确显示了除其内部数据之外的所有内容。

<小时/>

这是当前结果: enter image description here

显然那些空行是我拥有的学生数量(在本例中为 5),但是没有打印任何数据。

我的想法是我需要为每一列分配一个特定的学生字段,例如。对于行名称,我分配 Student.getName()

<小时/>

如果您想查看RowTableModel

这是我使用过的已记录的类方法/构造函数:

构造函数

/**
* Constructs a <code>RowTableModel</code> with initial data and
* customized column names.
*
* Each item in the <code>modelData</code> List must also be a List Object
* containing items for each column of the row.
*
* Each column's name will be taken from the <code>columnNames</code>
* List and the number of colums is determined by thenumber of items
* in the <code>columnNames</code> List.
*
* Sub classes creating a model using this constructor must make sure
* to invoke the setRowClass() method.
*
* @param modelData the data of the table
* @param columnNames <code>List</code> containing the names
* of the new columns
*/
protected RowTableModel(List<T> modelData, List<String> columnNames)
{
setDataAndColumnNames(modelData, columnNames);
}

setDataAndColumnNames:

/**
* Reset the data and column names of the model.
*
* A fireTableStructureChanged event will be generated.
*
* @param modelData the data of the table
* @param columnNames <code>List</code> containing the names
* of the new columns
*/
protected void setDataAndColumnNames(List<T> modelData, List<String> columnNames)
{
this.modelData = modelData;
this.columnNames = columnNames;
columnClasses = new Class[getColumnCount()];
isColumnEditable = new Boolean[getColumnCount()];
fireTableStructureChanged();
}
<小时/>

getValueAt(int row, int col) - 工作代码!:

@Override
public Object getValueAt(int row, int col) {
Student myStudent = getRow(row);
switch(col){
case 1: return students.get(row).getTeacherId();
case 2: return students.get(row).getId();
case 3: return students.get(row).getName();
case 4: return students.get(row).getSurname();
case 5: return students.get(row).getEmail();
case 6: return students.get(row).getDateOfBirth();
case 7: return students.get(row).getTel();
case 8: return students.get(row).getCourse();
case 9: return students.get(row).isOOP();
case 10: return students.get(row).getYearOfMembership();
case 11: return students.get(row).hasConsent();
default: return null;
}
}

最佳答案

来自link你已经给出了:

The RowTableModel is an abstract class. It does not implement the getValueAt() or setValueAt() methods of the TableModel. This will allow you to extend the model to support row Objects of any type in the model. You will only need to extend this class to implement these methods for your particular row Object type. For a simple example of how this might be done see the JButtonTableModel example code found below.

如果未实现这些方法,JTable 将无法询问表数据模型。因此构造函数中传递的数据将不会被使用。

请参阅the Java Doc for TableModel .

关于java - JTable AbstractTableModel 未触发动态数据列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25306454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com