Solved

Question Review: "The size of an applet inside a browser"

Posted on 2004-09-14
29
244 Views
Last Modified: 2010-03-31
Hey Experts,

Please revisit the following question, and post your comments.....I think this question can be re-opened and a better solution could be obtained:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21119414.html
0
Comment
Question by:mte01
  • 18
  • 10
29 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12060940
you can use percentage widths in the html, eg width="100%" to make it the width of window.
0
 
LVL 92

Expert Comment

by:objects
ID: 12060943
to dynamically resize it you could try using javascript.
0
 
LVL 3

Author Comment

by:mte01
ID: 12060953
objects, we went through all of this in the previous question.....and we had a problem in java-to-javascript communication, and in the setSize method of a JComponent. Please re-visit the previous question and read our comments if you can
0
 
LVL 92

Expert Comment

by:objects
ID: 12060956
you certainly cannot use setSize(), the size is controlled by the browser.
0
 
LVL 92

Expert Comment

by:objects
ID: 12060960
why do you need to change the size?
0
 
LVL 92

Expert Comment

by:objects
ID: 12060963
You can always open a frame from your applet, that can be resized.
0
 
LVL 3

Author Comment

by:mte01
ID: 12060978
>> why do you need to change the size?

My applet displays different forms, and each form has its own size, and I what I want is that the size of the applet inside the browser to be changed for the form to look better in the browser
0
 
LVL 92

Expert Comment

by:objects
ID: 12061001
have you tried using javascript?

another option would be to give your applet the entire browser window (or a large enough chunk of it) and let the applet handle resizing itself within that.
0
 
LVL 3

Author Comment

by:mte01
ID: 12061004
>> You can always open a frame from your applet, that can be resized.

If you can post a few lines of code that show a JFrame added to a JApplet......that would be really greatful....(This is easy but we are used to add everything to the ContentPane of a JApplet, and I don't want to do later a silly mistake in using the JFrame, besides we will have to worry about the size of the applet when we are changing the size of the frame....that's why I want you to please to post your proper code)
0
 
LVL 92

Expert Comment

by:objects
ID: 12061015
JFrame f = new JFrame();
// add the content to the frame
f.pack();
f.show();
0
 
LVL 3

Author Comment

by:mte01
ID: 12061041
>> have you tried using javascript?

Please objects do read the comments on the previous question: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21119414.html . We had problems in the java-to-javascript communication........read the comments there and post any recommendations of yours

>> another option would be to give your applet the entire browser window (or a large enough chunk of it) and let the applet handle resizing itself within that.

How could that be done in Java code given that setSize doesn't work?

>> JFrame f = new JFrame();
Adding the frame to the applet would use: this.getContentPane.add(f).......right?, and this is better to be before adding my JPanels to the frame or after them?
0
 
LVL 92

Expert Comment

by:objects
ID: 12061062
I did read the other q, it wasn't clear what problems you had with using js.

> How could that be done in Java code given that setSize doesn't work?

you would layout your forms panel within your applet so that it was the required size.
you cannot set the size of your applet, but all the components within your applet's size can be controlled typically by your layout managers.

> Adding the frame to the applet would use: this.getContentPane.add(f).......right?,

you don't need to add the frame to the applet, it is a seperate top level window

0
 
LVL 3

Author Comment

by:mte01
ID: 12061107
>> it wasn't clear what problems you had with using js

I want to change to the size of the applet upon user requests of certain forms from the database, so I need to do it in the Java code. We found out that we can use a netscape javascript library, to use javascript functions inside my java....first of all it would be cumbersome, second it might cause some problems in IE broswers......that was out problem

>> you would layout your forms panel within your applet so that it was the required size.

Yes this is what I am doing right now, but what I also want is that the size of the applet inside the browser to be just fitting the JPanels (and not very large)

>> you don't need to add the frame to the applet, it is a seperate top level window

You mean to use a frame instead of an applet?? But what I am building is an applet to be viewed inside a browser (not a Java application)
0
 
LVL 2

Expert Comment

by:gen718
ID: 12061141
How about the applet calling back to the codebase server via a socket and triggering the creation of a temporary html page (createdURL) with the applet defined size parameters.

Then use getAppletContext().showDocument(createdURL) to force the browser to display the new document.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
ID: 12061153
>  so I need to do it in the Java code. We found out that we can use a netscape javascript library,

correct, that is using liveconnect
you can also try using showDocument() to pass js call to the browser.

> to use javascript functions inside my java....first of all it would be cumbersome,

why so?

> second it might cause some problems in IE broswers

the only problem is if js is disabled, there are no specific problem with IE that I'm aware of

> but what I also want is that the size of the applet inside the browser to be just fitting the JPanels (and not very large)

as already discussed you cannot directly change the size of the applet in the browser that is the responsibility of the browser.
you can however set the size of any child components within your applet, so if the applet is set to be as big as you would possible need you can adjust the size of your form within the applet.

> You mean to use a frame instead of an applet?? But what I am building is an applet to be viewed inside a browser (not a Java application)

No an standalone frame. They can be used with an applet or an application.
0
 
LVL 3

Author Comment

by:mte01
ID: 12061197
>> why so?
>> the only problem is if js is disabled, there are no specific problem with IE that I'm aware of

This would be cumbersome and you might have problems if you are using the netscape library that I am not talking about: http://www.netspade.com/articles/java/javascript.xml


.>> you can however set the size of any child components within your applet, so if the applet is set to be as big as you would possible need you can adjust the size of your form within the applet

Yes this is what I'm doing right now.........I was just trying for an optimal way of appearance

>> you can also try using showDocument() to pass js call to the browser
>> an standalone frame. They can be used with an applet or an application.

I'll be trying both of these methods........
0
 
LVL 92

Expert Comment

by:objects
ID: 12061208
>  if you are using the netscape library

its not a netscape library, it just had its origins from netscape.
it works fine in ie
0
 
LVL 92

Expert Comment

by:objects
ID: 12061211
from the link you just posted:

"Despite these classes being in Netscape packages, Internet Explorer also supports them."
0
 
LVL 3

Author Comment

by:mte01
ID: 12061244
from the link I just posted:

"There are a few annoying differences which I have found in Explorer, such as the fact that it won't always return values from functions correctly"

That's what I was a bit worried about.....and the fact that my code needs to use many functions of this library in many of its parts (if I wanted to use it in this manner), not only a few lines....

0
 
LVL 92

Expert Comment

by:objects
ID: 12061249
i've never had a problem with it, and in your case you don't need return values.

i'd suggest trying it though, if there is a problem with the approach it is more likely to be the actual resizing rather than with liveconnect.
0
 
LVL 3

Author Comment

by:mte01
ID: 12061741
1- I tried usign showDocument and loading a new HTML file.......it seems that it would need a new HTML file with new settings for each form.......there are around 61 tables in my database, and a simple form would be a combination of two tables of these 61 which would make the minimum number of forms to be 61*61! So this solution is clearly unttainable

2-I tried using a JFrame to load everything in it instead of the JApplet......it would be fine.........but I don't know if my supervisor would agree about opening a window from the current applet window, and also it doesn't look that good....it would be a possible solution.

3-I tried using the JScript library, and I added the following code (to replace setSize):

try
   {
      Object[] params = new Object[3];
      params[0] = this.getAppletContext().getApplet("TestApplet");
      params[1] = new Integer(this.getWidth());
      params[2] = new Integer(this.getHeight());
      JSObject.getWindow((Applet)params[0]).call("changeApplet",params);
   }
   catch(Exception ex)
   {
      ex.printStackTrace();
    }

and I got a java.lang.IllegalAccessException: Field is final at the JSObject.getWindow line.......

I am using the HTML script:

<html>
<head>
<title>
LC Applet
</title>
<script language="javascript">
<!--

function changeApplet(applet,width,height)
{
   if (width)
   {
      applet.width=width;
   }
   if (height)
   {
      applet.height=height;
   }
}

//-->
</script>

</head>
<body>
LC Applet will appear below in a Java enabled browser.<br>
<applet
  codebase = "."
  code     = "lcfilemain.Applet5.class"
  archive  = "lcfmapplet.jar, jbcl.jar"
  width    = 900
  height   = 550
  name     = "TestApplet"
  hspace   = "0"
  vspace   = "0"
  align    = "top"
>
</applet>
</body>
</html>


0
 
LVL 92

Expert Comment

by:objects
ID: 12061755
> I tried usign showDocument and loading a new HTML file.

sorry I didn't mean load a new page, I meant to call a js function using "javascript:...."
0
 
LVL 92

Expert Comment

by:objects
ID: 12061761
you need mayscript in your applet tag.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12061768
if there's nothing else on your (html) page whats the problem with giving the applet as much space as it possibly needs and sizing your form within the applet?
0
 
LVL 3

Author Comment

by:mte01
ID: 12061828
>> you need mayscript in your applet tag.

I included it (I had forgot to do that), but the error remained the same

>> I meant to call a js function using "javascript:...."

Can provide an example on how changeApplet would be called in this case?

>> if there's nothing else on your (html) page whats the problem with giving the applet as much space as it possibly needs and sizing your form within the applet?

I might try to do that, and set the Applet's background to white, and not show any boundaries, becasue we have naive users who are using this system and they will ask why is this area empty and such stuff.....
0
 
LVL 92

Expert Comment

by:objects
ID: 12061852
> and they will ask why is this area empty and such stuff.....

isn't the same going to occur if you resize the applet itself :)
0
 
LVL 92

Expert Comment

by:objects
ID: 12061855
> Can provide an example on how changeApplet would be called in this case?

something like:

context.showDocument(new URL("javascript:changeApplet();"));
0
 
LVL 3

Author Comment

by:mte01
ID: 12062035
>>isn't the same going to occur if you resize the applet itself :)

yes, but the empty area would be the browser's area not the applet's (except if as I said the applet's background is white and there are no boundaries....so no difference would show)


As for the showDocument method, I tried the following:

try
    {
      Object[] params = new Object[3];
      params[0] = this.getAppletContext().getApplet("TestApplet");
      params[1] = new Integer(700);
      params[2] = new Integer(400);
      //JSObject.getWindow((Applet)params[0]).call("changeApplet",params);
      String surl = "javascript:changeApplet("+params[0].toString()+","+params[1].toString()+","+params[2].toString()+");";
      this.getAppletContext().showDocument(new URL(surl));
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }

I don't think the URL String was pretty accurate.....it didn't give an exception, but nothing happened, the size of the applet remained the same



Finally, I think I'll be trying either the JFrame, or the window size applet, or the internal sizing of the form with a white background applet
0
 
LVL 92

Expert Comment

by:objects
ID: 12062048
You could of course call the js directly from the html just to test if it works, I'm not sure how the applet will react to a resize. Will save you wasting some time if it turns out that that the applet does not like it.

>  or the internal sizing of the form with a white background applet

I think that seems to be your best option
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now