TreeCellEditor and TreeCellRenderer

J

Jason Cavett

I'm a little confused about the differences between a CellEditor and a
CellRenderer (specifically the "Tree" version). From my
understanding, the renderer is HOW the icons are displayed and the
editor is how to edit the fields. It seems other people have this
same idea from my searches in Google.

If this is right, what I am confused about is why a
DefaultTreeCellEditor takes a DefaultTreeCellRenderer as part of its
constructor? Is this so icons can be rendered even when editing?

The reason I ask is I have created my own TreeCellEditor and my own
TreeCellRenderer by subclassing DefaultTreeCellEditor and
DefaultTreeCellRenderer, respectively. Within my renderer, I wrote
the logic so that the icons would be set to icons I wanted (based on
the current state of a node in the tree since I have different states
other than open/close). Within my editor I override
determineOffset(...) so that I can keep the current active Icon and
don't get the default folder/bullet icons. I also implement
CellEditorListener so I can tell when a person starts/stops editing
the tree node name.

Am I approaching this correctly? Thanks for any feedback.
 
V

volkanagun

I'm a little confused about the differences between a CellEditor and a
CellRenderer (specifically the "Tree" version). From my
understanding, the renderer is HOW the icons are displayed and the
editor is how to edit the fields. It seems other people have this
same idea from my searches in Google.

If this is right, what I am confused about is why a
DefaultTreeCellEditor takes a DefaultTreeCellRenderer as part of its
constructor? Is this so icons can be rendered even when editing?

The reason I ask is I have created my own TreeCellEditor and my own
TreeCellRenderer by subclassing DefaultTreeCellEditor and
DefaultTreeCellRenderer, respectively. Within my renderer, I wrote
the logic so that the icons would be set to icons I wanted (based on
the current state of a node in the tree since I have different states
other than open/close). Within my editor I override
determineOffset(...) so that I can keep the current active Icon and
don't get the default folder/bullet icons. I also implement
CellEditorListener so I can tell when a person starts/stops editing
the tree node name.

Am I approaching this correctly? Thanks for any feedback.

Hi, I am also confused with the idea but I have an opinion,
Actually the renderer part only for display, it takes your specific
(BookInfo)class variables and displayes them where you want, this info
comes into the JTree at the construction. And the editor, is the
reverse operation, when you edit some thing it recognize and fires
some events, at this part it uses "public Object getCellEditorValue
()" in order to create new instances from your inputs for example;


public Object getCellEditorValue ()
{
JTextField textField = renderer.getRenderer ();
OntologyNode node = new OntologyNode (); //MY INFO
node.setValue (textField.getText ());
node.setValue (textField.getText ());
return node;
}

so that when your editing finished it calls this method.

I think you work only for icons, but there can be done anything with
the idea, you can displaye you usercontrols in JTree and also edit
them.
 
R

Roedy Green

If this is right, what I am confused about is why a
DefaultTreeCellEditor takes a DefaultTreeCellRenderer as part of its
constructor? Is this so icons can be rendered even when editing?

Have a look at the code for TreeCellRenderer. It is an interface with
only one method. It returns a Component -- in other words a rendered
cell. Normally you recycle the same component over and over.

It has a simple job, something that looks like the cell, often just
selecting from a set of icons for leafs and nodes.


A TreeCellEditor is looks superficially to be identical. It too
produces a component to represent that node/leaf. However, that
component has to be smart enough to ALSO accept editing -- e.g.
keystrokes, an on-off switch, a JComboBox etc.

It would typically notify of changes by the attached
CellEditorListeners.

TreeCellRenderers get passed a hasFocus parameter , SO SOMEHOW the
JTree is tracking focus on a cell basis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top