Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Extending JTable

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) {

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
Posts: 25673
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
This is my favorite show. And this is my favorite tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic