3

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

ありがとうございます....すべての列を編集したいです。ただし、このコードは最初の列でのみ機能します。

4

1 に答える 1

4

これは簡単な例で、簡単な手順です

  • AbstractMutableTreeTableNode を拡張して、カスタムの編集可能な TreeTableNode を実装する
  • それを 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 に答える