java中JTable单元格中放复选框

网上查资料,这方面讲的不实很详细。感觉写下来也许有点意义。呵呵本人刚学不久可能体会不深。请高人前辈指点

需要定义2个类:CheckButtonEditor,CheckBoxRenderer

测试类:table

 CheckButtonEditor:代码:

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JTextField;

class CheckButtonEditor extends    DefaultCellEditor
implements ItemListener {
private JCheckBox button;

public CheckButtonEditor(JCheckBox  checkBox) {
super(checkBox);
}

public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (value==null) return null;
button = (JCheckBox)value;
button.addItemListener(this);
return (Component)value;
}

public Object getCellEditorValue() {
button.removeItemListener(this);
return button;
}

public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
}

CheckBoxRenderer类代码:

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

class   CheckBoxRenderer   implements   TableCellRenderer{

 public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
if (value==null) return null;
return (Component)value;

}

table(测试类代码)

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;


public class table implements MouseListener{
 JTable table=new JTable();
public table(){
 JFrame frame=new JFrame("sjh");
 frame.setLayout(null);
 
 table=this.gettable();
 table.addMouseListener(this);
 JScrollPane src=new JScrollPane(table);
 src.setBounds(0, 0, 400, 200);
 frame.setSize(new Dimension(400,200));
 frame.add(src);
 frame.setVisible(true);
}
public JTable gettable(){
 DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(
      new Object[][]{{new JCheckBox("111"),new JCheckBox("111"),new JCheckBox("111"),new JCheckBox("111"),new JCheckBox("111"),new JCheckBox("111")},
                     {new JCheckBox("222"),new JCheckBox("222"),new JCheckBox("222"),new JCheckBox("222"),new JCheckBox("222"),new JCheckBox("222")},
                     {new JCheckBox("333"),new JCheckBox("333"),new JCheckBox("333"),new JCheckBox("333"),new JCheckBox("333"),new JCheckBox("333")},
      },
      new Object[]{"选择","结果物","说明","类型","发包要求文档","操作"});

    JTable table = new JTable(dm){
     public void tableChanged(TableModelEvent e) {
            super.tableChanged(e);
            repaint();
          }
        };
        table.getColumn("选择").setCellEditor(new CheckButtonEditor(new JCheckBox ()));
        table.getColumn("结果物").setCellEditor(new CheckButtonEditor(new JCheckBox ()));
        table.getColumn("说明").setCellEditor(new CheckButtonEditor(new JCheckBox ()));
        table.getColumn("类型").setCellEditor(new CheckButtonEditor(new JCheckBox ()));
       
        table.getColumn("发包要求文档").setCellEditor(new CheckButtonEditor(new JCheckBox ()));
        table.getColumn("操作").setCellEditor(new CheckButtonEditor(new JCheckBox ()));
        table.getColumn("选择").setCellRenderer(new RadioButtonRenderer());
//        table.getColumn("选择").setCellRenderer(new   CheckBoxRenderer());  
       // table.setCellEditor(new CheckButtonEditor(new JCheckBox ()));
        table.getColumn("选择").setCellRenderer(new   CheckBoxRenderer());
        table.getColumn("结果物").setCellRenderer(new   CheckBoxRenderer());
        table.getColumn("说明").setCellRenderer(new   CheckBoxRenderer());
        table.getColumn("类型").setCellRenderer(new   CheckBoxRenderer());
        table.getColumn("发包要求文档").setCellRenderer(new   CheckBoxRenderer());
        table.getColumn("操作").setCellRenderer(new   CheckBoxRenderer());
       
 return table;
}
public static void main(String args[]){
 new table();
}
public void mouseClicked(MouseEvent arg0) {
 // TODO Auto-generated method stub

}
public void mouseEntered(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
}
public void mouseExited(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
}
public void mousePressed(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
}
public void mouseReleased(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
}
}
好了.呵呵.

我的邮箱是smhwell@163.com有空可以探讨啊。小弟很乐意。

  • 1
    点赞
  • 1
    评论
  • 1
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

评论 1 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

小宏

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值