0

我从这里找到了一个使用 swing 组件制作树表的教程。我已经稍微改变了它以动态喂养它。当时我可以为表格和树行提供所需的文本。现在我想探索它并使用setValueAt(Object value,int column,int row). 我想将每行标题名称与列标题名称进行比较,并且对于每个匹配项,我想在该特定单元格中输入一些值并更改颜色。你有什么建议我该怎么做?我放了 3 个主要类,我猜这些类将被修改以获得比较列和行的功能。

import javax.swing.tree.TreeModel;

public interface MyTreeTableModel extends TreeModel {


/**
 * Returns the number of available columns.
 * @return Number of Columns
 */
public int getColumnCount();

/**
 * Returns the column name.
 * @param column Column number
 * @return Column name
 */
public String getColumnName(int column);


/**
 * Returns the type (class) of a column.
 * @param column Column number
 * @return Class
 */
public Class<?> getColumnClass(int column);

/**
 * Returns the value of a node in a column.
 * @param node Node
 * @param column Column number
 * @return Value of the node in the column
 */
public Object getValueAt(Object node, int column);


/**
 * Check if a cell of a node in one column is editable.
 * @param node Node
 * @param column Column number
 * @return true/false
 */
public boolean isCellEditable(Object node, int column);

/**
 * Sets a value for a node in one column.
 * @param aValue New value
 * @param node Node
 * @param column Column number
 */
public void setValueAt(Object aValue, Object node, int column);
}

二等

import java.awt.Rectangle;

import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.tree.TreePath;

public class MyTreeTableModelAdapter extends AbstractTableModel {

 JTree tree;
MyAbstractTreeTableModel treeTableModel;

public MyTreeTableModelAdapter(MyAbstractTreeTableModel treeTableModel, JTree tree) {
    this.tree = tree;
    this.treeTableModel = treeTableModel;

    tree.addTreeExpansionListener(new TreeExpansionListener() {
        public void treeExpanded(TreeExpansionEvent event) {
            fireTableDataChanged();
        }

        public void treeCollapsed(TreeExpansionEvent event) {
            fireTableDataChanged();
        }
    });
}



public int getColumnCount() {
    return treeTableModel.getColumnCount();
}

public String getColumnName(int column) {
    return treeTableModel.getColumnName(column);
}

public Class<?> getColumnClass(int column) {
    return treeTableModel.getColumnClass(column);
}

public int getRowCount() {
    return tree.getRowCount();
}

protected Object nodeForRow(int row) {
    TreePath treePath = tree.getPathForRow(row);
    return treePath.getLastPathComponent();
}

public Object getValueAt(int row, int column) {
    return treeTableModel.getValueAt(nodeForRow(row), column);
}

public boolean isCellEditable(int row, int column) {
    return treeTableModel.isCellEditable(nodeForRow(row), column);
}

public void setValueAt(Object value, int row, int column) {
    treeTableModel.setValueAt(value, nodeForRow(row), column);
}
}

三等舱

import java.awt.Dimension;

import javax.swing.JTable;
import javax.swing.ToolTipManager;

public class MyTreeTable extends JTable {
private MyTreeTableCellRenderer tree;
public MyTreeTable(){

}

public void expandTree(){
    for (int i = 0; i < tree.getRowCount(); i++) {
         tree.expandRow(i);
        }
}
public MyTreeTable(MyAbstractTreeTableModel treeTableModel) {
    super();

    //  set JTree.
    tree = new MyTreeTableCellRenderer(this, treeTableModel);
    ToolTipManager.sharedInstance().registerComponent(tree);

    setToolTipText("text");//ToolTip
    //expands all nodes at tree
      for (int i = 0; i < tree.getRowCount(); i++) {

          tree.expandRow(i);
            }
    // set model.
    super.setModel(new MyTreeTableModelAdapter(treeTableModel, tree));

    // select tree and table at the same time
    MyTreeTableSelectionModel selectionModel = new MyTreeTableSelectionModel();
    tree.setSelectionModel(selectionModel); // For the tree
    setSelectionModel(selectionModel.getListSelectionModel()); // For the table

    // Renderer for  Tree.
    setDefaultRenderer(MyTreeTableModel.class, tree);
    // Editor for  TreeTable
    setDefaultEditor(MyTreeTableModel.class, new MyTreeTableCellEditor(
            tree, this));

    //  Grid .
    setShowGrid(true);

    // no space
    setIntercellSpacing(new Dimension(0, 0));
    //setValueAt("jjjj", 0, 0); not working

}

}

4

0 回答 0