Link to home
Start Free TrialLog in
Avatar of EdwardWong
EdwardWong

asked on

How to disable the Movement of JTable Column

JTable has a useful function that the User can move any Column as the like and JTable can reorganize it automatically. But for some reason,I need to disable this function. How can I do?

Now My method is:
  class CustomModel extends DefaultTableModel {
        public CustomModel(Object[][] data, Object[] columnNames) {
              super(data, columnNames);
        }
        public Class getColumnClass(int col) {
            // dataVector is a protected member of DefaultTableModel

              Vector v = (Vector)dataVector.elementAt(0);
              return v.elementAt(col).getClass();
        }
    public boolean isCellEditable(int row, int column) {
      if(column < 3)
        return false;
      else
        return true;
    }

  }
  CustomModel dataModel = new CustomModel(data,names);
  JTable tblData = new JTable(dataModel);

But when using TableColumnModelListener,
it report an error.

Thank you very much.
ASKER CERTIFIED SOLUTION
Avatar of sankars98
sankars98

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EdwardWong
EdwardWong

ASKER

> But when using TableColumnModelListener, it report an error.

What error are you getting and how do you use it?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I use
"table.getColumnModel().addColumnModelListener ( new tableColumnModelListener () {
.........}); "

in init() Function.
The java compiler reports an error, as below:
Anonymous class of method jbinit() should be declared abstract, it does not define method columnSelectionChanged(javax.swing.event.ListSelectionEvent) in Interface javax.swing.event.TableColumnModelListener.

Thank you for your help.

>Anonymous class of method jbinit() should be declared abstract, it does not define method columnSelectionChanged
>(javax.swing.event.ListSelectionEvent) in Interface javax.swing.event.TableColumnModelListener.

  It seems you have not implement the
  TableColumnModelListener() properly.

  You have to implement all the functions(5) in TableColumnModelListener when you try to implement this interface.

 table.getColumnModel().addColumnModelListener( new TableColumnModelListener(){

 public void columnAdded(TableColumnModelEvent e){}

 public void  columnMarginChanged(ChangeEvent e){}

 public void columnMoved(TableColumnModelEvent e){}

 public void  columnRemoved(TableColumnModelEvent e){}

 public void columnSelectionChanged(ListSelectionEvent e){}

});
 
Sankar S.

No, I implement all the functions(5) in tableColumnModelListener, As below:

table.getColumnModel().addColumnModelListener( new TableColumnModelListener(){

 public void columnAdded(TableColumnModelEvent e){}

 public void  columnMarginChanged(ChangeEvent e){}

 public void columnMoved(TableColumnModelEvent e){
table.getTableMode()...
^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

 public void  columnRemoved(TableColumnModelEvent e){}

 public void columnSelectionChanged(ListSelectionEvent e){}

});

I think if in the Implementation Java doesn't allow user to cite the table it self.