首页 > 代码库 > Java Swing 开发之JTable中在添加组件(JCheckBox)
Java Swing 开发之JTable中在添加组件(JCheckBox)
首先是创建表格
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 = http://www.mamicode.com/{ i,2,new Boolean(false)/**这里就那个JCheckBox位置*/};>
将下面的类容复制粘贴到你正在想加组件的类下面
//~ 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最后效果:
Java Swing 开发之JTable中在添加组件(JCheckBox)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。