Solved

JTree and JTable questions

Posted on 2003-11-30
9
622 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 67
hibernate insert example 13 29
iterator/ListIterator approach 17 27
going to wrong jsp page 2 21
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

809 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