aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Extending JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Extending JTable" Watch "Extending JTable" New topic
Author

Extending JTable

G. Trevize
Greenhorn

Joined: Aug 05, 2006
Posts: 2
I'm trying to extend the functionality of JTable (mostly as a way of self study rather than a real application). The main idea is that the "enhanced" table will have a hidden panel which the user can deploy to change many aspects of the table behavior (very much like the JDK demo).

I already acomplished this using a JSplitPane and putting the JTable on top of it and the Options panel on the bottom. No magic here, I just extended JTable and overide the constructors to create the JSplitPane and so.

Problem is that this is not a "drop-in" replacement for JTable, because program needs to be modified for this to work.

Normally you use JTable, something like this:

JTable table = JTable(data, headers);
JScrollPane scroll = JScrollPane(table);

If I use this with my extended component, obviously only the standard JTable gets drawn and all the extra (the splitPane and the options panel) are not drawn. The easy way was to add a method getPanel() that returned the JSplitPane instead of the JTable. so

JTable table = JTable(data, headers);
JScrollPane scroll = JScrollPane(table.getPanel());

works, but as I said I would like I to be completely transparent, meaning no need for a custom method for it to work.

What I've tried so far:

1. Override the paint and/or PaintComponent methods in the JTable, problem is that if I do

public void paintComponent(Graphics g) {
pnlOptions.paint(g);
super.paint(g)
}

There seems to be a lot of issues with this approach (maybe the whole idea is fundamentaly wrong), because JTable is a child of pnlOptions so it seem to cause some recurrency. I also tried a loop that gets all the components from pnlOptions, identifies the JTable and call super.paints(g). But in all the cases I get wrongly drawn components, and don't get the desired result.

2. Extend the BasicTableUI overriding the paint method. Now I can control the drawing of the JTable, but when I tried to add other JComponents like the JSplitPane, I run into similar issues as the first approach.

3. There is always the possibility of "manually" drawing al the requiered extras for the JTable and adding the corresponding event listeners, but this looks like and excessive task.

I also done a lot of search and found many examples on Custom Components and Custom Drawing, but none of them tried to "encapsulate" a Swing Component into another like I'm trying to do. I should have started saying that I've been self learning (in the time I can spare) Java only for a few months now, so maybe I'm looking at this issue in a completely wrong way.

Thanks for any suggestions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Why does your component need to be a JTable?

Why not just have it provide a getTable() method that returns the JTable part of the component?

(Composition rather than Inheritance.)
G. Trevize
Greenhorn

Joined: Aug 05, 2006
Posts: 2
There was no explicit need for it to be a JTable, and you are right these can be done in other ways, as you mentioned.

Actually I've done more or less the same, by implementing a getPanel() method that returns the panel for it to be included in the appropiated container.

I was merely trying to see if replacing an existing component could be achieved by extending the class, but at the same time being able to draw the component differently.

The main idea was that anyone using the Component should use it as an standard JTable without having to call an extra method like the getTable(). Again this was just an effort of trying to learn it this approach leaded somewhere, it seems clear now that despite the fact that this could be achieved, it looks like it's not worth the trouble it involves, because much simpler approachs can achieve similar (although not exactly the same) results.

Thanks for the reply
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Extending JTable