1

I'm using MVC in my project.

I want to separate the creation of the object from the Override.

How can I do it?

table = new JTable() {
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component component = super.prepareRenderer(renderer, row, column);
            int rendererWidth = component.getPreferredSize().width;
            TableColumn tableColumn = getColumnModel().getColumn(column);
            tableColumn.setPreferredWidth(
                    Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
            return component;
        }
    };
ALUFTW
  • 1,914
  • 13
  • 24
max barcon
  • 23
  • 4
  • You may look into Decorator pattern, which allows adding/changing the functionality dynamically to existing objects. See: [Decorator Pattern Design](https://stackoverflow.com/questions/37521842/decorator-pattern-design) – Nowhere Man Feb 14 '21 at 09:37
  • Why do you wish to do this? @Override has no overhead; it's just there to give a compiler error if the specified method isn't overriding a superclass method. – NomadMaker Feb 14 '21 at 10:01

1 Answers1

2

Follow these steps:

  1. Create a class which will extend JTable and override the method.

  2. Create the instance like this:

    JTable table = new ClassWhichExtendsJTable();
    
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Dragos Ionut
  • 257
  • 1
  • 7