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