Solved

JTree and JTable questions

Posted on 2003-11-30
9
616 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
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 …

747 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

15 Experts available now in Live!

Get 1:1 Help Now