This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes tree in frame, frame in applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "tree in frame, frame in applet" Watch "tree in frame, frame in applet" New topic
Author

tree in frame, frame in applet

Charlotte Shearrill
Ranch Hand

Joined: Dec 07, 2001
Posts: 34
I am attempting to have a frame inside of an applet. Inside of my frame I am creating a tree structure. Why does it not see TreeDemo2 as a method of the JApplet superclass?

I am getting the following error message:
: cannot resolve symbol
Symbol : constuctor Japplet (java.lang.String)
Location: class.javax.swing.Japplet
Super(growTree);
^
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Charlotte,
A Frame (and Window) are considered top-level containers and therefore can not be added into an Applet which is not a top-level container. You can only add another applet or a panel as containers into an Applet.
Regards,
Manfred.
Charlotte Shearrill
Ranch Hand

Joined: Dec 07, 2001
Posts: 34
hi manfred,
thank you for your comment, but i am more confused. in one of the books that i own, this small section of code works fine. it is using both applet and frame. is that not correct?

i also read in another post on this site (i am unsure of date and topic title) that it is possible as long as i create an instance of an applet and pass that instance to a frame, then something about adding it at center and setting the layout of frame as border layout. also your post on dec. 19, 2001 re: calling JFrame in Japplet. what am i misunderstanding with all of this? thanks in advance.
charlotte
Mark Fletcher
Ranch Hand

Joined: Dec 08, 2001
Posts: 897
Hello Charlotte,
In the code you have quoted look closely:
The line
frame.getContentPane().add(applet)
Adds the applet to the frame.
You can add an applet to a frame, but not the other way round.
Have a look at Suns Java Tutorial, especially the Swing/AWT trails. They should hopefully clear things up
Hope that helps
Mark


Mark Fletcher - http://www.markfletcher.org/blog
I had some Java certs, but they're too old now...
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Charlotte,
Sorry about that I didn't look at your code. You are correctly placing the Applet into the Frame but you just described it backwards in your post.
Getting back to your original code snippet. The problem lies in all the applet methods you are calling and when you are calling them. Two points about Applets are important here:
1. Applets don't use constructors and should not even have them in the class because it just adds confusion.
2. An Applet gets kicked off by calling the init method. No constructor is ever called nor should be.
Your approach was close but the main problem is that you are calling many applet methods and when those methods are being called.
The idea is to create an Applet class and call its init method before we add it into a Frame. That is all there is to it and it will then work fine. The code below shows the changes to your original code in bold.

Regards,
Manfred.
Charlotte Shearrill
Ranch Hand

Joined: Dec 07, 2001
Posts: 34
hi again manfred,
thank you for delving into this a little more, this is correct in what i want but i am now back to my original problem. my expandable tree is not being displayed. in the last section of the edited code the function that performs to display my tree has been commented out. the following is a vital part of this code.

how do i now get my expandable tree to be displayed and if i am still misunderstanding in a big way, where/how should my tree be displayed?
also thank you for taking a look at this mark. i think manfred pointed out my problem in that i misworded what i was doing.
thank you both,
charlotte
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Charlotte,
This is what you are setting up with your code snippet given before:
frame -> applet -> label
what does this mean? Well you have a label inside of your applet which is inside of a frame.
It sounds like you want to replace the label with some Expandable Tree. That means that you should place the stuff you are doing in the constructor into the Applet init method. Then you will be doing the following in your main method:
frame -> applet -> TreeDemo3
Regards,
Manfred.
Charlotte Shearrill
Ranch Hand

Joined: Dec 07, 2001
Posts: 34
hi manfred,
i have taken the opportunity to put my code that use to reside in public TreeDemo3() into my public void init()

(1) since public TreeDemo3 is no longer a method, i can’t make a call to super(“TreeDemo3”) because it has no superclass? right or wrong?
(2) commenting the super(“TreeDemo3”); statement allows me to compile and run it from my command prompt fine (java TreeDemo3) with no errors. however it will not allow me to view it if i use appletviewer TreeDemo3.html at my command prompt. it gives me a java.security.AccessControlException: access denied and it references the section where i start my database stored procedure calls. i am sure that my applet is telling me that it will not allow me to create it because now my code is considered dangerous…. i don’t know exactly where my danger is. any suggestions on narrowing my search and finding danger? matinee friday….only in the movies do they go looking for danger.
thanks,
charlotte
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Charlotte,
That is the major difference between an Applet and an Application. Applets are by default serverly restricted when performing I/O.
Look at the following web site for a good explanation of how to play outside the sandbox with Applets:
http://developer.java.sun.com/developer/technicalArticles/Security/Signed/
NOTE: The super("") can only be used inside a constructor and your applet doesn't have one.
Regards,
Manfred.
Charlotte Shearrill
Ranch Hand

Joined: Dec 07, 2001
Posts: 34
heah manfred…
i thought i would give you the lastest update. i got my code to work. since my applet didn’t have permission to access the sun.jdbc.odbc.JdbcOdbcDriver class, it kept bombing out when it tried to connect to my database. i had to add the following two lines of code to my java policy file located in the JAVA_HOME/jre/lib/security/ directory:
permission java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc";
permission java.util.PropertyPermission "file.encoding", "read";
i was then able to run my code with appletviewer FILENAME.html.
thank you so much for helping me out with this.
thanks.
charlotte
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tree in frame, frame in applet
 
Similar Threads
Need SERIOUS HELP!!
No compilation error in 1 dir, but compilation error in other, why?
How Do I Turn My Java Application into a JApplet and keep it as an Java Application too
JSplitterView treats panes differently
Swing Look and Feel not working