• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrame and JPanel

 
Wynn Teoh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I try to have a main class that calls TreeList.java, which will call another class FileTree.java . However, when I run the program, it came out as two separate window.
How can I make it in one window ? I tried to put leftPanel.add(fileTree, "FileTree"); in my TreeList.java but it didn't work. Anyone can help? Thanks

I have a main.java


And my TreeList.java


And here's my FileTree.java

 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what you are trying to do.



FileTree is a JFrame. You CAN"T add a frame to a panel.

Read the Swing tutorial for examples of adding "panels" to "frames".

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
 
Wynn Teoh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, my mistake on that line, it supposed to be


However, the main issue is actually like what you mentioned, I can't add a frame to a panel...
I need to call the function addWindowListener , am I right to say that it's only applicable to JFrame?

I'm trying to have this example http://www.java2s.com/Code/Java/Swing-JFC/FileTreewithPopupMenu.htm onto my left hand side of my applet, and right hand side will be another tree from the server.
Any advice how I can achieve this?
Thank you
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not have your FileTree either subclass a JPanel, or (my preference) extend JPanel functionality by composition, rather than have it subclass a JFrame?
 
Wynn Teoh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pete, thanks for the headstart. Sorry, but have a question that may sound stupid.. I do not understand how I should go about, meaning I change my FileTree to extend JPanel instead? From there, how can I go about to call the JFrame functions?



Thanks again.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wynn Teoh wrote:Hi Pete, thanks for the headstart. Sorry, but have a question that may sound stupid.. I do not understand how I should go about, meaning I change my FileTree to extend JPanel instead?

To be honest, my GUI code never subclasses JFrame and only rarely subclasses JPanel, usually when I need to override one of JPanel's methods such as paintComponent. In this situation, I would gear my class to create a JPanel and then make that JPanel available to other classes using this one, perhaps via a getPanel() method that I create. Doing it this way helps prevent me from shooting my foot, like I've done in the past when I accidentally overrode JPanel's getX() and getY() methods. This is off the subject of your main question, and you'll likely be able to get by with extending JPanel, but I feel obliged to mention this.


From there, how can I go about to call the JFrame functions?



Why does this class need to have a window listener? What purpose does it serve? If absolutely necessary, you could always have a public method in this class that you have your WindowListener call.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic