JXTreeTable可编辑示例?

时间:2012-10-17 07:44:23

标签: java swing swingx jxtreetable

是否有任何可编辑的JxTreeTable的工作示例..我确实尝试了一些方法。但没有运气......只有第一栏是可编辑的。

如何使所有列都可编辑?

对不起伙计们,

这是我的src代码:

/*
* To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
package org;
import java.awt.Color;
import java.awt.Component;
import java.util.EventObject;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.CellEditorListener;
import javax.swing.event.TreeModelListener;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreePath;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.decorator.*;
import org.jdesktop.swingx.decorator.HighlightPredicate.ColumnHighlightPredicate;
import org.jdesktop.swingx.painter.CheckerboardPainter;
import org.jdesktop.swingx.renderer.DefaultTreeRenderer;
import org.jdesktop.swingx.renderer.IconValue;
import org.jdesktop.swingx.renderer.StringValue;
import org.jdesktop.swingx.treetable.*;
public class JTreeTable extends JXTreeTable{
public JTreeTable(){
setTreeTableModel(new RandomTextTreeTableModel(5));
    setRowSelectionAllowed(true);

    setDragEnabled(true);
    setEditable(true);
 setTreeCellRenderer(new DefaultTreeRenderer());
    setSelectionBackground(new Color(176, 197, 227));
    setSelectionForeground(new Color(0, 0, 128));

    BorderHighlighter border = new BorderHighlighter(new ColumnHighlightPredicate(1, 2), BorderFactory.createEmptyBorder(0, 6, 0, 6), false, false);
    addHighlighter(border);


}
@Override
public TableCellEditor getCellEditor(int row, int column){
    System.out.println(column);
    return new TableCellEditor() {

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            //throw new UnsupportedOperationException("Not supported yet.");
            return new JTextField();
        }

        public Object getCellEditorValue() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return "";
        }

        public boolean isCellEditable(EventObject anEvent) {
            //throw new UnsupportedOperationException("Not supported yet.");
            return true;
        }

        public boolean shouldSelectCell(EventObject anEvent) {
            //throw new UnsupportedOperationException("Not supported yet.");
            return true;
        }

        public boolean stopCellEditing() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return false;
        }

        public void cancelCellEditing() {
            //throw new UnsupportedOperationException("Not supported yet.");

        }

        public void addCellEditorListener(CellEditorListener l) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        public void removeCellEditorListener(CellEditorListener l) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    };
}
}class RandomTextTreeTableModel extends DefaultTreeTableModel {

DefaultMutableTreeTableNode Root = null;

RandomTextTreeTableModel(int i) {
    Root = new DefaultMutableTreeTableNode();
    this.setRoot(Root);
    for (int x = 0; x < 5; x++) {
        DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode("HI");
        Root.add(node);
        for (int j = 0; j < i; j++) {
            node.add(new DefaultMutableTreeTableNode(j));
        }
    }
}

@Override
public String getColumnName(int column){
    return "dsdsddds";
}

@Override
public int getColumnCount() {
    return 2;
}


@Override
public Object getValueAt(Object arg0, int arg1) {
    if (arg1 == 1) {
        return new String("hi " + arg0.toString());
    }
    return arg0;
}
}

谢谢....我想编辑所有列。但此代码仅适用于第一列。

1 个答案:

答案 0 :(得分:4)

这是一个简单的例子,简单的步骤

  • 实现自定义可编辑TreeTableNode,扩展AbstractMutableTreeTableNode
  • 在DefaultTreeTableModel中使用它
  • 使用模型
  • 实例化JXTreeTable

在代码中:

/**
 * A custom node implementation.
 */
public static class ArrayNode extends AbstractMutableTreeTableNode {

    public ArrayNode(Object[] data) {
        super(data);
    }

    @Override
    public Object getValueAt(int column) {
        return getUserObject()[column];
    }

    @Override
    public void setValueAt(Object aValue, int column) {
        getUserObject()[column] = aValue;
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public Object[] getUserObject() {
        return (Object[]) super.getUserObject();
    }

    @Override
    public boolean isEditable(int column) {
        return true;
    }

}

// usage
ArrayNode root = new ArrayNode(new Object[] {"root", ""});
for (int i = 0; i < 5; i++) {
    root.add(new ArrayNode(new Object[]{"child", "" + i}));
}
JXTreeTable table = new JXTreeTable(new DefaultTreeTableModel(root));