home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Figure 38.20 shows the JPanel that results from the code in Listing 38.10.


FIGURE 38.20  Use a ButtonGroup to implement a set of radio buttons.

Remembering State with JToggleButton

JToggleButton is the parent class of both JCheckBox and JRadioButton, but it’s a concrete class that you can use in your design. When unselected, a JToggleButton is indistinguishable from a JButton. When someone clicks it, it goes to its selected state and stays there. (By default, it looks like a push button that’s locked in the “Down” state.)

Managing Text

Swing’s JTextComponent gives you more than you’d expect from a simple field or text area. Its methods include

  copy()
  cut()
  paste()
  getSelectedText()
  getSelectionStart()
  getSelectionEnd()
  getText()
  setText()
  setEditable()
  setCaretPosition()

Figure 38.21 illustrates the inheritance hierarchy that derives from JTextComponent.


FIGURE 38.21  Each member of the JTextComponent family supports the methods of a simple text editor.

JTextField and JTextArea resemble their AWT counterparts, but JTextPane is something new. It implements a complete text editor; you can format text and embed images. Words will wrap where you expect them to, based on their current font, size, and style. TProgressNotePanel, a class used in a pharmacy application, is shown in Listing 38.11. Figure 38.22 shows the TProgressNotePanel itself.


When a user enters a password or other sensitive information, the information should not become visible on the screen. Use JPasswordField instead of JTextField to ensure privacy. Use setEchoChar() if you want to override the default echo character, asterisk (*).

Listing 38.11 TProgressNotePanel.javaNurses Enter Progress Notes to Report Significant Events in the Care of Their Patients


public class TProgressNotePanel extends javax.swing.JPanel
{
  public TProgressNotePanel() {
    setLayout(new java.awt.BorderLayout());
    setPreferredSize(new java.awt.Dimension(400,400));
    javax.swing.JTextPane theText = new javax.swing.JTextPane();

    javax.swing.text.MutableAttributeSet theAttributes = 
      new javax.swing.text.SimpleAttributeSet();
    javax.swing.text.StyleConstants.setFontFamily(theAttributes, 
                                                     “Serif”);
    javax.swing.text.StyleConstants.setFontSize(theAttributes, 18);
    javax.swing.text.StyleConstants.setBold(theAttributes, true);
    theText.setCharacterAttributes(theAttributes, false);

    add(theText, java.awt.BorderLayout.CENTER);
  }
}


FIGURE 38.22  You can get a full text editor in only nine lines of code.

Giving Feedback with JProgressBar

In Chapter 39 we’ll talk about starting up more than one independent thread of control. Sometimes you’ll want a thread running in the background while the user goes on with other work. You can put up a progress bar to report on the progress in that thread and, if you like, enable the user to control that thread. Listing 38.12 and Figure 38.23 show one way to use a JProgressBar.

• To learn more about threads, see “Adding Animation” on p. 1104.

Listing 38.12 TBackgroundPanel.javaYou Can Use a JProgressBar to Report the Progress of a Thread


import javax.swing.*;
public class TBackgroundPanel extends JPanel {
  private Thread fThread;
  private Object fLock;
  private boolean fNeedsToStop = false;
  private JProgressBar fProgressBar;
  private JButton fStartButton;
  private JButton fStopButton;

  public TBackgroundPanel() {
    fLock = new Object();
    setLayout(new java.awt.BorderLayout());
    add(new JLabel(“Status”), java.awt.BorderLayout.NORTH);
    fProgressBar = new JProgressBar();
    add(fProgressBar, java.awt.BorderLayout.CENTER);
    JPanel theButtons = new JPanel();

    fStartButton = new JButton(“Start”);
    fStartButton.setBackground(java.awt.SystemColor.control);
    theButtons.add(fStartButton);
    fStartButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent e) {
        startTheThread();
      }
    });
    fStopButton = new JButton(“Stop”);
    fStopButton.setBackground(java.awt.SystemColor.control);
    theButtons.add(fStopButton);
    fStopButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent e) {
        stopTheThread();
      }
    });
    add(theButtons, java.awt.BorderLayout.SOUTH);
  }

  public void startTheThread() {
    if (fThread == null)
      fThread = new TBackgroundThread();
    if (!fThread.isAlive())
    {
      fNeedsToStop = false;
      fThread.start();
    }
  }

  public void stopTheThread() {
    synchronized(fLock) {
      fNeedsToStop = true;
      fLock.notify();
    }
  }

  // inner class, so it has access to private members of panel
  class TBackgroundThread extends Thread {
    public void run() {
      // run at a low priority; after all, we _
      // are_ a background thread
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
      int theMinimum = 0;
      int theMaximum = 100;
      fProgressBar.setValue(theMinimum);
      fProgressBar.setMinimum(theMinimum);
      fProgressBar.setMaximum(theMaximum);
      for (int i=0; i<theMaximum; i++) {
        fProgressBar.setValue(i);

        // do the real work of the background thread
        // here

        synchronized(fLock) {
          if (fNeedsToStop) 
            break;
          try {
            fLock.wait(100);
          } catch (InterruptedException e) {
            // ignore the exception
          }
        }
      }
      // clue the garbage collector that we’re done with the thread
      fThread = null;
    }
  }
}

}


FIGURE 38.23  For a nice touch, enable the user to start and stop the work of the background thread.

TBackgroundPanel is a generic JPanel that can be used to display and control a background thread. The class has six instance variables:

  Thread fThread—The background thread itself; see “Adding Animation” in Chapter 39 to learn more about threads.
  Object fLock—A simple object used to synchronize the foreground and background threads.
  boolean fNeedsToStop—A bit of shared data that tells the background thread that the foreground thread wants it to stop.
  JProgressBar fProgressBar—The Swing progress bar.
  JButton fStartButton and JButton fStopButton—The controls to start and stop the background thread.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.