|
Re: How to get values from a domino form into an applet
|
Posted: Jan 2, 2003 12:42 PM
|
|
I am assuming that you want to read values from hidden fields in HTML form from an applet. At the end , you can find the code for the applet and HTML file. I have demonstrated this with text input in a form. However, it will work for hidden fields too. 1. Within applet you need to get the reference of the window Javascript object. You can do this inside an applet as follows. netscape.javascript.JSObject win = netscape.javascript.JSObject.getWindow ( this );
In above code, "this" argument to getWindow() method refers to the applet itself. After this code is executed, then win is the reference to the JavaScript window object. Since you can call eval() method on window object, you do it as; String value = (String)win.eval ( "document.form1.name1" ) ;
This way you can read the values from form fields. 2. If you have any Javascript function then yu can call it using call() method on window object as: win.call ( "your_cunction_name", your_arguments_wrapped ) ;
Please visit this link for details:http://java.sun.com/products/plugin/1.3/docs/jsobject.html
//////////////////////////////////////////////////////// Applet code and HTML file are below... In applet code you would be interested only in void jButton1_actionPerformed(ActionEvent e) method code ///////////////////////////////////////////////////////
/******** Applet code ******************/
package applettojs;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class TestApplet extends JApplet {
private boolean isStandalone = false;
private JPanel jPanel1 = new JPanel();
private GridBagLayout gridBagLayout1 = new GridBagLayout();
private JLabel jLabel1 = new JLabel();
private JTextField formFieldName = new JTextField();
private JLabel jLabel2 = new JLabel();
private JTextField valueField = new JTextField();
private JButton jButton1 = new JButton();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public TestApplet() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setSize(new Dimension(400,300));
jPanel1.setLayout(gridBagLayout1);
jLabel1.setText("Form Field Name(Fully-qualified):");
formFieldName.setText("document.form1.name1");
jLabel2.setText("Field Value:");
valueField.setText(" ");
jButton1.setText("Get Value");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
jPanel1.add(formFieldName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
jPanel1.add(jLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
jPanel1.add(valueField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
jPanel1.add(jButton1, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
TestApplet applet = new TestApplet();
applet.isStandalone = true;
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}
void jButton1_actionPerformed(ActionEvent e) {
/* Get the value of the field from HTML Page */
// First get the browser window reference
netscape.javascript.JSObject win = netscape.javascript.JSObject.getWindow ( this );
// Get the fully-qualified name of form field
String fieldName = this.formFieldName.getText();
// Call the method eval on window to get the value from
// YOu can use call() method on win object to call any
// Javascript function. See documentation for call
String result = (String)win.eval(fieldName + ".value");
// Set the result in another field
this.valueField.setText(result);
}
}
/************Applet code ends here *****/ /******** HTMl File content ******************/ <html> <head> <title> HTML Test Page </title> </head> <body> <applet codebase = "." code = "applettojs.TestApplet.class" name = "TestApplet" width = "400" height = "300" hspace = "0" vspace = "0" align = "middle" > </applet> <br/></br>Below is form1 with two fields name1 and name2: <form name="form1"> Name1:<input type="text" name="name1" Value="105"> Name2:<input type="text" name="name2" Value="123"> </form> </body> </html>
/************HTML File content ends here *****/
|
|