Can someone help me understand the following construct? I am having trouble understanding if this is an initializer or an anonymous class. I am not familiar with this syntax.
JTable jt = new JTable(data, fields) **{
public TableCellRenderer getCellRenderer(int row, int column) {
// TODO Auto-generated method stub
return renderer;
}
};**
It creates an anonymous inner class which extends JTable, and overrides getCellRenderer method.
Long explanation:
your are creating a class that extends JTable without explicitly assign it a name instead of using standard class declaration:
public class ExtendedJTable extends JTable{}
The visibility of this class is limited to the class inside which it is defined and instantiated. It's quite useful for example when you need, like in the code you posted, to override a method (getCellRenderer()) of a particular class (JTable), for some purposes limited to the current class context.
This approach has some benefits and also some limitations. For a deeper discussion have a look at this article.
You're doing 2 things here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With