advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

JavaBoutique : Articles :

An Illustration of these Techniques

Contents
Introduction
Intro pt 1
Intro pt 2
Intro pt 3
Intro pt 4
An Illustration of these Techniques
An Applet Example
An Active X Example
A Pseudo-Constructor Example
Pseudo-Constructor pt 2
Applet Persists Indefinitely; Breaks into JavaScript
Persistence
Conclusion

3. A Pseudo-Constructor Example

Notice, in this code, that neither applet ‘app’ nor Frame ‘ShopFrame’ contain any variables at all. All variables are contained in the class ShopPanel. The applet, in its method init(), refers to ShopPanel, which in and of itself forces all of ShopPanel’s declared variables, all of which here are static, to be instantiated. In particular, shopFrame and counter are formed. The applet’s life cycle then calls init() in the applet, which transfers the call to init() in ShopPanel. ShopPanel’s init() calls formPanelElements() in ShopPanel, which sets the characteristics of the already instantiated objects in ShopPanel. The next call is to shopFrame, which now exists, and has defined characteristics, and in particular to its method formPanel(), which takes the frame elements defined in ShopPanel, and sets them into shopFrame, which is also defined in ShopPanel.

Create the following applet:

import java.applet.*;

public class app extends Applet{
  public void init(){
    ShopPanel.init();
  }
}

import java.awt.*;
import java.awt.event.*;

public class ShopFrame extends Frame implements ActionListener{
  void formPanel()  {
    add(ShopPanel.counter);
    ShopPanel.counter.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e){
    ShopPanel.increment();
  }
}

import java.awt.*;

//notice that it can be public class ShopPanel,
//not just abstract class ShopPanel. We will discuss this.
public class ShopPanel{
  static ShopFrame shopFrame = new ShopFrame();
  static Button counter = new Button("Click");
  static int r = 0;

  static void formPanelElements(){
    shopFrame.setBounds(200,200,100,100);
  }

  static void init(){
    formPanelElements();
    shopFrame.formPanel();
    shopFrame.show();
  }

  static void increment(){
    counter.setLabel(new Integer(r).toString());
    r++;
  }
}

Place it into the following HTML page:

<HTML>
<HEAD>
</HEAD>
<BODY>
  <applet code=app.class name=app width=1
    height=1  VIEWASTEXT>
  </applet>
</BODY>
</HTML>

Event handling can now be set up, in shopFrame’s formPanel(). Events are caught in shopFrame, but the event handlers are all located as static methods in ShopPanel. Notice that this structure is not dependent upon the applet at all – there is a call from ‘app’ to ShopPanel, but nothing from ShopPanel back to applet ‘app’. The frame shopFrame, which is created indirectly by the applet through its reference to ShopPanel, in fact knows absolutely nothing about its parent – it is aware only of the class ShopPanel, which is not being instantiated. When page transfers are made, it is the applet which is altered from active to inactive – notice how short it is, and how little time will be required to reload it. The frame doesn’t change at all; it simply sits there, and watches as you browse.

Let’s look now at what our example does. The applet indirectly creates a frame. Clicking this frame increments a number (there may be problems in some browsers – we are not including minor tweaking elements, composed solely of standard JAVA, that solve these 'glitches'). Moving away from the HTML page does not destroy the said frame – rather, it continues to operate as a kind of 'terminate and stay resident' web program. Notice that the said frame now maintains its position from page to page automatically, without any user intervention, unlike the previous two examples.

Next ->


Lane Friesen

lanelise@dowco.com

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs