Article View: borland.public.jbuilder.java.swing
Article #9093Re: Rendering
From: John P. McGrath
Date: Tue, 23 May 2000 00:00
Date: Tue, 23 May 2000 00:00
67 lines
3905 bytes
3905 bytes
On Tue, 23 May 2000 12:23:44 -0400, Shabber Abbas wrote: > I am wanting to put some buttons on each of my tree nodes , > I believe to customize this I have made a a TreeNode class > extending DefaultMutableTreeNode, a Tree class, and a > treeCellRendererClass, however now i am confused. Some > references say to implement TreeCellRenderer and implement > the getCellRendererComponent Method, however I am not sure > how to accomplish that also some books said to extend > JComponent , and override the paint method, even this is > confusing. A CellRenderer is an object that will provide a component that can be used to render a cell. When the JTree needs to render the cell, it passes information about the cell to the CellRenderer, and asks it to return a Component that can do the rendering. The CellRenderer typically has one reusable Component which it configures and returns to the JTree. Then the JTree "places" that Component in the cell area and asks it to paint itself. After painting, the JTree forgets about the component. When your books talk about extending JComponent, they may be talking about the component that is returned by the CellRenderer. I have seen some CellRenderers written to extend JComponent or JLabel, and then return "this" when asked for the cell renderer component. I would not recommend coding this way, but you may run into it. Writing your own paint() method is not always necessary. You can often instantiate a standard component such as a JLabel to do the rendering. The reason for this CellRenderer design is efficiency. If each cell was an actual component, you could end up with an awfully large number of components. Imagine a JTable with 100 rows and 100 columns. With an individual component for each cell, you would need 10,000 cell components. This way, you just need a few. In addition to CellRenderers there are also CellEditors. When the user clicks on a cell (or double clicks -- a configurable option), the JTree uses a similar mechanism. It asks the CellEditor for an editor component, places that component in the cell position, gives it focus, and then lets it do the editing. When the user tabs to another field or mouse clicks elsewhere, the JTree updates the cell value and gets rid of the editor component. I am not sure what is involved in creating buttons in the cells, but I am pretty sure that a simple CellEditor will not do it. If you think about it, there is no component in the cell except when the JTree is actually painting the cell. So there is no component to receive the events. You could create listeners that listened for clicks on the JTree, and have them invoke the appropriate action on the mouse click. But this would not be using the normal JButton logic, so you would have to implement the button functionality yourself. With just a simple listener on the JTree, the "button" would not react as you would probably want, looking pressed when you press the mouse button, returning to raised when you release the mouse, etc. My gut feeling is that the best approach would probably be to create a CellEditor based on a JButton. But it may turn out that the user has to press the button twice to activate it with this approach -- one to activate the cell editor, and another to trigger the button. -- John P. McGrath <JPMcGrath@fast.net> <jpm@nscs.fast.net>
Technical Details
Message-ID:
<MPG.13951ac42411316a98994b@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<392AB08F.7E8F162@codetech.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,8cfb10d94f0312a8 X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: Rendering Date: 2000/05/23 Message-ID: <MPG.13951ac42411316a98994b@forums.borland.com> X-Deja-AN: 626561772 References: <392AB08F.7E8F162@codetech.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing