Results 1 to 2 of 2
Thread: JTable
- 07-21-2009, 10:34 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
- 07-26-2009, 04:59 AM #2
First, JTable is really designed as one piece of a framework, not as a ready to use component. It has several classes that provide pieces of functionality, all of which have default implementations. But the design really is intended for the developer to build each of the component classes according to their needs. If you intend to do serious Swing development, take the time to build up the various components from their interfaces, starting with TableModel.
As far as what displays each cell, look at the TableCellRenderer and TableCellEditor interfaces. Note that one class can implement both interfaces; you don't need separate classes. Also, note that just ONE instance of each interface is instantiated for the table. The table uses the ONE instance over and over, especially TableCellRenderer. Read the API carefully for things like repaint() and revalidate() that need to be stubbed out.
If you use one class for both the rendered and the editor, which makes things easier, remember you need one instance for the renderer and one for the editor.
You control what Component is returned to paint each cell. In the renderer, you should return the same small group of instances (one for each thing you want to display) over and over. Don't instantiate new instances on every call! TableCellRenderer is called hundreds of times a second when the mouse is moving over the table.
As far as the editor, it is only used to paint the one cell that is currently being edited. It is especially important to not keep instantiating whatever Component you return from the editor, since that will cause the values the user types in to go away! stopEditing() is your opportunity to save the value (or to return false, to force editing to continue). cancelEdiing() should throw the new value away.
A final note. The default renderer (not the editor) is a subclass of JLabel. The renderer, and the editor as well, if it is implemented correctly, can actually return *themselves* as the Component. I say this only to point out that you *don't* have to do that. Extend Object, instantiate whatever components (JLabel, JComboBox, etc.) you want to return, and keep them in instance fields. All the renderer or editor has to do is set the value.
I mentioned implementing both interfaces in one class. What I have done is to call the renderer's method from the editor's to set up the component; I then do additional setup needed for editing. I have a very complicated requirement that made me use JPanel as the return Component; I add other components to the JPanel as required. It causes a lot of problems, so I suggest avoiding that solution if you can. (I'm a contractor, and I was given a design and not asked for my input.)
I you're overwhelmed , welcome to Swing. It's not a toy, but it provides you low-level access to almost everything, so you can do almost anything. Take the time to dig into JTable, it's a very common component, and if you understand it, you will understand the others as well.
Good luck!
Similar Threads
-
Jtable
By prashant in forum Java AppletsReplies: 0Last Post: 07-19-2009, 07:04 PM -
Add a row in JTable
By makpandian in forum AWT / SwingReplies: 4Last Post: 04-15-2009, 08:48 PM -
Regarding JTable
By adeeb in forum AWT / SwingReplies: 12Last Post: 06-19-2008, 07:39 AM -
Regarding JTable
By adeeb in forum SWT / JFaceReplies: 0Last Post: 06-18-2008, 06:13 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks