I try to make JTree with custom TreeRenderer
public class TextCellRender implements TreeCellRenderer {
JPanel panel;
JTextArea text;
JLabel label;
LayoutManager Layout;
public TextCellRender() {
text = new JTextArea();
text.setWrapStyleWord(true);
text.setLineWrap(true);
text.setSize(360, text.getPreferredSize().height);
label = new JLabel();
panel = new JPanel();
panel.add(label);
panel.add(text);
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf,
int row, boolean hasFocus) {
Component returnValue = null;
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if ((userObject instanceof FieldObj)) {
FieldObj my = (FieldObj) userObject;
String fieldText = "";
text.setText(my.valueList);
label.setText(my.FieldName);
}
return panel;
}
return returnValue;
}
}
and with custom Editor
public class TextCellEdit extends AbstractCellEditor implements TreeCellEditor {
Wich getTreeCellEditorComponent return panel as getTreeCellEditorComponent but with JComboBox wich Items populates from db. Render and Editor work great i can click on field and comboBox shows with values from db.
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row) {
if (value != null && value instanceof DefaultMutableTreeNode) {
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof FieldObj) {
FieldObj my = (FieldObj) userObject;
box.removeAllItems();
label.setText(my.FieldName);
populatebox(my.FieldName);
box.addItem(my.valueList);
panel.add(label);
panel.add(box);
} else {
box.addItem("Uknown object type");
}
return panel;
}
}
public Object getCellEditorValue() {
System.out.println("getCellEditoValue returns :" + box.getSelectedItem());
return box.getSelectedItem();
}
But it don't save in Render textArea i mean in Render i have panel with: JLabel (FieldName) JTextArea (FieldValue)
when i click on JTextArea i have my Editor wich have JLabel (FieldName) JComboBox(FieldValues wich i have populated from db)
but when i chose something form Edit ComboBox it don't save in Render TextArea So the question is how this things should work ? How render component get value from Edit component ?