Solved

JTree and JTable questions

Posted on 2003-11-30
9
617 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 51
topping2 challenge 13 80
micro services vs rest web services 16 81
Java Server Faces parameter pass? 6 37
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

937 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now