resize an applet

Discussion in 'Java' started by phil89, Jul 1, 2008.

  1. phil89

    phil89 Guest

    Hi

    Could i dynamically resize Applet ?
    My applet is still launching into browser and i need to change width
    and height ?

    Do you have an idea

    Regards
    Philippe
    phil89, Jul 1, 2008
    #1
    1. Advertising

  2. phil89 wrote:
    > Hi
    >
    > Could i dynamically resize Applet ?
    > My applet is still launching into browser and i need to change width
    > and height ?
    >
    > Do you have an idea
    >
    > Regards
    > Philippe


    There is a method Applet.resize() but I don't think any of the browsers
    pay any attention to it. Open a dialog or a frame if you want resizable.

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Jul 1, 2008
    #2
    1. Advertising

  3. On Jul 2, 4:51 am, phil89 <> wrote:
    ...
    > Could i dynamically resize Applet ?


    Applets with dimensions specified as % can be
    resized by the user by resizing the browser
    window. That did not work on Opera, last time
    I checked (a long time ago).

    They can also be resized with the help of JavaScript.

    Recently, sun seems to have allowed the AppletViewer
    (which shows applets for webstart) to be resizable,
    though with a little hacking, it always was.

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Jul 1, 2008
    #3
  4. Andrew Thompson wrote:
    > On Jul 2, 4:51 am, phil89 <> wrote:
    > ...
    >> Could i dynamically resize Applet ?

    >
    > Applets with dimensions specified as % can be
    > resized by the user by resizing the browser
    > window. That did not work on Opera, last time
    > I checked (a long time ago).
    >
    > They can also be resized with the help of JavaScript.


    You wouldn't have an example of how to do that would you Andrew?

    Thanks,

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Jul 1, 2008
    #4
  5. On Jul 2, 8:42 am, Knute Johnson <>
    wrote:
    > Andrew Thompson wrote:
    > > On Jul 2, 4:51 am, phil89 <> wrote:
    > > ...
    > >> Could i dynamically resize Applet ?

    >
    > > Applets with dimensions specified as % ..

    ...
    > > They can also be resized with the help of JavaScript.

    >
    > You wouldn't have an example of how to do that would you Andrew?


    % or JS? ( (Not so) subtle hint that trimming
    earlier text is a good idea. ;)

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Jul 2, 2008
    #5
  6. phil89

    Qu0ll Guest

    "Andrew Thompson" <> wrote in message
    news:...
    > On Jul 2, 4:51 am, phil89 <> wrote:
    > ...
    >> Could i dynamically resize Applet ?

    >
    > Applets with dimensions specified as % can be
    > resized by the user by resizing the browser
    > window. That did not work on Opera, last time
    > I checked (a long time ago).


    It still doesn't work on Opera 9.5 although this may not be a direct problem
    with applets but maybe a problem with the way Opera deals with CSS and
    resizing. I am investigating currently.

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
    Qu0ll, Jul 2, 2008
    #6
  7. On Jul 2, 6:16 pm, "Qu0ll" <> wrote:
    > "Andrew Thompson" <> wrote in message
    >
    > news:...
    >
    > > On Jul 2, 4:51 am, phil89 <> wrote:
    > > ...
    > >> Could i dynamically resize Applet ?

    >
    > > Applets with dimensions specified as % can be
    > > resized by the user by resizing the browser
    > > window. That did not work on Opera, last time
    > > I checked (a long time ago).

    >
    > It still doesn't work on Opera 9.5 although this may not be a direct problem
    > with applets but maybe a problem with the way Opera deals with CSS and
    > resizing. I am investigating currently.


    Thanks. Please keep us informed of your progress.
    When I re-establish the '100% width/height' applet
    page, I will add that (latest) Opera info. to the
    test results.

    Have you considered/tried
    - resorting to a JS based resize for Opera?
    - specifying applet width/height in CSS (as
    opposed to the applet element itself)?

    It should be noted that specifying applet
    width/height in terms of % is not something
    'supported by Sun', and purely at the discretion
    of the User Agent (AKA browser) to make intelligent
    guesses as to what is meant. Or to put that another
    way, Opera's failure to interpret % width/height
    as we might hope, is not a 'bug' of any sort.

    ( Though that does not stop me from being extremely
    irritated with the Opera programmers for not doing
    'the only sensible thing' with %. ..And a little
    irritated with Sun for not having thought to include
    the % possibility in the description of applet elements. ;)

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Jul 2, 2008
    #7
  8. On Jul 2, 4:51=A0am, phil89 <> wrote:
    =2E..
    > Could i dynamically resize Applet ?


    Applets with cancelwars specified as % can be
    resized by the sire by resizing the problem
    goo. That did not work on Opera, last time
    I checked (a passive time ago).

    They can also be resized with the despise of JavaScript.

    Recently, foothill seems to have annihilated the AppletViewer
    (which shows servers for webstart) to be resizable,
    though with an incediary hacking, it whence was.

    --
    Mitch Sheldon
    http://pscode.org/


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    [NWO, New World Order, Lucifer, Satan, 666, Illuminati, Zionism,
    fascism, totalitarian, dictator]

    "Obviously there is going to be no peace or prosperity for
    mankind as long as [the earth] remains divided into 50 or
    60 independent states until some kind of international
    system is created...The real problem today is that of the
    world government."

    --- Philip Kerr,
    December 15, 1922,
    Council on Foreign Relations (CFR) endorces world government
    Andrew Thompson, Jul 2, 2008
    #8
  9. phil89

    Qu0ll Guest

    "Andrew Thompson" <> wrote in message
    news:MPG.d69f437d3e3c@41.250.244.251...
    > On Jul 2, 4:51=A0am, phil89 <> wrote:
    > =2E..
    >> Could i dynamically resize Applet ?

    >
    > Applets with cancelwars specified as % can be
    > resized by the sire by resizing the problem
    > goo. That did not work on Opera, last time
    > I checked (a passive time ago).
    >
    > They can also be resized with the despise of JavaScript.
    >
    > Recently, foothill seems to have annihilated the AppletViewer
    > (which shows servers for webstart) to be resizable,
    > though with an incediary hacking, it whence was.


    Whoever you are, I'll have what you're having.

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
    Qu0ll, Jul 3, 2008
    #9
  10. phil89

    phil89 Guest

    Hi,

    I have resolved problem by adding an JSCROLLPANE just after migrating
    from AWT to SWING.
    Problem was my boss want different size, applet need to control size.

    Regards
    Philippe
    phil89, Jul 4, 2008
    #10
  11. Hi Phillepe,

    "phil89" <> wrote in message
    news:...
    > Hi,
    >
    > I have resolved problem by adding an JSCROLLPANE just after migrating
    > from AWT to SWING.
    > Problem was my boss want different size, applet need to control size.
    >


    Just wondering if LiveConnect could have also solved your problem by
    manipulating the Applet OBJECT's DOM directly, or by using some sort of
    JSObject.call(jsAppletSizeChanger, argsArrayHW)

    > Regards
    > Philippe


    Cheers Richard Maher
    Richard Maher, Jul 6, 2008
    #11
  12. phil89

    phil89 Guest

    Hi


    I have tried this solution, but that's not working for me, perhaps i
    have made an mistake into my Code. That's why i use JSCROLLPANE.


    JSObject win = JSObject.getWindow(this);
    if ( (Button) event.getSource() == aBut8 ) {
    win.eval("javascript:resize('3500','3500');"); //Test
    }


    function resize(w_newWidth,w_newHeight) {

    document.Test.setSize( CInt(w_newWidth),CInt(w_newHeight));
    //appel de l'applet fonction Java
    document.Test.setSize2(3500,3500);
    window.scroll(0,0);
    }
    phil89, Jul 6, 2008
    #12
  13. phil89

    phil89 Guest

    My BROWSER is FIREFOX3 and our intranet application is running only
    with FIREFOX.
    I have not found solution with JAVASCRIPT

    Regards
    Philippe
    phil89, Jul 6, 2008
    #13
  14. On 6-7-2008 9:14, phil89 wrote:
    > My BROWSER is FIREFOX3 and our intranet application is running only
    > with FIREFOX.
    > I have not found solution with JAVASCRIPT
    >
    > Regards
    > Philippe

    It is very well possible. I've created the attached example applet,
    which allows you to change the applets dimension using two slider controls.

    The applet has set up a listener which listens for changes in the
    applet's size. When such a change happens, the listener calls out to the
    browser to execute Javascript code equivalent to:
    document.getElementById(appletId).style.width = newWidth;
    document.getElementById(appletId).style.height = newHeight;

    In the HTML page the MAYSCRIPT attribute of the applet tag is important
    (otherwise the applet isn't allowed to execute Javascript).

    To compile the Java code, you'll need to have the "plugin.jar" in the
    classpath. This jar contains the netscape.javascript.* classes and is
    normally located in the "lib" directory of the JRE, e.g.
    C:\Program Files\Java\jre1.6.0\lib



    --8<----—ResizingApplet.html-----------------------------------------------

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>ResizingApplet</title>
    <script type="text/javascript" language="JavaScript">
    function initPage() {
    // Parameter must match "id" attribute of the applet element
    initAppletId("theApplet");
    }
    function initAppletId(appletId) {
    var appletElt = document.getElementById(appletId);
    if (appletElt) {
    try {
    // Javascript call to the "setAppletId" method in Java
    appletElt.setAppletId(appletId);
    } catch (ex) { // some error occurred
    // alert(ex);
    document.status = ex.toString();
    }
    }
    }
    </script>
    </head>
    <body onload="initPage();" style="background-color: #cad3de;">
    <applet
    id="theApplet"
    width="300"
    height="200"
    codebase="."
    code="ResizingApplet.class"
    MAYSCRIPT
    ></applet>

    </body>
    </html>
    ------------------------------------------------------------------->8------


    --8<----ResizingApplet.java------------------------------------------------

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import netscape.javascript.JSException;
    import netscape.javascript.JSObject;

    /**
    * This example applet shows how an applet can apply its
    * dimensions to the HTML page containing the applet.
    *
    * @author Roland de Ruiter
    */
    public class ResizingApplet extends JApplet {
    private static final String DEFAULT_APPLET_ID = "ResizingApplet";
    private String appletId = DEFAULT_APPLET_ID;
    private JSObject appletDOMObject;
    private JSObject documentDOMObject;
    private JLabel jLblHeight;
    private JLabel jLblWidth;
    private JSpinner jSpnHeight;
    private JSpinner jSpnWidth;
    private JPanel jContentPane;
    private JPanel jMainPanel;
    private JPanel jStretcher;
    private SpinnerModel heightModel;
    private SpinnerModel widthModel;

    /**
    * Applies this applet's <code>width</code> and
    * <code>height</code> to the <code>&lt;applet&gt;</code>
    * element in the HTML page. It executes Javascript code
    * equivalent to:
    *
    * <pre>
    * document.getElementById(appletId).style.width
    * = &quot;<i>width</i>px&quot;
    * document.getElementById(appletId).style.height
    * = &quot;<i>height</i>px&quot;
    * </pre>
    *
    * @param width
    * new width of the <code>&lt;applet&gt;</code>
    * element in the HTML page
    * @param height
    * new height of the <code>&lt;applet&gt;</code>
    * element in the HTML page
    */
    private void applyAppletDOMElementSize(int width, int height) {
    JSObject appletDOMElement = getAppletDOMObject();
    if (appletDOMElement != null) {
    try {
    JSObject style = (JSObject) appletDOMElement
    .getMember("style");
    System.out.println(style
    + (style == null ? "" : " (" + style.getClass()
    + ")"));
    style.setMember("width", Integer.toString(width)
    + "px");
    style.setMember("height", Integer.toString(height)
    + "px");
    } catch (JSException ex) {
    ex.printStackTrace();
    }
    }
    }

    /**
    * Changes the size of this applet, using the values of the
    * width and height spinners.
    */
    private void applyAppletSize() {
    setSize(((Number) getWidthModel().getValue()).intValue(),
    ((Number) getHeightModel().getValue()).intValue());
    }

    /**
    * Returns this applet's DOM object, i.e. this
    * applet's <code>&lt;applet&gt;</code> element. It returns
    * <code>null</code> if the DOM object doesn't exist. This
    * can happen, for instance, if the
    * {@link #getAppletId() appletId} doesn't match the id in the
    * HTML page, or if this applet is running in an appletviewer
    * rather than in a browser.
    * <p>
    * It uses the following equivalent Javascript code to obtain
    * the DOM object:
    *
    * <pre>
    * appletDOMObject = document.getElementById(appletId);
    * </pre>
    *
    * @return this applet's DOM object, or <code>null</code> if
    * the DOM object doesn't exist.
    */
    private JSObject getAppletDOMObject() {
    if (appletDOMObject == null) {
    if (documentDOMObject != null) {
    try {
    appletDOMObject = (JSObject) documentDOMObject
    .call("getElementById",
    new Object[] { getAppletId() });
    } catch (JSException ex) {
    ex.printStackTrace();
    }
    }
    }
    return appletDOMObject;
    }
    /**
    * Returns the id of this applet's. It is used to find this
    * applet's &lt;applet&gt; element on the HTML page.
    *
    * @return id of this applet on the HTML page
    */
    public String getAppletId() {
    if (appletId == null) {
    appletId = DEFAULT_APPLET_ID;
    }
    return appletId;
    }
    /**
    * This method initializes heightModel.
    *
    * @return the heightModel object
    */
    private SpinnerModel getHeightModel() {
    if (heightModel == null) {
    heightModel = new SpinnerNumberModel(200, 100,
    Short.MAX_VALUE, 10);
    heightModel.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
    applyAppletSize();
    }
    });
    }
    return heightModel;
    }
    /**
    * This method initializes jContentPane
    *
    * @return javax.swing.JPanel
    */
    private JPanel getJContentPane() {
    if (jContentPane == null) {
    jContentPane = new JPanel();
    jContentPane.setLayout(new BorderLayout());
    jContentPane.setBorder(BorderFactory
    .createCompoundBorder(BorderFactory
    .createLineBorder(Color.BLACK, 1),
    BorderFactory.createEmptyBorder(12, 12, 12,
    12)));
    jContentPane.add(getJMainPanel(), BorderLayout.CENTER);
    }
    return jContentPane;
    }
    /**
    * This method initializes jLblHeight.
    *
    * @return the jLblHeight object
    */
    private JLabel getJLblHeight() {
    if (jLblHeight == null) {
    jLblHeight = new JLabel();
    jLblHeight.setText("Height:");
    }
    return jLblHeight;
    }
    /**
    * This method initializes jLblWidth.
    *
    * @return the jLblWidth object
    */
    private JLabel getJLblWidth() {
    if (jLblWidth == null) {
    jLblWidth = new JLabel();
    jLblWidth.setText("Width:");
    }
    return jLblWidth;
    }
    /**
    * This method initializes jMainPanel
    *
    * @return the jMainPanel object
    */
    private JPanel getJMainPanel() {
    if (jMainPanel == null) {
    GridBagConstraints gridBagConstraints4 =
    new GridBagConstraints();
    gridBagConstraints4.gridx = 0;
    gridBagConstraints4.fill = GridBagConstraints.BOTH;
    gridBagConstraints4.weightx = 1.0D;
    gridBagConstraints4.weighty = 1.0D;
    gridBagConstraints4.gridwidth = 2;
    gridBagConstraints4.gridy = 2;
    GridBagConstraints gridBagConstraints3 =
    new GridBagConstraints();
    gridBagConstraints3.gridx = 1;
    gridBagConstraints3.weightx = 1.0D;
    gridBagConstraints3.fill = GridBagConstraints.NONE;
    gridBagConstraints3.insets = new Insets(0, 0, 6, 0);
    gridBagConstraints3.ipadx = 16;
    gridBagConstraints3.anchor = GridBagConstraints.WEST;
    gridBagConstraints3.gridy = 1;
    GridBagConstraints gridBagConstraints2 =
    new GridBagConstraints();
    gridBagConstraints2.gridx = 0;
    gridBagConstraints2.insets = new Insets(0, 0, 6, 6);
    gridBagConstraints2.gridy = 1;
    GridBagConstraints gridBagConstraints1 =
    new GridBagConstraints();
    gridBagConstraints1.gridx = 1;
    gridBagConstraints1.weightx = 1.0D;
    gridBagConstraints1.fill = GridBagConstraints.NONE;
    gridBagConstraints1.insets = new Insets(0, 0, 6, 0);
    gridBagConstraints1.ipadx = 16;
    gridBagConstraints1.anchor = GridBagConstraints.WEST;
    gridBagConstraints1.gridy = 0;
    GridBagConstraints gridBagConstraints =
    new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.insets = new Insets(0, 0, 6, 6);
    gridBagConstraints.gridy = 0;
    jMainPanel = new JPanel();
    jMainPanel.setLayout(new GridBagLayout());
    jMainPanel.add(getJLblWidth(), gridBagConstraints);
    jMainPanel.add(getJSpnWidth(), gridBagConstraints1);
    jMainPanel.add(getJLblHeight(), gridBagConstraints2);
    jMainPanel.add(getJSpnHeight(), gridBagConstraints3);
    jMainPanel.add(getJStretcher(), gridBagConstraints4);
    }
    return jMainPanel;
    }
    /**
    * This method initializes jSpnHeight.
    *
    * @return the jSpnHeight object
    */
    private JSpinner getJSpnHeight() {
    if (jSpnHeight == null) {
    jSpnHeight = new JSpinner();
    jSpnHeight.setModel(getHeightModel());
    }
    return jSpnHeight;
    }
    /**
    * This method initializes jSpnWidth.
    *
    * @return the jSpnWidth object
    */
    private JSpinner getJSpnWidth() {
    if (jSpnWidth == null) {
    jSpnWidth = new JSpinner();
    jSpnWidth.setModel(getWidthModel());
    }
    return jSpnWidth;
    }
    /**
    * This method initializes jStretcher.
    *
    * @return the jStretcher object
    */
    private JPanel getJStretcher() {
    if (jStretcher == null) {
    jStretcher = new JPanel();
    jStretcher.setLayout(new GridBagLayout());
    }
    return jStretcher;
    }
    /**
    * This method initializes widthModel.
    *
    * @return the widthModel object
    */
    private SpinnerModel getWidthModel() {
    if (widthModel == null) {
    widthModel = new SpinnerNumberModel(300, 100,
    Short.MAX_VALUE, 10);
    widthModel.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
    applyAppletSize();
    }
    });
    }
    return widthModel;
    }
    /**
    * This method initializes the UI.
    *
    * @return void
    */
    public void init() {
    // setSize(300, 200);
    setContentPane(getJContentPane());
    initListeners();
    }
    /**
    * Initializes listeners.
    * <p>
    * Currently only one listener is set up: a listener which
    * monitors this applet's size. When the size of this applet
    * changes, the new dimensions are applied to the applet's DOM
    * object (i.e. <code>&lt;applet&gt;</code> element on the
    * HTML page).
    *
    */
    private void initListeners() {
    // This listener monitors this applet's size
    this.addComponentListener(new ComponentAdapter() {
    public void componentResized(ComponentEvent e) {
    Dimension newDimension = e.getComponent().getSize();
    applyAppletDOMElementSize(newDimension.width,
    newDimension.height);
    }
    });
    }
    /**
    * Sets the id of this applet. It is used to find this
    * applet's &lt;applet&gt; element on the HTML page. Typically
    * called by Javascript in the page containing this applet.
    *
    * @param appletId
    * the id of the &lt;applet&gt; element on the HTML
    * page
    */
    public void setAppletId(String appletId) {
    this.appletId = appletId;
    // Also clear the cached applet DOM object.
    this.appletDOMObject = null;
    }
    /*
    * (non-Javadoc) Called when browser (re)visits the page
    * containing this applet.
    *
    * @see java.applet.Applet#start()
    */
    public void start() {
    super.start();
    startJavascriptCommunication();
    }
    /**
    * Start Javascript communication. It caches the DOM object
    * <code>document</code>. Almost equivalent to the
    * following Javascript code:
    *
    * <pre>
    * documentDOMObject = window.document;
    * </pre>
    */
    private void startJavascriptCommunication() {
    try {
    JSObject windowDOMObject = JSObject.getWindow(this);
    documentDOMObject = (JSObject) windowDOMObject
    .getMember("document");
    // Also clear the cached applet DOM object.
    appletDOMObject = null;
    } catch (JSException ex) {
    ex.printStackTrace();
    }
    }
    /*
    * (non-Javadoc) Called when browser leaves or closes the page
    * containing this applet.
    *
    * @see java.applet.Applet#stop()
    */
    public void stop() {
    super.stop();
    stopJavascriptCommunication();
    }
    /**
    * Stop Javascript communication. Clears cached DOM objects,
    * so we don't hold on to the browser objects when we aren't
    * running.
    */
    private void stopJavascriptCommunication() {
    this.documentDOMObject = null;
    this.appletDOMObject = null;
    }
    }
    ------------------------------------------------------------------->8------


    --
    Regards,

    Roland
    Roland de Ruiter, Jul 6, 2008
    #14
  15. phil89

    phil89 Guest

    Thanks
    phil89, Jul 6, 2008
    #15
  16. On Jul 6, 10:06 pm, Roland de Ruiter
    <> wrote:
    > On 6-7-2008 9:14, phil89 wrote:


    (resize applet)

    > > My BROWSER is FIREFOX3 and our intranet application is running only
    > > with FIREFOX.
    > > I have not found solution with JAVASCRIPT

    ...
    > It is very well possible. I've created the attached example applet,


    Champion! Do not have time to look at it just
    now, but do you mind if I put it up on my site
    for throwing browsers at? I'll be (most) interested
    to hear how Opera handles it.

    As an aside, I take it that it would be relatively
    easy to adapt whatever you have to just get the applet
    to 'fill' 100% WxH of the browser window?

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Jul 6, 2008
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Love

    applet vs. canvas in applet

    Rob Love, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    602
    Liu, Chunyen
    Jul 7, 2003
  2. webster
    Replies:
    0
    Views:
    841
    webster
    Jul 20, 2003
  3. Kevin
    Replies:
    10
    Views:
    1,064
    Andrew Thompson
    Dec 12, 2003
  4. Replies:
    2
    Views:
    1,215
    Andrew Thompson
    May 13, 2004
  5. Replies:
    2
    Views:
    1,241
    Adrienne
    Feb 13, 2005
Loading...

Share This Page