通过TableColumnModel添加和删除列不会影响表格的数据,仅影响哪些显示在表格中的列。

解决方案 »

  1.   

    我不可能插入列的时候,再从XML里面读一把吧?我现在想单一的只对table进行操作,如何实现呢?
      

  2.   

    You can implement your own tableModel.
      

  3.   

    需要通知你的tableModel内容更新
    fireTableRowsDeleted();
    fireTableDataChanged();
    fireTableRowsInserted();
      

  4.   

    setLayout(new BorderLayout());
        String[][] row = {{"111", "222"},{"333","444"}};
        String[] col = {"aaa", "bbb"};
        DefaultTableModel dtm = new DefaultTableModel(row, col);
        JTable table = new JTable(dtm);
        this.add(new JScrollPane(table), BorderLayout.CENTER);
        //添加两列
        dtm.addColumn("ccc");
        dtm.addColumn("ddd");
        //删除一列(“ccc”)
        table.getColumnModel().removeColumn(table.getColumnModel().getColumn(2));
      

  5.   

    那是因为你只是删除了ColumnModel中的数据,TableModel仍然保留原来的数据结构。但TableModel并没有提供这样的接口方法,你只有自己去扩展。
    好的方法是将TableModel只作为显示层,你对结构的任何变动都通过操作数据层来完成
    例如在删除一列数据时,需要删除row和col
        ((Vector)row.get(0)).removeElementAt(1);
        ((Vector)row.get(1)).removeElementAt(1);
        col.removeElementAt(1);
        dtm.setDataVector(row , col);
      

  6.   

    我现在不仅仅涉及每个单元格的数据,我每个单元格还有字体、字色、背景色等属性,要是这么一移动,不很麻烦吗?在显示层,怎么能让他插入的时候,别显示出以前删除的列啊,在我保存的XML串里,插入列后是不影响,最主要就是显示有问题
      

  7.   

    怎么会麻烦呢?你所需要进行增删改操作的是数据对象,而单元格的字体、字色、背景色等属性怎么来显示与你的数据结构是没有关系的举个简单的例子:
    当前为一个三行两列的表,你规定第二列(或者你规定列名为“***”)的显示为粗体,背景为蓝色,其他均为默认显示。
    然后,你删除数据结构中第二列数据,重新设置(tableModel.setDataVector(row , col);)
    tableModel会重排表信息,并按照你所规定的要求显示数据
      

  8.   

    tableModel里面有办法把删除的列彻底移除吗?
      

  9.   

    Give you an example. You can add as many colums as you can. public class Panel extends JPanel {
      private JTable table_ = new JTable();
      private ButtonPanel buttonPanel_ = new ButtonPanel();  public Panel() {
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.getViewport().add(table_);
        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel_, BorderLayout.SOUTH);
        setModel();
      }  private void setModel() {
        Vector colums = new Vector();
        colums.add("Colum1");
        colums.add("Colum2");
        MyTableModel tableModel = new MyTableModel(colums);
        table_.setModel(tableModel);
      }
      private void addCol(String newColName) {
        ( (MyTableModel) table_.getModel()).addCol(newColName);
      }  private void removeCol() {
        ( (MyTableModel) table_.getModel()).removeCol();
      }  private class ButtonPanel extends JPanel {
        private JButton addBtn_ = new JButton("Add Colum");
        private JButton removeBtn_ = new JButton("Remove Colum");    ButtonPanel() {
          this.setLayout(new FlowLayout());
          ActionListener listener = new BtnListener();
          this.add(addBtn_);
          this.add(removeBtn_);      addBtn_.addActionListener(listener);
          removeBtn_.addActionListener(listener);
        }    class BtnListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
            if (e.getSource() == addBtn_)
              addCol("Test");
            else if (e.getSource() == removeBtn_)
              removeCol();
          }
        }
      }  private class MyTableModel
          extends AbstractTableModel {
        Vector value_ = null;    public MyTableModel(Vector value) {
          value_ = value;
        }    public void refresh() {
          fireTableDataChanged();
        }    public int getColumnCount() {
          return value_.size();
        }    public int getRowCount() {
          return 0;
        }    public String getColumnName(int col) {
          return (String) value_.get(col);
        }    public Object getValueAt(int row, int col) {
          return null;
        }    public boolean isCellEditable(int row, int col) {
          return true;
        }    public void setValueAt(Object value, int row, int col) {
          
        }    public void removeCol() {
          int lastElement = value_.size()-1;
          if(lastElement < 0) return;
          value_.remove(lastElement);
          fireTableStructureChanged();
        }    public void addCol(String newColName) {
          value_.add(newColName);
          fireTableStructureChanged();
        }  }}