Creating TreeTables in Swing

Using this guide ( http://www.comp.nus.edu.sg/~cs3283/ftp/Java/swingConnect/tech_topics/tables-trees/tables-trees.html ) I am trying to create a tree table for my Swing user interface.

In the constructor of my class for the table, I redefined the visualization as follows:

public class JTreeTable extends JTable {
    public JTreeTable(ITreeTableModel treeTableModel) {
        super();
        ...
        setDefaultRenderer(ITreeTableModel.class, tree); 
        ...
    }

}

where treeTableModel is my implementation for

interface ITreeTableModel extends TreeModel

The results table shows what I want, but I have problems with the password:

enter image description here

  • The field (ID) in my code defines it as an Object, but in fact it represents numbers (1,2,3 and so on). How to change the presentation of a field identifier?

  • Nodes in my table are not expanding. But

    public int getChildCount (Object parent_object_id)

returns a number> 0

p.s. , , , , , , , .

+4
1

TreeCellRenderer. , - :

//and override also all the other functions of TreeCellRenderer
public Component getTreeCellRendererComponent(JTree tree, Object value,
      boolean selected, boolean expanded, boolean leaf, int row,
      boolean hasFocus) {
    Component returnValue = null;
    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
      Object userObject = ((DefaultMutableTreeNode) value)
          .getUserObject();
      if (userObject instanceof YourClass) {
        YourClass yourElement = (YourClass) userObject;
        if(col==0) titleLabel.setText(yourElement.getID());
        if(col==1) titleLabel.setText(yourElement.getName());
        if(col==2) titleLabel.setText(yourElement.getParentID());
        if (selected) {
          renderer.setBackground(backgroundSelectionColor);
        } else {
          renderer.setBackground(backgroundNonSelectionColor);
        }
        renderer.setEnabled(tree.isEnabled());
        returnValue = renderer;
      }
    }
    if (returnValue == null) {
      returnValue = defaultRenderer.getTreeCellRendererComponent(tree,
          value, selected, expanded, leaf, row, hasFocus);
    }
    return returnValue;
  }
}

, , Cell Renderer (, DictionaryItem @11abb71) , getID().

TreeCellRenderer.

. TreeModel. , . "+" "-", , , , getChildCount , , , - getChild (int row), .

0

All Articles