Results 1 to 2 of 2
- 07-21-2009, 11:34 AM #1Member
- Join Date
- Nov 2008
- Rep Power
- 07-26-2009, 05: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.
- By prashant in forum Java AppletsReplies: 0Last Post: 07-19-2009, 08:04 PM
- By makpandian in forum AWT / SwingReplies: 4Last Post: 04-15-2009, 09:48 PM
- By adeeb in forum AWT / SwingReplies: 12Last Post: 06-19-2008, 08:39 AM
- By adeeb in forum SWT / JFaceReplies: 0Last Post: 06-18-2008, 07:13 PM
- By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 11:15 AM