Mahbubur Rahman Khan
Mahbubur Rahman Khan

Reputation: 415

How to override a JTable methods which one already made

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...

Upvotes: 1

Views: 1615

Answers (1)

Mahbubur Rahman Khan
Mahbubur Rahman Khan

Reputation: 415

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();
        }

    });

}

Upvotes: 1

Related Questions