如何摆动树桌的选定节点?

时间:2016-04-06 01:54:05

标签: java swing jtreetable treetablecelleditor

我已经实现了一个可以使用的教程here 。我有两列,其中将显示Message下的Message Object或Fields对象。如果节点是Field,则它的值将显示在第二列中。我可以查看所需的数据,成功地将单元格渲染器添加到第二列,其中值是一个集合,然后将一个组合框添加到单元格。当我添加单元格编辑器时会出现问题。下面是我的单元格编辑器。

    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);
        }
    }

}

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

0 个答案:

没有答案