1

Having trouble finding information on the thing I'm after as I'm not sure what the process is called. As an example, check out the code below.

When making a GUI, the following is used (copy/paste from internet):

regionSelectionTable = new JTable()
{
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col)
    {
        //Do some stuff
        Component comp = super.prepareRenderer(renderer, row, col);
        JComponent jcomp = (JComponent)comp;

        if (comp == jcomp) 
        {
            jcomp.setToolTipText((String)getValueAt(row, col));
        }

        return comp;
    };
};

I'm not so interested in what happens inside the prepareRenderer() method; but rather, is the JTable object being given a method in the above code? If true:

  • what is this process called?
  • why isn't the method being passed as an argument to the constructor or via a setter method?

If this is not what is happening, please explain what is happening and what this process is called.

Stephen Clark
  • 145
  • 1
  • 7
  • 1
    No, not really, you are creating what is known as an [Anonymous Class](http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html), which, in this case, is overriding the `prepareRenderer` method – MadProgrammer May 26 '14 at 09:23

2 Answers2

3

No, not really, you are creating what is known as an Anonymous Class, which, in this case, is overriding the prepareRenderer method.

Technically, you could add new methods to the class, but unless you are calling them internally, you won't be able to access them externally, as from the outside, it's just a JTable

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • `you won't be able to access them externally, as from the outside, it's just a JTable` - [reason for my question](http://stackoverflow.com/q/16814512/714968), its feature, but with buggy history – mKorbel May 26 '14 at 10:39
2

You are overriding an existing method by creating a nameless class that extends JTable.

Jorge_B
  • 9,712
  • 2
  • 17
  • 22