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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs