|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Figure 38.20 shows the JPanel that results from the code in Listing 38.10.
Remembering State with JToggleButtonJToggleButton is the parent class of both JCheckBox and JRadioButton, but its 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 thats locked in the Down state.) Managing TextSwings JTextComponent gives you more than youd expect from a simple field or text area. Its methods include
Figure 38.21 illustrates the inheritance hierarchy that derives from JTextComponent.
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.
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); } }
Giving Feedback with JProgressBarIn Chapter 39 well talk about starting up more than one independent thread of control. Sometimes youll 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 were done with the thread fThread = null; } } } }
TBackgroundPanel is a generic JPanel that can be used to display and control a background thread. The class has six instance variables:
|
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. |