如Swing JTable中CellRenderer为JCheckBox,Cell中的数据是一个对象,对象有一个字段是boolean值,当对Cell操作时,如点击选定JCheckBox,则要把CellRenderer的选定转化为CellEditor中对象的字段的boolean值.要如何实现呢?事件在哪里响应呢?
要实现什么接口呢?!

解决方案 »

  1.   

    其实你写一个TableModel就可以了, 给你个例子import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;public class ExampleTableModel extends AbstractTableModel {
      String columnName[] = { "description", "data", "boolean values" };  Class columnType[] = { String.class, String.class, Boolean.class };  Object table[][] = { { "this is line", "data", new Boolean(true) },
          { "this is line", "data", new Boolean(false) },
          { "this is line", "data", new Boolean(true) },
          { "this is line", "data", new Boolean(false) },
          { "this is line", "data", new Boolean(true) },
          { "this is line", "data", new Boolean(false) },
          { "this is line", "data", new Boolean(true) },
          { "this is line", "data", new Boolean(false) },
          { "this is line", "data", new Boolean(true) } };  public int getColumnCount() {
        return table[0].length;
      }  public int getRowCount() {
        return table.length;
      }  public Object getValueAt(int r, int c) {
        return table[r][c];
      }  public String getColumnName(int column) {
        return columnName[column];
      }  public Class getColumnClass(int c) {
        return columnType[c];
      }  public boolean isCellEditable(int r, int c) {
        return false;
      }  public void setValueAt(Object aValue, int r, int c) {
      }
      public static void main(String[] arg){
        JFrame f = new JFrame();
        f.getContentPane().add(new JScrollPane(new JTable(new ExampleTableModel())));
        f.setSize(300,300);
        f.setVisible(true);
      }
    }
      

  2.   

    不明白!
    首先,我的意思是说如何操作Cell中的值.
    其实在所有Cell中的对象都不是一个Boolean,而是一个复杂的对象,这个对象有一个Boolean型的属性.只是通过对Boolean值的操作来决定这个对象是否被选中!
    如把Cell中的checkbox点为选中,既要表现出来为选中,且后台的CellEditor也要把相应的值的更改反映到对象中.
      

  3.   

    首先isCellEditable()必须为true吧!?
    算了,我把代码贴出来.
    现在我遇到的问题是,点击了之后,checkbox没显示出改变.
      

  4.   

    public class CheckBoxTable extends JTable implements TableModelListener{
    private Log log = LogFactory.getLog(CheckBoxTable.class);

    public CheckBoxTable(Object[][] array,String[]columnName){
    super(array,columnName);
    initCheckBoxTable();
    } public CheckBoxTable(){
    super();
    initCheckBoxTable();
    } private void initCheckBoxTable() {
    // TODO Auto-generated method stub this.setCellSelectionEnabled(true);
    this.setCellEditor(new CheckBoxTableCellEditor());
    this.setDefaultRenderer(Boolean.class, new CheckBoxRenderer());
    this.setUI(new BasicTableUI());
    this.setRowHeight(22);
    }

    public void tableChanged(TableModelEvent e) {
    int row = this.getEditingRow();
    int column = this.getEditingColumn();
    boolean selected = ((Equipment)getValueAt(row,column)).getBoolean();

    }
    //表现
    class CheckBoxRenderer extends JCheckBox implements TableCellRenderer{
    private JCheckBox checkBox = new JCheckBox();
    public Component getTableCellRendererComponent(JTable table, 
                                               Object value, boolean isSelected, 
                                               boolean hasFocus, int row, int column) {
    // TODO Auto-generated method stub
    if(null != value){
    // checkBox.setEnabled(true);
    checkBox.setText(((Equipment)value).toString()) ;
    checkBox.setSelected(((Equipment)value).getBoolean());
    // checkBox.setSelected(isSelected);
    }else{
    // checkBox.setEnabled(false);
    checkBox.setText("");
    }
    return checkBox;
    }
    }


    //CellEditor
    class CheckBoxTableCellEditor extends AbstractCellEditor implements TableCellEditor{
    Equipment equipment;
    JCheckBox checkBox ;
    public CheckBoxTableCellEditor(){
    checkBox = new JCheckBox();
    }
    public Component getTableCellEditorComponent(JTable table, Object value,
                                             boolean isSelected, int row, int column) {
    // TODO Auto-generated method stub
    equipment = (Equipment)value;
    // checkBox.setSelected(isSelected);
    equipment.setBoolean(checkBox.isSelected());
    checkBox.setText(equipment.toString()); return checkBox;
    } public void addCellEditorListener(CellEditorListener e) {
    // TODO Auto-generated method stub

    } public void cancelCellEditing() {
    // TODO Auto-generated method stub

    } public boolean isCellEditable(EventObject e) {
    // TODO Auto-generated method stub
    return true;
    } public void removeCellEditorListener(CellEditorListener arg0) {
    // TODO Auto-generated method stub

    } public boolean shouldSelectCell(EventObject e) {
    // TODO Auto-generated method stub
    return true;
    } public boolean stopCellEditing() {
    // TODO Auto-generated method stub
    return true;
    }
    public Object getCellEditorValue() {
    // TODO Auto-generated method stub
    //取得编辑完的值
    return equipment;
    }}}