Solved

JTree and JTable questions

Posted on 2003-11-30
9
625 Views
Last Modified: 2011-09-20
Hi,

I have a few questions about JTree and JTable:

1) JTree: When extedning DefaultTreeCellRenderer, I often see examples implementing the method

    getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus).

    When/by what is this method called and what does it do (from Java API: "Configures the renderer based on the passed in components.")?

2) JTree: What is a userObject, received in method getUserObject? (From Java API:  an Object provided by the user that constitutes the node's data) What is is really?

3) JTable: I have a JTable and a class implementing TableModel. How are the table celles populated? I want each row to present the attributes of one object in a Vector. Is it the getValueAt(int rowIndex, int columnIndex) method that I have to implement with a switch statement, each column number returns one of the attributes?

Thanks in advance!!
0
Comment
Question by:MarteJ
  • 6
  • 3
9 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9846293
1) it is called by the code responsible for painting the tree. The method retruns a component that is used to paunt a tree/table cell. Sort of like a rubber stamp.
0
 
LVL 92

Expert Comment

by:objects
ID: 9846300
2) It can be anything. It is a mechanism for you to attach your own object to a tree node.
0
 
LVL 92

Expert Comment

by:objects
ID: 9846307
3) You populate them in your implementation of the table model. A default implementation is available, DefaultTableModel which includes methods for adding data.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MarteJ
ID: 9850971
Thanks!

2) Do I have to set the user object for a TreeNode (using DefaultMutableTreeNode) with setUserObject(..) to be able to user getUserObject() ?

0
 
LVL 92

Expert Comment

by:objects
ID: 9852389
yes, or you can specifiy one in the ctor.
0
 

Author Comment

by:MarteJ
ID: 9856515
Strange things are happening. In the constructor of the node I call the setUserObject(result) method (result being an object passed to the constructor), and then I remove it. The JTree is displayed in the same manner, with the correct icons in both cases. Here is the Component getTreeCellRendererComponent which calls getUserObject():


 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {

      Result item;

      super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
      DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
      Object temp = node.getUserObject();
     
      if (temp instanceof Result) {
        item = (Result)temp;

        if (item.getValue() == 1) {
          setIcon(_greenIcon);
        }
         if (item.getValue() == 2) {
          setIcon(_redIcon);
        }
         if (item.getValue() == 3) {
          setIcon(_yellowIcon);
        }
      } else {
        setIcon(_yellowIcon);  
      }
      return this;
    }
  }


Seems like I do not have to set the user object explicitly. Is that right?
Thank you!
0
 
LVL 92

Expert Comment

by:objects
ID: 9856595
not sure what you mean, can you explain your confusion further and post the code where you are setting the user object.
0
 

Author Comment

by:MarteJ
ID: 9856683
Well, I want to understand the "nature" of user object. I call the getUserObject() in the code posted above. I wonder if I need to call the setUserObject() method or if this is not neccessary.

My JTree contains of MyTreeNodes, extending DefaultMutableTreeNode.
 
MyTreeNode node = new MyTreeNode(new Result("Result 1"));

---

public class MyTreeNode extends DefaultMutableTreeNode {

  public MyTreeNode(Result result) {
    super(result);
    setUserObject(result);  //neccessary?
  }

---

I just checked the constructor of DMTreeNode, called in super(result) above. The argument passed to the constructor is actually the UserObject... So I don't have to call setUserObject as I posted in the code here. I am no longer confused. Thanks for all help!

0
 
LVL 92

Expert Comment

by:objects
ID: 9856698
>  setUserObject(result);  //neccessary?

u r correct, not necessary

Happy to help :)

http://www.objects.com.au
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question