[英]Display JCheckBox in JTable
I have a Jtable in which I want to add a JCheckbox in one column. 我有一个Jtable,我想在其中添加一个JCheckbox。 However, when I create a JCheckbox object, javax.swing.JCheckBox is being displayed in the column.Please refer to the image.
但是,当我创建JCheckbox对象时,该列中将显示javax.swing.JCheckBox。请参考图片。 Can you tell me how to amend that please?
你能告诉我如何修改吗? I have searched everywhere but cannot seem to find any solution for it.
我到处搜索,但似乎找不到任何解决方案。 Thank you.
谢谢。
TableModel
, that's not the responsibility of the TableModel
TableModel
,这不是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
DefaultTableModel
,您可以简单地用一堆布尔值填充列,这应该可以工作-测试之后,您将需要重写DefaultTableModel
的getColumnClass
方法(或任何TableModel
实现),并确保“复选框”列,则返回Boolean.class
See How to use tables for more details 有关更多详细信息,请参见如何使用表格 。
For example... 例如...
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);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.