Two Laptop Bag
The moose likes Swing / AWT / SWT and the fly likes Generic 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 "Generic jtable" Watch "Generic jtable" New topic

Generic jtable

Gene Hilpert
Ranch Hand

Joined: May 22, 2002
Posts: 51

Hello, I'm working on a maintenance screen that has a JTabbedPane that has about 15 tabs. Each tab has a jTable. Each of these jtables contains a simple ID (int), value(String) and a description(String).These values are used in drop down lists in the other parts of my application. So one has statues, address types, etc. As you can imagine there is a lot of very similar code. So I thought of making class(BuildMyjTable) that extends the jTable so I can pass in the class of what I want to display, a list of objects that I what to display, and a listener. So I got to a point where I can build and display the jTable using reflection to get the table headings and values. But my problem is with the listener. I have a class(MyModelListener) that implements TableModelListener that will contain the tableChanged method. Then I use that to extend my listener(MyTableModleListener) that I pass into the buildMyjTable constructor. Everything works but I can't figure out how to call the methods in the MyTableModleListener when the tableChanged method fires in MyModelListener. The methods in MyTableModleListen are where the update,insert, delete to the database is done. I'm trying to clean up the code so I can post an example. Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2454
So I thought of making class(BuildMyjTable) that extends the jTable

Well, I doubt you should be extending JTable for this. You aren't adding any functionality to the table. The table is still displaying data from the TableModel.

A better approach is to use a generic TableModel. Maybe something like the Bean Table Model.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

I agree with Rob that you should create your own custom TableModel instead; you can extend DefaultTableModel if you want. The JTable is nothing more than just a graphical representation of the data stored in the TableModel. The ID, value etc should go with the data - not the view.

How To Ask Questions How To Answer Questions
Gene Hilpert
Ranch Hand

Joined: May 22, 2002
Posts: 51

Thanks Then I will redo it.
I agree. Here's the link:
subject: Generic jtable
It's not a secret anymore!