Creating TreeTables in Swing
Asked Answered
L

1

6

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

In constructor of my class for table I redefined render like this:

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

}

where treeTableModel is my implementation for

interface ITreeTableModel extends TreeModel

In result table looks near to what I want, but I have couple issues:

enter image description here

  1. Field (ID) in my code defines as Object but really it represents numbers (1,2,3 and so on). How to change representation of field ID?

  2. Nodes in my table do not expand. But

    public int getChildCount(Object parent_object_id)

returns number > 0

p.s. I know that there is maybe not enough information for direct answer, but I need at least direction in which I can to continue my investigations.

Luciferin answered 19/5, 2015 at 7:19 Comment(6)
A JTree will use the toString method of the node to generate the output. You can either change what the toString method returns or use a TreeCellRenderer (preferably)Hungerford
@Hungerford Thank you for advise. It works for me (A JTree will use the toString method of the node to generate the output). The second method is bit complicated :-)Luciferin
don't reinvent the wheel, nor bothering with (isn't easy without override BasicsTreeXxx, both Renderer, both Models, too hard XxxCellEditor wihtout excelent knowledge about) 1. to buy Jide, 2. SwingX has very good workaroundSwainson
@Swainson Thank you for suggestion. I've downloaded SwingX demo. It looks pretty good and contains TreeTable classLuciferin
Also consider Outline.Georgeannageorgeanne
See here for expanding jtree nodes.Caltanissetta
K
0

Related to the first of your questions, you should implement a TreeCellRenderer. Guessing that you do would do something similar to:

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

What is currently happening to you is that your Cell Renderer is returning the Object instance element ID (DictionaryItem@11abb71 for example) instead of getting the object and call the getID() function.

You can find extra example and information on TreeCellRenderer example.

Related to your second question see the example on TreeModel example. Maybe you can also try to expand the row by code. If the "+" icon is changing to a "-" it would probably means that getChildCount is working well but what it isn't working is the getChild(int row) which will return null or an unvalid tree row element.

Karaganda answered 25/6, 2015 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.