Swing JTree how custom Render get value from custo

2019-09-09 21:33发布

问题:

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 ?

回答1:

When you implement TreeCellEditor you must to override public Object getCellEditorValue() method. That method calls when you cancel/exit from editing. In this method you can save your new value to object that you store in TreeNode. Then your Renderer got edited object with new value in getTreeCellRendererComponent method.

EDIT: Your editor be like that :

public class TextCellEdit extends AbstractCellEditor implements TreeCellEditor {

private JComboBox<Object> box;
private JLabel label;
private JPanel panel;
private FieldObj my;

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) {
            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());
    my.FieldName = box.getSelectedItem();
    return box.getSelectedItem();
}
}