1

I have many JTable but they populate data by using another public class. I have passed the entire JTable by its constructor. ok works fine. Now for some reason, i need to override some function ... so i will do it before populate data....

@Override
public boolean getScrollableTracksViewportWidth() {
}
@Override
public void doLayout() {
}
@Override
public void columnMarginChanged(ChangeEvent e) {        
}

But my JTable object is already created which is already pass... i can't overide like this...

new JTable(1,5){
    @Override
    public void columnMarginChanged(ChangeEvent e) {        
    }
}

may be its so simple basic..i don't know how override a component object's basic functions which is already created...

  • 1
    Did you read the [JTable tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html) already? Instead of overriding methods that are used by `JTable` _internally_, you should write a [Table Model Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/tablemodellistener.html). – Mick Mnemonic Sep 18 '15 at 23:38
  • 1
    or a `TableColumnModelListener` - which will notify you of a `columnMarginChanged` event. Use something like: `table.getColumnModel().addColumnModelListener(...)` – camickr Sep 18 '15 at 23:43
  • thanks...i am now reading.......but if someone give me an example, it will be fine... – Mahbubur Rahman Khan Sep 19 '15 at 00:39
  • ok i implement columnMargin ... its work fine ... but where the getScrolableTrackViewortWidth...and doLayout .... @camickr – Mahbubur Rahman Khan Sep 19 '15 at 00:50
  • 1
    There are no listeners for those methods. I'm not sure why you are overriding them. Maybe you should be using the `setAutoResizeMode(...)` method of the table to control the layout. Otherwise, instead of using an annonymouse inner class, just create you own `CustomTable` that implements the methods you need to override. Then you can just use `new CustomTable(...)` when you need to create a new table. The question is why are your trying to recreate the table in the first place. If you need to change the data then just use the `setModel(...)` method of the table. – camickr Sep 19 '15 at 01:08
  • Yeh ..base on your voice...i am now understand... what kind of fool i am...thanks for easy explanation .... @camickr – Mahbubur Rahman Khan Sep 19 '15 at 01:23
  • For [example](http://stackoverflow.com/a/8260663/230513). – trashgod Sep 19 '15 at 05:46
  • I can't use the setModel(...) function after getting data. Because i need to use external library to populate my table and this library is untouchable. Another thing, i am not worried to set data. I worry about show up. However i am now solve this issue. – Mahbubur Rahman Khan Oct 18 '15 at 05:10

1 Answers1

1

I design a function...Now its working for my issue ....

 void getAutoResizeTable(final JTable table) {

    table.getColumnModel().addColumnModelListener(new TableColumnModelListener() {
        @Override
        public void columnSelectionChanged(ListSelectionEvent lse) {
        }

        @Override
        public void columnAdded(TableColumnModelEvent tcme) {
        }

        @Override
        public void columnRemoved(TableColumnModelEvent tcme) {
        }

        @Override
        public void columnMoved(TableColumnModelEvent tcme) {
        }

        @Override
        public void columnMarginChanged(ChangeEvent ce) {
            TableColumn resizingColumn = table.getTableHeader().getResizingColumn();
            if (resizingColumn != null) {
                resizingColumn.setPreferredWidth(resizingColumn.getWidth());
            }
            if (hasExcessWidth(table)) {
                table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
            } else {
                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            }

        }

        protected boolean hasExcessWidth(JTable table) {
            return table.getPreferredSize().width < table.getParent().getWidth();
        }

    });

}