Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Threads and applets and visual controls

The last part in the series explores<br> reading multiple data channels

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Before jumping into this final column on threads and inter-applet communication let's review a bit of what we've covered so far. In "Synchronizing threads in Java" I looked at synchronizing two or more threads using the wait and notify methods of the Object class. In "Using threads in Java, Part II" I took the synchronized threads and created an in-memory communication channel so that two or more threads could exchange object references. Then in "Using communication channels in applets, Part 3" I applied these communication channels to co-operating applets that were laid out in an HTML page by a Java-aware browser. These applets combined to create an active HTML page that implemented a simple temperature conversion applet.

In this last column on threads and interapplet communication I'll look at a couple issues associated with this approach, and in particular I'll discuss how a layer can be created on top of the existing DataChannel design to allow multiple DataChannels to feed into a single DataChannel. The source code is also online and available for your use, in either an elaborate form, or as a tar or zip archive.

Next month I'll pick on another oft-misunderstood aspect of the Java system, class loaders.

Choice control

The choice control is a common and useful control for applications. You use these controls when you want an application to accept one of several choices and the choices are well specified. Examples of such choices are "true or false" and "strongly agree, agree, neutral, disagree, strongly disagree." To implement a choice control, you must specify two properties to be satisfied:

  • The control must know when it is selected.
  • All of the controls in the group must know when they are not selected.


We implement this easily with a DataChannel.

The skeleton of the OptionButton control is as follows:

 1  public class OptionButton extends Applet implements Runnable {
 2      public void init() {
 3      new DataChannel(getparameter("datachannel");
 4      state = false;
 5      }
 6      public void paint(Graphics g) {
 7      ... display my label and my choice ring ...
 8      }
 9      public void start() { ... create the data channel ... }
10      public void stop() { ... release the data channel ... }
11      public void run() {
12      while (thread == currentthread) {
13          value = getValue();
14          current_state = value == myValue;
15          repaint();
16     }
17      }
18      public boolean mouseUp(...) { sendValue(myID); }
19  }


As you can read in the code, the applet is quite simple. The magic is taken care of by the DataChannel. The basic applet knows only two things -- how to render itself in the checked and unchecked state -- and when it gets clicked it sends its value out to its DataChannel, which is the same channel that it is monitoring. When an option button receives a value from its DataChannel, if the value is equivalent to its own value, it sets its checked state to true and repaints itself.

Can you see how it would be modified to be a non-exclusive choice control? Certainly the boundary condition of an OptionButton with no one else in the group would get halfway there. But recall that the way the button gets unset is that some other choice is set. Obviously the mouseUp method would have to implement a toggle rather than a single set semantic. I'll leave it as an exercise for you to create this class (although there is a version in the sources), and when your write it, change the box shape from round to square. This will give users the idea that this is not a single-choice option.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources