高手们
我在用AbstractTableModel,我有一个类是继承AbstractTableModel的,其中有一列是boolean类型的,按理来说应该显示checkbox,但是如果这列在第一列的时候就没问题,如果在点击查询之前,我先把这个列拖到其他位置,查询出来就变成false和true了,checkbox再也无法正常显示出来,大家帮帮忙啊,谢谢了。

解决方案 »

  1.   

    我重写了getColumnClass这个方法,但是因为这一列是可以拖动的,前人是这么写的
        if (col == 0)
          return Boolean.class;
        else
          return getValueAt(0, col).getClass();
    但是因为可以拖动,所以一旦位置变化,checkbox就无法正常显示了,大家帮帮忙啊,谢谢了
      

  2.   

    你能不能仔细描述一下你的操作过程。或者可以把代码贴出来。
    我认为你的问题就出在
    public Class<?> getColumnClass(int columnIndex) {}
    这个方法上面。
    但是,没有看到具体代码不知道你的操作流程步骤,不知道数据的实际类型,不敢妄言。还有你想要的效果,加入将第一列拖拽到了第三列,那么下次查询的时候就要将对应的数据列对换吗?时可以随意调整?
    默认的JTable对于表列的reorder操作并不会影响实际数据,实际行列的顺序。请你给出更多细节,仅凭上述描述,无法彻底诊断问题。
      

  3.   

    好吧
    我有一个类是继承AbstractTableModel的package sg.com.cet.escalade.uc.csa.ui;/**
     * <p>
     * Title: Escalade
     * </p>
     * <p>
     * Description: Taxi Dispatch and Bureau Service System
     * </p>
     * <p>
     * Copyright: Copyright (c) 2003
     * </p>
     * <p>
     * Company: CET
     * </p>
     *
     * @author : Sasikumar
     * @version 1.0
     */import java.util.Vector;import javax.swing.table.AbstractTableModel;public class SvcProviderTableModel
        extends AbstractTableModel {  public Vector colum;
      public Vector vrow;
      public SvcProviderTableModel() {
        colum = new Vector();
        vrow = new Vector();
        colum.addElement(" "); //0
        colum.addElement("title1"); //1
        colum.addElement("title2"); //2
        colum.addElement("title3"); //3
        colum.addElement("title4"); //4
        colum.addElement("title5"); //5
        colum.addElement("title6"); //6
        colum.addElement("title7"); //7
        colum.addElement("title8"); //8
        colum.addElement("title9"); //9
        colum.addElement("title10"); //10
        colum.addElement("title11"); //11
        colum.addElement("title12"); //12
        colum.addElement("title13"); //13
        colum.addElement("title14"); //14
        colum.addElement("title15"); //15
        colum.addElement("title16"); //16
      }  // new methods  public Vector getrowvalues() {
        // get all row values    return vrow;
      }  //  public int getColumnCount() {
        return colum.size();
      }  public int getRowCount() {
        return vrow.size();
      }  public String getColumnName(int col) {
        return colum.elementAt(col).toString();
      }  public Object getValueAt(int row, int col) {
        try {
          Vector rowData = (Vector) vrow.elementAt(row);
          if (rowData != null) {
            return rowData.elementAt(col);
          }
          return null;
        }
        catch (Exception e) {
          return null;
        }
      }  public void setValueAt(Object value, int row, int col) {
        // get the array for the proper row
        Vector v = (Vector) vrow.elementAt(row);
        // replace the boolean object that changed
        v.set(col, value);
      }  public boolean isCellEditable(int row, int col) {
        if (col == 0)
          return true;
        else
          return false;
      }  public Class getColumnClass(int col) {
        if (col == 0)
          return Boolean.class;
        else
          return getValueAt(0, col).getClass();
      }  public void addRow(Vector someVector) {
        vrow.addElement(someVector);
        fireTableDataChanged();
      }  public void removeRow() {
        int count = this.getRowCount();
        for (int i = 0; i < count; i++) {
          vrow.removeElementAt(0);
          fireTableDataChanged();
        }
      }  public void removeRow(int row) {
        vrow.removeElementAt(row);
        fireTableDataChanged();
      }}
      

  4.   

    晕。只给个model。数据到底什么样子,你有没有做其他的操作?查询之后的数据每一列都是什么类型?你如何向JTable加载的数据?之所以出现你所说的那种状况很大的可能就是因为将Boolean类型当作String来处理了,而调用了toString方法。我写的这段肯定实现不了你要的效果,我想。但是,我也确实不知道你要什么。import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Vector;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;public class JCheckBoxTable extends JFrame { private static final long serialVersionUID = 1L; // column names
    Vector<String> colName = new Vector<String>(); // data to display
    Vector<Vector<Object>> data = new Vector<Vector<Object>>(); private JTable table; public JCheckBoxTable() {
    super();
    initData(); Container c = getContentPane();
    c.setLayout(new BorderLayout()); table = new JTable(new CModel());
    JScrollPane jsp = new JScrollPane();
    jsp.getViewport().add(table); c.add(jsp); JButton button = new JButton("ok");
    c.add(button, BorderLayout.SOUTH);
    button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    table.setModel(new CModel());
    } }); setSize(800, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
    } private void initData() {
    for (int i = 0; i < 6; i++) {
    colName.add("column " + i);
    } for (int i = 0; i < 20; i++) {
    Vector<Object> record = new Vector<Object>();
    record.add(new Boolean(i % 2 == 0));
    record.add(new Boolean(i % 3 == 0));
    record.add(i + " 2");
    record.add(i + " 3");
    record.add(new Boolean(false));
    record.add("i" + i + "5"); data.add(record);
    }
    } class CModel extends AbstractTableModel { private static final long serialVersionUID = 1L; @Override
    public int getColumnCount() {
    return colName.size();
    } @Override
    public int getRowCount() {
    return data.size();
    } @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
    return data.get(rowIndex).get(columnIndex);
    } @Override
    public Class<?> getColumnClass(int columnIndex) {
    return data.get(0).get(columnIndex).getClass(); } @Override
    public String getColumnName(int column) {
    return colName.get(column);
    } @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
    return columnIndex == 0;
    } @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
    data.get(rowIndex).set(columnIndex, value);
    fireTableCellUpdated(rowIndex, columnIndex);
    }
    } public static void main(String[] args) {
    new JCheckBoxTable();
    }
    }