Solved

JTree and JTable questions

Posted on 2003-11-30
9
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 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