首页 > 解决方案 > 用于多节点编辑的 TreeCellEditor

问题描述

有人可以展示一个可以在一次编辑中更新多个节点的 TreeCellEditor 示例吗?据我所知,GetCellEditorValue()只会更新一个节点。

我的编辑器当前接收选定的节点,比较它们并显示不同的值,同时显示节点共有的其他值。

我的构造函数初始化“myDevice”。

    public DeviceEditor(Collection<DefaultMutableTreeNode> nodes) throws NoSuchFieldException {
    System.out.println("CREATING NEW EDITOR \n");
    this.nodes = nodes;
    ObjectMatcher matcher = new ObjectMatcher();
    try {
        myDevice = matcher.match(nodes, DefaultDevice.CREATE_MULTIVALUE_DEFAULTDEVICE(), new DefaultDevice());
        //System.out.println("Device= " + myDevice.getAddress() + " " + myDevice.getHostName());
    } catch (Exception e) {
        System.out.println(e);
    }
    initComponents();

}

TreeCellEditor 的方法

@Override
public Component getTreeCellEditorComponent(JTree jtree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
   // this.tree = jtree;
    return this;

}

@Override
public Object getCellEditorValue() {
    //This value is what is populated into the JTree
    return this.myDevice;
}

@Override
public boolean isCellEditable(EventObject eo) {
    //should be true only if it's a leaf. 
    return true;
}

@Override
public boolean shouldSelectCell(EventObject eo) {
    //Don't Select
    return false;
}

@Override
public boolean stopCellEditing() {
    try {
        System.out.println("\n Cell Editing Stopped");
        //update myDevice
        if (!this.addressField.getText().equals(DefaultDevice.MULTIVALUE)) {
            myDevice.setAddress(this.addressField.getText());
        }
        myDevice.setDeviceType(this.deviceTypeField.getText());
        myDevice.setLocation(this.locationField.getText());
        myDevice.setSerialNumber(this.serialField.getText());
        myDevice.setUser(this.userField.getText());
        myDevice.setPassword(new String(this.passwordField.getPassword()));
        myDevice.setVendor(this.vendorField.getText());
        myDevice.setModel(this.modelField.getText());
        myDevice.setOS(this.osField.getText());
        myDevice.setDescription(this.descriptionField.getText());
        myDevice.setVersion(this.versionField.getText());
        myDevice.setDeviceType(this.deviceTypeField.getText());
        myDevice.setDisplayHostName(this.hostNameCheckBox.isSelected());
        myDevice.setDisplayIPV4Address(this.ipV4checkBox.isSelected());
        myDevice.setDisplayIPV6Address(this.ipV6CheckBox.isSelected());

        DeviceEditor.UPDATE_DEVICES(nodes, myDevice);
        return true;
    } catch (IPConverter.InvalidIPException ex) {
        Exceptions.printStackTrace(ex);
        return false;
    }

}

标签: javajtree

解决方案


推荐阅读