在某本书上有以下的例子:
// FileModel.java
// A custom table model to display information on a directory of files
//
import javax.swing.table.*;
import java.util.Date;
import java.io.File;public class FileModel extends AbstractTableModel {  String titles[] = new String[] {
    "Directory?", "File Name", "Read?", "Write?", "Size", "Last Modified"
  };  Class types[] = new Class[] { 
    Boolean.class, String.class, Boolean.class, Boolean.class, 
    Number.class, Date.class
  };
      
  Object data[][];  public FileModel( ) { this("."); }  public FileModel(String dir) {
    File pwd = new File(dir);
    setFileStats(pwd);
  }  // Implement the methods of the TableModel interface we're interested
  // in. Only getRowCount( ), getColumnCount( ), and getValueAt( ) are
  // required. The other methods tailor the look of the table.
  public int getRowCount( ) { return data.length; }
  public int getColumnCount( ) { return titles.length; }
  public String getColumnName(int c) { return titles[c]; }
  public Class getColumnClass(int c) { return types[c]; }
  public Object getValueAt(int r, int c) { return data[r][c]; }  // Our own method for setting/changing the current directory
  // being displayed. This method fills the data set with file info
  // from the given directory. It also fires an update event, so this
  // method could also be called after the table is on display.
  public void setFileStats(File dir) {
    String files[] = dir.list( );
    data = new Object[files.length][titles.length];    for (int i=0; i < files.length; i++) {
      File tmp = new File(files[i]);
      data[i][0] = new Boolean(tmp.isDirectory( ));
      data[i][1] = tmp.getName( );
      data[i][2] = new Boolean(tmp.canRead( ));
      data[i][3] = new Boolean(tmp.canWrite( ));
      data[i][4] = new Long(tmp.length( ));
      data[i][5] = new Date(tmp.lastModified( ));
    }    // Just in case anyone's listening
    fireTableDataChanged( );
  }
}// FileTable.java
// A test frame for the custom table model
import java.awt.*;
import javax.swing.*;
import java.util.Date;
import java.io.File;public class FileTable extends JFrame {  public FileTable( ) {
    super("Custom TableModel Test");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);    FileModel fm = new FileModel( );
    JTable jt = new JTable(fm);
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jt.setColumnSelectionAllowed(true);    JScrollPane jsp = new JScrollPane(jt);
    getContentPane( ).add(jsp, BorderLayout.CENTER);
  }  public static void main(String args[]) {
    FileTable ft = new FileTable( );
    ft.setVisible(true);
  }
}请问如何才能使得Boolean类型列中显示的CheckBox框可以编辑呢?

解决方案 »

  1.   

    给你个JTable例子看看:import java.awt.*;
    import java.util.*;import javax.swing.*;
    import javax.swing.table.*;/**
     * JTable的例子
     */
    public class Test {
    private JFrame frame = null;private JTable table = null;private Table_Model model = null;private JScrollPane s_pan = null;public Test() {
    frame = new JFrame("Test");
    model = new Table_Model(20);
    table = new JTable(model);
    table.setBackground(Color.white);
    String[] age = { "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" };
    JComboBox com = new JComboBox(age);
    TableColumnModel tcm = table.getColumnModel();
    tcm.getColumn(2).setCellEditor(new DefaultCellEditor(com)); // 设置某列采用JComboBox组件model.addRow("宋江", true, "30");
    model.addRow("孙二娘", false, "21");
    model.addRow("武松", true, "24");s_pan = new JScrollPane(table);frame.getContentPane().add(s_pan, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true);model.addRow(2); // 在某处插入一空行
    table.updateUI(); // 刷新}public static void main(String args[]) {
    new Test();
    }}class Table_Model extends AbstractTableModel {
    private static final long serialVersionUID = -3094977414157589758L;private Vector content = null;private String[] title_name = { "姓名", "性别", "年龄" };public Table_Model() {
    content = new Vector();
    }public Table_Model(int count) {
    content = new Vector(count);
    }/**
     * 加入一空行
     * @param row 行号
     */
    public void addRow(int row) {
    Vector v = new Vector(3);
    v.add(0, null);
    v.add(1, null);
    v.add(2, null);
    content.add(row, v);
    }/**
     * 加入一行内容
     */
    public void addRow(String name, boolean sex, String age) {
    Vector v = new Vector(3);
    v.add(0, name);
    v.add(1, new Boolean(sex)); // JCheckBox是Boolean的默认显示组件,这里仅仅为了看效果,其实用JComboBox显示Sex更合适
    v.add(2, age); // 本列在前面已经设置成了JComboBox组件,这里随便输入什么字符串都没关系
    content.add(v);
    }public void removeRow(int row) {
    content.remove(row);
    }public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
    }public void setValueAt(Object value, int row, int col) {
    ((Vector) content.get(row)).remove(col);
    ((Vector) content.get(row)).add(col, value);
    this.fireTableCellUpdated(row, col);
    }public String getColumnName(int col) {
    return title_name[col];
    }public int getColumnCount() {
    return title_name.length;
    }public int getRowCount() {
    return content.size();
    }public Object getValueAt(int row, int col) {
    return ((Vector) content.get(row)).get(col);
    }public Class getColumnClass(int col) {
    return getValueAt(0, col).getClass();
    }}