Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTree unsing in other class  RSS feed

 
Douby Luka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I am trying to make a JTree. I have used this guide :http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#eg

Now when I call my Class with
TreeMainMenu tree = new TreeMainMenu();
JScrollPane MainMenu = new JScrollPane(tree);

I get only the default JTree..

I need to understand how I should build my code to call the class as JTree. Please explain to me.

thank you

This is my code:
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Douby,

do you perhaps add your JTree to the panel? If so, just add the JScrollPane to that panel.

Greetz,
Piet
 
Douby Luka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo Piet,

no I am adding the ScrollPane to the panel



 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must say, I was quite a bit puzzled. Never worked with JTrees myself,
and when I ran your code in NetBeans, I kept seeing other nodes than
what your code intended. I really had no clue whare the nodes that I saw
were coming from. Added to this, the JScrollPane was acting as it should.

Well, what struck me is that you have some kind of constructor,
where you create a JTree and return it, while at the same time your class
already extends JTree. I didn't understand what was happening there.

I took the liberty to change your code a little bit, especially the contructor,
and I made a main() method to test it out.

The APNode stuff and the Node itself are unchanged.

I now see a frame with a scrollpane, with a vertical scroll bar, and a horizontal when
the frame is dragged small enough.

Can you have a look at the code above, and compare it to what you have? Is it okay
this way?

Greetz,
Piet
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgetting GUI for a moment, you actually don't have a constructor. The line is actually declaring a method and not a constructor. A constructor should not have a return value. Unfortunately in Java, you can have a method which is same as the class name and so this would be difficult to find. I think this is the root of your problem. You can read this post where this has been explained better.

Nevertheless, you should think about why you have to extend JTree as well as declare a JTree variable within the class. You should only need either of these.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:I must say, I was quite a bit puzzled. Never worked with JTrees myself,
and when I ran your code in NetBeans, I kept seeing other nodes than
what your code intended. I really had no clue whare the nodes that I saw
were coming from.

javax.swing.JTree.java#getDefaultTreeModel() [lines 479 to 509] called from the default constructor.

I've never understood why they did that. Maybe it's just testing code that was never removed.
 
Douby Luka
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Ranganathan
I already tried with constructor and I get same issue. But I didn't change the name, I will try at home today.

Well, I get my problem solved by creating a DefaultTreeModel. So I call the methode, create new JTree and set the model.

thank you for help
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!