码迷,mamicode.com
首页 > 编程语言 > 详细

Java Swing 开发之JTable中在添加组件(JCheckBox)

时间:2014-12-09 17:57:42      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   java   

首先是创建表格

JTable demoTable= new JTable(); 
DefaultTableModel dtmDemo = (DefaultTableModel) demoTable.getModel();
String[] tableHeads = { "序号","id","jCheckBox"};
dtmDemo.setColumnIdentifiers(tableHeads);


//这里的2指的是第几列,从0开始计数

demoTable.getColumnModel().getColumn(2).setCellEditor(new CheckBoxCellEditor());
demoTable.getColumnModel().getColumn(2).setCellRenderer(new CWCheckBoxRenderer());



//JTable中的数据

for(int i=0;i<10;i++){
 Object[] objdata = { i,2,new Boolean(false)/**这里就那个JCheckBox位置*/};
 dtmDemo.addRow(objdata);
}


将下面的类容复制粘贴到你正在想加组件的类下面

//~ Inner Classes ----------------------------------------------------------------------------------------------------

class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
  //~ Static fields/initializers -------------------------------------------------------------------------------------

  private static final long serialVersionUID = 1L;

  //~ Instance fields ------------------------------------------------------------------------------------------------

  protected JCheckBox checkBox;

  //~ Constructors ---------------------------------------------------------------------------------------------------

  public CheckBoxCellEditor() {
    checkBox = new JCheckBox();
    checkBox.setHorizontalAlignment(SwingConstants.CENTER);
    // checkBox.setBackground( Color.white);
  }

  //~ Methods --------------------------------------------------------------------------------------------------------

  @Override public Object getCellEditorValue() {
    return Boolean.valueOf(checkBox.isSelected());
  }

  //~ ----------------------------------------------------------------------------------------------------------------

  @Override public Component getTableCellEditorComponent(
    JTable  table,
    Object  value,
    boolean isSelected,
    int     row,
    int     column) {
    checkBox.setSelected(((Boolean) value).booleanValue());

    return checkBox;

  }
} // end class CheckBoxCellEditor

class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
  //~ Static fields/initializers -------------------------------------------------------------------------------------

  private static final long serialVersionUID = 1L;

  //~ Instance fields ------------------------------------------------------------------------------------------------

  Border border = new EmptyBorder(1, 2, 1, 2);

  //~ Constructors ---------------------------------------------------------------------------------------------------

  public CWCheckBoxRenderer() {
    super();
    setOpaque(true);
    setHorizontalAlignment(SwingConstants.CENTER);
  }

  //~ Methods --------------------------------------------------------------------------------------------------------

  @Override public Component getTableCellRendererComponent(
    JTable  table,
    Object  value,
    boolean isSelected,
    boolean hasFocus,
    int     row,
    int     column) {
    if (value instanceof Boolean) {
      setSelected(((Boolean) value).booleanValue());

      // setEnabled(table.isCellEditable(row, column));
      setForeground(table.getForeground());
      setBackground(table.getBackground());

    }

    return this;
  }
} // end class CWCheckBoxRenderer

 最后效果:

bubuko.com,布布扣

Java Swing 开发之JTable中在添加组件(JCheckBox)

标签:style   http   io   ar   color   os   sp   for   java   

原文地址:http://my.oschina.net/Thinkeryjgfn/blog/354213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!