aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JFrame and JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame and JPanel" Watch "JFrame and JPanel" New topic
Author

JFrame and JPanel

Wynn Teoh
Greenhorn

Joined: Dec 04, 2009
Posts: 10
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

Joined: Jun 13, 2009
Posts: 2200
    
    7
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

Joined: Dec 04, 2009
Posts: 10
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Dec 04, 2009
Posts: 10
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

Joined: Feb 23, 2007
Posts: 1561
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFrame and JPanel