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
</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><applet></code>
* element in the HTML page. It executes Javascript code
* equivalent to:
*
* <pre>
* document.getElementById(appletId).style.width
* = "<i>width</i>px"
* document.getElementById(appletId).style.height
* = "<i>height</i>px"
* </pre>
*
* @param width
* new width of the <code><applet></code>
* element in the HTML page
* @param height
* new height of the <code><applet></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><applet></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 <applet> 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><applet></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 <applet> element on the HTML page. Typically
* called by Javascript in the page containing this applet.
*
* @param appletId
* the id of the <applet> 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------