Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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

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
mte01
Asked:
mte01
  • 18
  • 10
1 Solution
 
objectsCommented:
you can use percentage widths in the html, eg width="100%" to make it the width of window.
0
 
objectsCommented:
to dynamically resize it you could try using javascript.
0
 
mte01Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
objectsCommented:
you certainly cannot use setSize(), the size is controlled by the browser.
0
 
objectsCommented:
why do you need to change the size?
0
 
objectsCommented:
You can always open a frame from your applet, that can be resized.
0
 
mte01Author Commented:
>> 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
 
objectsCommented:
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
 
mte01Author Commented:
>> 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
 
objectsCommented:
JFrame f = new JFrame();
// add the content to the frame
f.pack();
f.show();
0
 
mte01Author Commented:
>> 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
 
objectsCommented:
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
 
mte01Author Commented:
>> 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
 
gen718Commented:
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
 
objectsCommented:
>  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
 
mte01Author Commented:
>> 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
 
objectsCommented:
>  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
 
objectsCommented:
from the link you just posted:

"Despite these classes being in Netscape packages, Internet Explorer also supports them."
0
 
mte01Author Commented:
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
 
objectsCommented:
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
 
mte01Author Commented:
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
 
objectsCommented:
> 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
 
objectsCommented:
you need mayscript in your applet tag.
0
 
objectsCommented:
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
 
mte01Author Commented:
>> 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
 
objectsCommented:
> 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
 
objectsCommented:
> Can provide an example on how changeApplet would be called in this case?

something like:

context.showDocument(new URL("javascript:changeApplet();"));
0
 
mte01Author Commented:
>>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
 
objectsCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 18
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now