gpt4 book ai didi

java - JTable 中的列名未更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:35 27 4
gpt4 key购买 nike

我有一个JTable,我已经为它创建了一个表模型。但是在将列名传递给表模型时,它不会更新列名。谁能告诉我为什么?

class MyTableModelTwo extends AbstractTableModel {

private Object[][] data;
private String[] columnNames = {"Name", "ID Number", "CGPA"};

public MyTableModelTwo(Object[][] data) {
this.data = data;

}

@Override
public int getRowCount() {
return data.length;
}

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

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}

@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

@Override
public boolean isCellEditable(int rowIndes, int columnIndex) {
return false;
}

@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
data[rowIndex][columnIndex] = value;
fireTableCellUpdated(rowIndex, columnIndex);
}
}

class MyTableTwo extends JPanel implements TableModelListener {

private JTable table;
private Object[][] data;
private JTextField t;

public MyTableTwo() {
data = new Object[3][3];
t = new JTextField();
for (int i = 0; i < noElements; i++) {
data[i][0] = "Kaushik";
data[i][1] = "2008A3";
data[i][2] = "7.79";
}

MyTableModelTwo m = new MyTableModelTwo(data);
table = new JTable(m);

}

@Override
public void tableChanged(TableModelEvent e) {
int row = table.getRowCount();
double sum = 0, d = 0;
for (int i = 0; i < row; i++) {
double c = (Double) table.getValueAt(i, 16);
sum += c;
}
t.setText("" + sum);
t.setHorizontalAlignment(JTextField.RIGHT);
}
}

最佳答案

您需要覆盖 getColumnName(int index)在你的 TableModel 中。

@Override
public String getColumnName(int index) {
return columnNames[index];
}

关于java - JTable 中的列名未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7426560/

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