Adding multiple row editor to TreeCellEditor

Hey all,

I need to have a text field, check box and a combo box all as part of an inline editor for a tree node. When the user hits NEW to insert a new tree node, or edit an existing one, I want the editor to appear with two rows. The top row is the text field and check box. The second row is the combo box.

Is this possible? I just tried it and I see my combo box appear, but it appears the height of the editing area is fixed, so only my combo box shows up and the text field is no longer visible?

Here is my code:

publicclass AdminTreeCellEditorextends DefaultTreeCellEditor

{

privatefinal JCheckBox activeBox =new JCheckBox("Active");

privatefinal JComboBox revenueCategoryBox =new JComboBox();

privatefinal JLabel iconLabel =new JLabel();

privatefinal JPanel editorPanel =new JPanel(new BorderLayout());

private Icon editIcon =null;

public AdminTreeCellEditor(JTree tree)

{

super(tree, (DefaultTreeCellRenderer) tree.getCellRenderer());

editorPanel.add(activeBox, BorderLayout.EAST);

editorPanel.setOpaque(false);

activeBox.setOpaque(false);

editIcon =new ImageIcon(getClass().getResource("/edit.gif"));

}

public Component getTreeCellEditorComponent(JTree tree, Object value,boolean isSelected,boolean expanded,boolean leaf,int row)

{

Component c = super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);

SortableTreeNode dmtn = (SortableTreeNode)value;

if (dmtn.getUserObject()instanceof CorporateItem)

{

CorporateItem ci = (CorporateItem)dmtn.getUserObject();

activeBox.setSelected(ci.isActive());

}

else

{

activeBox.setSelected(true);

}

editorPanel.add(c, BorderLayout.WEST);

if (dmtn.getUserObject()instanceof CorporateSpecial)

{

editorPanel.add(revenueCategoryBox, BorderLayout.SOUTH);

}

JTextField jtf = (JTextField)editingComponent;

jtf.setSelectionStart(0);

jtf.setSelectionEnd(jtf.getText().length());

jtf.requestFocus();

// set icon to renderers icon

editingIcon = editIcon;

return editorPanel;

}

publicboolean isCellEditable(EventObject event)

{

boolean retValue = super.isCellEditable(event);

if (retValue)

{

SortableTreeNode dmtn = (SortableTreeNode) tree.getSelectionPath().getLastPathComponent();

if (dmtn.getUserObject()instanceof LowItem)

{

retValue =false;

}

}

return retValue;

}

In the above, if the user object is a CorporateSpecial, I add the combo box to the editor panel. So, it seems to work, accept that the height of the tree node editing area doesn't expand enough to show two rows of items. Is there some call I need to make on the tree for the editing path to set it's height? Can this be done? I am trying to avoid doing a pop-up dialog for editing... I like the idea of the inline node editing.

Thanks.

[5121 byte] By [buckman1] at [2007-9-30 4:23:14]
# 1
Aint nobody out there know the going ons of this? ;)
buckman1 at 2007-7-1 12:52:20 > top of Java-index,Archived Forums,Socket Programming...
# 2
Don't you need a custom TreeCellRenderer to pull this off?
k_mccarthy at 2007-7-1 12:52:20 > top of Java-index,Archived Forums,Socket Programming...
# 3

Got that. Turns out that you have to set the Tree.setRowHeight(0) in order to avoid using a fixed cell height. It uses the cell renderer (or editor) to figure out the preferred size of the cell. Problem is, even though I did this, it appears I still have to set the row height in the getTreeCellEditorComponent() method directly, as the one time setting on the tree seems to get overridden somehow. Not sure how. Anyway, it does work. I have a two row editor showing up now, which is very slick! Inline expansion to show a 2 row editor, then when done it goes away. Nice.

buckman1 at 2007-7-1 12:52:20 > top of Java-index,Archived Forums,Socket Programming...
# 4
Figured it out, by the way. Simply return as part of the TreeCellEditor a JPanel with the components you want, and set the tree.setRowHeight(0) so that it uses the editing components height to adjust. Works great.
buckman1 at 2007-7-1 12:52:20 > top of Java-index,Archived Forums,Socket Programming...