gpt4 book ai didi

java - 动态添加数据到 AbstractTableModel 的扩展类

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

我有一些数据从我的数据库加载并作为公共(public)静态列表存储在另一个类中,我无法访问 MyTableModel 类中的数据以在 jtable 中存储和查看它们...还有许多其他方法可以用我的数据填充表格,但它们没有给我列复选框...我应该做什么?

    class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"UserName","Admin","Blocked"};
private Object[Size][3] data;
//size is an variable thing witch i get it from db,uses as number of the row;
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 < 2) {
return false;
} else {
return true;
}
}

public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}

}

最佳答案

您应该避免使用object.getClass()来返回列的类类型。如果该值恰好是数据集中唯一的 null 值,会发生什么?

相反,您应该传回实际的列类...(注意如果没有您的实际数据,我不知道应该传递哪些值,所以这只是一个示例...)

public Class getColumnClass(int c) {
return c < 2 ? String.class : Boolean.class;
}

已更新

要使 JTable 显示 JCheckBox,表模型必须从模型 getColumnClass 方法返回 Boolean。 ..这是最简单的解决方案,不过,您可以简单地为特定列提供自定义单元格渲染器。

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class TestTable11 {

public static void main(String[] args) {
new TestTable11();
}

public TestTable11() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

TableModel model = new SimpleTableModel();

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new JTable(model)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class SimpleTableModel extends AbstractTableModel {

private List<Object[]> rows;

public SimpleTableModel() {
rows = new ArrayList<>(5);
rows.add(new Object[]{"Test1", "Test2", false});
rows.add(new Object[]{"Test3", "Test4", false});
rows.add(new Object[]{"Test5", "Test6", false});
rows.add(new Object[]{"Test7", "Test8", false});
rows.add(new Object[]{"Test9", "Test10", false});
rows.add(new Object[]{"Test11", "Test11", false});
}

@Override
public int getRowCount() {
return rows.size();
}

@Override
public int getColumnCount() {
return 3;
}

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

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 2;
}

@Override
public Class<?> getColumnClass(int columnIndex) {
return columnIndex < 2 ? String.class : Boolean.class;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 2 && aValue instanceof Boolean) {
rows.get(rowIndex)[columnIndex] = aValue;
}
}
}
}

关于java - 动态添加数据到 AbstractTableModel 的扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17313415/

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