gpt4 book ai didi

java - 更改 AbstractTableModel 的列名称

转载 作者:行者123 更新时间:2023-12-01 17:16:16 24 4
gpt4 key购买 nike

是否可以动态更改 AbstractTableModel 列名称?

我正在尝试实现setColumnName(0, "Speed rpm")方法。

public class MyModel extends AbstractTableModel {

private String[] columnNames = {"Speed", "Pressure",
"Force"};
public ArrayList<Values> list;

public MyModel() {

list = new ArrayList<Values>();
}

public void setColumnName(int i, String name) {
columnNames[i,name];
}
@Override
public int getRowCount() {
return list.size();
}

@Override
public int getColumnCount() {
return columnNames.length;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

最佳答案

改变

public void setColumnName(int i, String name) {
columnNames[i,name];
}

public void setColumnName(int i, String name) {
columnNames[i] = name;
fireTableStructureChanged();
}

(始终)遵循@camickr 的好建议

Invoking the fireTableStructureChanged() method will cause all custom renderers/editors to be lost. You can use the table.setAutoCreateColumnsFromModel(..) method when you create the table to prevent this from happening

关于java - 更改 AbstractTableModel 的列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937856/

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