1

我已经在摇摆中实现了一个树表,参考此处提供的教程 。我有两列,其中将显示 Message 下的 Message Object 或 Fields 对象。如果节点是字段,那么它的值将显示在第二列中。我可以成功查看所需的数据,将单元格渲染器添加到第二列,其中值是一个集合,然后向单元格添加一个组合框。当我添加单元格编辑器时,问题就来了。下面是我的单元格编辑器。

    private class ValueCellEditor extends AbstractCellEditor implements   TableCellEditor {
        JTextField textEditor = new JTextField();
        JComboBox comboBox = new JComboBox();
        Component comp;
        Field field = null;

        private ValueCellEditor(){
            int row=myTreeTable.getSelectedRow();
            Object object= myTreeTable.getValueAt(row, 3);
            TreePath path= myTreeTable.getPathForRow(row);
            Object o= path.getLastPathComponent();
            MyDataNode node=(MyDataNode)o;
            Field f=(Field)node.getNodeDataObject();

            comboBox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if(field!=null){
                        if(e.getStateChange() == ItemEvent.SELECTED) {
                            field.setSelectedValue(comboBox.getSelectedItem());
                            field.setDefaultValue(comboBox.getSelectedItem());
                        }
                    }
                }
            });

            textEditor.addKeyListener(new KeyAdapter() {
                @Override
                public void keyReleased(KeyEvent e) {
                    if(field!=null){
                        field.setDefaultValue(textEditor.getText());
                    }
                }
            });
        }

        @Override
        public Component getTableCellEditorComponent
                (JTable table, Object value, boolean isSelected, int row, int column) {

            if(value instanceof List<?>) {
                populateComboBox((ArrayList)value);
                comp = comboBox;
            } else {
                textEditor.setText((String)value);
                comp = textEditor;
            }
            return  comp;
        }

        @Override
        public Object getCellEditorValue() {

            if(comp != null && comp instanceof JTextField ) {
                return  textEditor.getText();
            } else if( comp != null && comp instanceof JCheckBox) {
                return comboBox.getSelectedItem();
            }
            return null;
        }

        private void populateComboBox(List<Value> valueList){
            comboBox.removeAll();
            comboBox.setSelectedItem(null);
            for (Value val: valueList) {
                comboBox.addItem(val.getDescription());
            }

            if(field.getSelectedValue() != null) {
                comboBox.setSelectedItem(field.getSelectedValue());
            }
        }
    }

在我的单元格编辑器的构造函数中,我添加了如下代码,通过它我试图获取树表选定行的当前选定节点。但是我输入的代码不起作用,因为“getPathForRow”函数不支持树表。

        int row=myTreeTable.getSelectedRow();
        Object object= myTreeTable.getValueAt(row, 3);
        TreePath path= myTreeTable.getPathForRow(row);
        Object o= path.getLastPathComponent();
        MyDataNode node=(MyDataNode)o;
        Field f=(Field)node.getNodeDataObject();

这是我正在使用的表格模型。

    import com.milleniumit.walle.frontend.frontEndDto.Field;

public class MyDataModel extends MyAbstractTreeTableModel {

    static protected String[] columnNames = { "Field", "Value" };
    static protected Class<?>[] columnTypes = 
        { MyTreeTableModel.class, Object.class};

    public MyDataModel(MyDataNode rootNode) {
        super(rootNode);
        root = rootNode;
    }

    public Object getChild(Object parent, int index) {
        return ((MyDataNode) parent).getChildren().get(index);
    }


    public int getChildCount(Object parent) {
        return ((MyDataNode) parent).getChildren().size();
    }


    public int getColumnCount() {
        return columnNames.length;
    }


    public String getColumnName(int column) {
        return columnNames[column];
    }


    public Class<?> getColumnClass(int column) {
        return columnTypes[column];
    }

    public Object getValueAt(Object node, int column) {
        MyDataNode mNode=(MyDataNode)node;
        Object obj =mNode.getNodeDataObject();

        if(column==0){
            return mNode.getName();
        }
        else if (column==1){
            if(obj instanceof Field){
                Field field=(Field)mNode.getNodeDataObject();
                if(field.getFieldDef().getListValue().size()>0){
                    return field.getFieldDef().getListValue();
                }
                else
                    return mNode.getDefaultValue();
            }
            else
                return mNode.getDefaultValue();
        }
        return null;
    }

    public boolean isCellEditable(Object node, int column) {
        return true; // Important to activate TreeExpandListener
    }

    public void setValueAt(Object aValue, Object node, int column) {
        MyDataNode mNode=(MyDataNode)node;

        if (mNode.getNodeDataObject() instanceof Field && column == 1) {
            Field field = (Field) mNode.getNodeDataObject();
            field.setDefaultValue(aValue);
            field.setSelectedValue(aValue);
        }
    }

}

我需要获取树表的选定节点,以便我可以获取当前选定的字段以通过编辑器编辑它的更改值。有人可以让我知道我的代码哪里出错了。获取此树表的当前选定节点的正确方法是什么。

4

0 回答 0