• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

applet and browser problem

 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My applet uses the Swing for GUI of it. So I download the newest version of
java plug-in. In the init() method of my applet, I use the following code:

Containter c = getCotentPane();
c.add(Label1);
c.add(Button);
c.add(other stuffs ....);

And in the start() method the applet creates a sockt(TCP SOCKET) to comm. with
the servlet in the same/another machine. My problem is that the GUI interface
of my applet will not displayed in the browser.Why?

If I change the code in the init() method. I mean don't let the label, button,
etc. to be added directly on the contentPane, but on a new Frame(I change the
defination of variable c to :
Frame c = getContentPane(); ), the GUI of my applet will be displayed. In
this case, when I load the applet, there will be a applet GUI pop up from the
browser window(not directly embeded in the brower window), that's not what I
want.

Can anyone tell me how to fix this problem?
If canot fix this problem, does that mean applet running by embedded in
browser window has limited functionality?




 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It's called the Java "sandbox". Among other things, an applet in a browser can't open network connections to any machine other than the one it was downloaded from.
To get around this restriction you have to produce a signed applet, which is a messy and tedious process and rarely worth the effort, since with careful design you can usually do what you want without leaving the sandbox and often with better results.
For example, a lot of people want to be able to read and write local files, because that's traditional, but actually, in a business environment, it's a bad idea - server-based files get backed up more reliably, and if your machine breaks, you can just walk over to another one and still get your data.
Of course, no solution's perfect - server-based files do no good if the server's not available, but it's safer to make users download files manually rather than have them created by applets, since this is one of the main ways that viruses work in less secure environment.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
What is the error you are getting in the Java console. If you are failing during your socket creation you should be getting some security access violation.
Regards,
Manfred.
 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your reply!
But your reply cannot solve my problem. My applet is always connected to the same machine where the applet is from. Actually my problem is there's no applet displayed when applet itself is embedded in the browser. From the server side I can see that the applet sucessfully connected with the server by socket communication. How can I let my applet displayed by embedded in the browser window?
If I let the applet be a pop-up frame(not running in the same window of browser), there's no problem for my program.
Can you help me to figure it out?
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
One thing that you should know about JApplet and how it differs from Applet. The default layout manager is BorderLayout for JApplet and FlowLayout for Applet. Therefore in your case whatever "other stuffs ..." is you are only adding one thing which will be the last item you have added. Each add is just replacing the last component in the BorderLayout center.
Try the following line before the first add:
c.setLayout( new FlowLayout() );
The output should appear as you probably original expected.
Regards,
Manfred.
 
Jeff Pierce
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this a JApplet, you need to set the content pane directly. Here's an example:
public void init() {
setContentPane(makeContentPane());
}
public Container makeContentPane() {

promptButton = new JButton("Prompt");// todo nls
promptButton.setVerticalTextPosition(AbstractButton.CENTER);
promptButton.setHorizontalTextPosition(AbstractButton.CENTER);
promptButton.setMnemonic(KeyEvent.VK_P);
promptButton.setActionCommand(PROMPT);
promptButton.addActionListener(this);
promptButton.setToolTipText("Prompt on the selected command before executing it.");
commandField = new JTextField(20);
commandField.addActionListener(this);
JPanel pane = new JPanel();
pane.add(commandField);
pane.add(promptButton);

return pane;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic