3

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

如何使所有列都可编辑?

对不起大家,

这是我的源代码:

/*
* 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;
}
}

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

4

1 回答 1

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));
于 2012-10-18T10:44:15.203 回答