更新时间:2023-12-03 17:28:22
TableModel
,这不是 TableModel的责任
DefaultTableModel
,您可以简单地用一堆布尔填充列,这应该可行 - 测试后,您将需要覆盖 getColumnClass
DefaultTableModel
的方法(或任何 TableModel
实现)并确保对于复选框列,它返回 Boolean.class
TableModel
, that's not the responsibility of the TableModel
DefaultTableModel
, you can simply fill the column with a bunch of booleans and this should work - After testing, you will need to override the getColumnClass
method of the DefaultTableModel
(or what ever TableModel
implementation) and make sure that for the "check box" column, it returns Boolean.class
有关更多详细信息,请参见如何使用表格
See How to use tables for more details
例如......
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class TestCardLayout {
public static void main(String[] args) {
new TestCardLayout();
}
public TestCardLayout() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
Random rnd = new Random();
DefaultTableModel model = new DefaultTableModel(new Object[]{"Check boxes"}, 0) {
@Override
public Class<?> getColumnClass(int columnIndex) {
return Boolean.class;
}
};
for (int index = 0; index < 10; index++) {
model.addRow(new Object[]{rnd.nextBoolean()});
}
JTable table = new JTable(model);
final JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}