如何在JTable中添加JCheckBox?
麻烦给一个简单的例子,实现这样的界面。姓名:年龄:是否通过aa12(这里是个JCheckBox,可以打勾的)bb43(这里是个JCheckBox,可以打勾的)这些都在JT...
麻烦给一个简单的例子,实现这样的界面。
姓名: 年龄: 是否通过
aa 12 (这里是个JCheckBox,可以打勾的)
bb 43 (这里是个JCheckBox,可以打勾的)
这些都在JTable中显示!(最好有详细点的注释说明)谢谢了! 展开
姓名: 年龄: 是否通过
aa 12 (这里是个JCheckBox,可以打勾的)
bb 43 (这里是个JCheckBox,可以打勾的)
这些都在JTable中显示!(最好有详细点的注释说明)谢谢了! 展开
3个回答
展开全部
package test;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
@SuppressWarnings("serial")
public class TestJtable extends JPanel {
public TestJtable(){
init();
}
private void init() {
setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(createTable());
add(scrollPane);
}
private JTable createTable() {
JTable table = new JTable();
String[] tableHeads = new String[]{"姓名","年龄","是否通过"};
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.setColumnIdentifiers(tableHeads);
/*******添加的一些数据,也可以从数据库中提取数据*************/
dtm.addRow(new Object[]{"aa","12",new Boolean(true)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
/*****设置table的列模型****/
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(2).setCellEditor(new DefaultCellEditor(new JCheckBox()));
tcm.getColumn(2).setCellRenderer(new TestTableCellRenderer());
tcm.getColumn(2).setPreferredWidth(80);
tcm.getColumn(2).setWidth(80);
tcm.getColumn(2).setMaxWidth(80);
return table;
}
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setBounds(300,100,300,200);
jframe.setTitle("测试");
jframe.add(new TestJtable());
jframe.setVisible(true);
}
class TestTableCellRenderer extends JCheckBox implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Boolean b = (Boolean) value;
this.setSelected(b.booleanValue());
return this;
}
}
}
一直没有时间给你说,不好意思。今天下午有点空。有什么不明白的。可以问我。
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
@SuppressWarnings("serial")
public class TestJtable extends JPanel {
public TestJtable(){
init();
}
private void init() {
setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(createTable());
add(scrollPane);
}
private JTable createTable() {
JTable table = new JTable();
String[] tableHeads = new String[]{"姓名","年龄","是否通过"};
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.setColumnIdentifiers(tableHeads);
/*******添加的一些数据,也可以从数据库中提取数据*************/
dtm.addRow(new Object[]{"aa","12",new Boolean(true)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
dtm.addRow(new Object[]{"aa","12",new Boolean(false)});
dtm.addRow(new Object[]{"bb","34",new Boolean(false)});
/*****设置table的列模型****/
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(2).setCellEditor(new DefaultCellEditor(new JCheckBox()));
tcm.getColumn(2).setCellRenderer(new TestTableCellRenderer());
tcm.getColumn(2).setPreferredWidth(80);
tcm.getColumn(2).setWidth(80);
tcm.getColumn(2).setMaxWidth(80);
return table;
}
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setBounds(300,100,300,200);
jframe.setTitle("测试");
jframe.add(new TestJtable());
jframe.setVisible(true);
}
class TestTableCellRenderer extends JCheckBox implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Boolean b = (Boolean) value;
this.setSelected(b.booleanValue());
return this;
}
}
}
一直没有时间给你说,不好意思。今天下午有点空。有什么不明白的。可以问我。
推荐于2016-08-25 · 知道合伙人互联网行家
关注
展开全部
只需:①将要添加的CheckBox功能的单元格的数据类型设置为Boolean ②添加如下代码的前两句即可。
//设置第0列显示JCheckBox
TableColumnModel tcm = jTable1.getColumnModel();
tcm.getColumn(0).setCellEditor(new DefaultCellEditor(new JCheckBox()));
//添加标格监听事件
jTable1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 1){
int columnIndex = jTable1.columnAtPoint(e.getPoint()); //获取点击的列
int rowIndex = jTable1.rowAtPoint(e.getPoint()); //获取点击的行
if(columnIndex == 0) {//第0列时,执行代码
if(jTable1.getValueAt(rowIndex,columnIndex) == null){ //如果未初始化,则设置为false
jTable1.setValueAt(false, rowIndex, columnIndex);
}
if(((Boolean)jTable1.getValueAt(rowIndex,columnIndex)).booleanValue()){ //原来选中
jTable1.setValueAt(false, rowIndex, 0); //点击后,取消选中
}
else {//原来未选中
jTable1.setValueAt(true, rowIndex, 0);
}
}
}
}
});
//设置第0列显示JCheckBox
TableColumnModel tcm = jTable1.getColumnModel();
tcm.getColumn(0).setCellEditor(new DefaultCellEditor(new JCheckBox()));
//添加标格监听事件
jTable1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 1){
int columnIndex = jTable1.columnAtPoint(e.getPoint()); //获取点击的列
int rowIndex = jTable1.rowAtPoint(e.getPoint()); //获取点击的行
if(columnIndex == 0) {//第0列时,执行代码
if(jTable1.getValueAt(rowIndex,columnIndex) == null){ //如果未初始化,则设置为false
jTable1.setValueAt(false, rowIndex, columnIndex);
}
if(((Boolean)jTable1.getValueAt(rowIndex,columnIndex)).booleanValue()){ //原来选中
jTable1.setValueAt(false, rowIndex, 0); //点击后,取消选中
}
else {//原来未选中
jTable1.setValueAt(true, rowIndex, 0);
}
}
}
}
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不需要自己的celleditor这么麻烦吧。jtable自动支持Jcheckbox,只要覆盖tablemodel的getColumnClass返回一个boolean的class,jtable会自动画一个Jcheckbox给你,你的value是true还是false直接读table里那个cell的值就可以
追问
你说的就是我想要的那种方法!可是我不会实现,你能帮我写段代码吗?
参考资料: 百度一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询