Pass parameters to JApplet

Swing Designer allows you to quickly create the frames, panels, dialogs, applets and other UI elements that comprise Java Swing applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Pass parameters to JApplet

Postby Mikey » Tue Jun 08, 2004 3:54 am

Hi,
Is there a way I can pass values from HTML to a Swing Designer generated applet? (Using getParameter().)

It seems that getParameter doesn't work unless it's in the init() method of the applet. So I tried this:

Code: Select all
import javax.swing.JApplet;
import java.awt.BorderLayout;
import javax.swing.JTextField;
// Created on 08-Jun-2004
// (c) Mike Kelly

// @author Mike Kelly
public class BBApplet3 extends JApplet {
   
   private String uN = new String();
   private String uR = new String();
   
   public void init(){
      uN = getParameter("userName");
      uR = getParameter("userRole");
   }
   
   public BBApplet3() {
      super();
      
      getContentPane().setLayout(new BorderLayout());
      {
         final JTextField textField = new JTextField();
         getContentPane().add(textField, BorderLayout.NORTH);
         textField.setText(uN);
      }
      {
         final JTextField textField = new JTextField();
         getContentPane().add(textField, BorderLayout.SOUTH);
         textField.setText(uR);
      }
      //
   }
}


The fields passed by HTML don't show up. Is there a way to do this without breaking the Designer functionality?
Thanks,
M
Mikey
 

Re: Pass parameters to JApplet

Postby Eric Clayberg » Tue Jun 08, 2004 12:49 pm

Mikey wrote:The fields passed by HTML don't show up. Is there a way to do this without breaking the Designer functionality?

I don't understand your question. What Designer functionality are your breaking? I tried your code in Designer and it looked fine on my end.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Postby Mikey » Wed Jun 09, 2004 12:39 am

Hi Eric,
The problem was that it didn't work. For me at least.

The code above doesn't cause any problems with Designer, but when I pass the values from the HTML page to the applet, they are not displayed in the textfields.

They are displayed if I change the structure of the auto-generated java for a Designer JApplet. For example if I dispense with the BBApplet3 constructor and do everything in init().

Code: Select all
import javax.swing.JApplet;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JButton;
// Created on 08-Jun-2004
// (c) Mike Kelly linst.ac.uk

// @author Mike Kelly
public class BBApplet5 extends JApplet {
   
   private JTextField textField;
   private String uN = new String();
   private String uR = new String();
   
   public void init(){
      uN = getParameter("userName");
      uR = getParameter("userRole");
      
      getContentPane().setBackground(Color.ORANGE);
      
      getContentPane().setLayout(new BorderLayout());
      {
         final JPanel panel = new JPanel();
         getContentPane().add(panel);
      {
         final JTextField textField = new JTextField();
         textField.setPreferredSize(new Dimension(200, 20));
         panel.add(textField);
         textField.setText(uN);
      }
      {
         final JTextField textField = new JTextField();
         textField.setPreferredSize(new Dimension(200, 20));
         panel.add(textField);
         textField.setText(uR);
      }
         {
            final JButton button = new JButton();
            panel.add(button);
            button.setText("get data");
         }
      }
      {
         final JPanel panel = new JPanel();
         getContentPane().add(panel, BorderLayout.SOUTH);
         {
            textField = new JTextField();
            panel.add(textField);
            textField.setText("this is where the data goes...");
         }
      }
   }
}


I thought this would cause problems with Designer's drag and drop GUI creation, but it doesn't seem to! :)
So my revised question is, is it OK to completely revise the auto-generated structure of Designer-made classes without breaking the drag and drop functionality of Designer?
Thanks,
Mike
Mikey
 

Postby Eric Clayberg » Wed Jun 09, 2004 3:43 am

Mikey wrote:The problem was that it didn't work. For me at least.
The code above doesn't cause any problems with Designer, but when I pass the values from the HTML page to the applet, they are not displayed in the textfields.

Since the init() code runs after the constructor, you would probably just need to set the text of the two widgets there rather than in the constructor. In order to do that, you would want to convert both text widgets from local variables into fields (one of the buttons above the property pane will do that for you).

Mikey wrote:I thought this would cause problems with Designer's drag and drop GUI creation, but it doesn't seem to! :)
So my revised question is, is it OK to completely revise the auto-generated structure of Designer-made classes without breaking the drag and drop functionality of Designer?

Yes. Designer can generally survive most refactorings or other hand-made modifications. It does not lock you into any specific template or code generation style (it supports a wide range of styles via its Code Generation prefs). Changing the name of the method that creates the widgets wouldn't both it at all. You can also add your own code interspersed with the code that Designer generates and it won't mind. Unlike most other GUI builders, you don't need to worry about the tool regenerating the code and blowing away your changes.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to Swing Designer

Who is online

Users browsing this forum: Google [Bot] and 1 guest