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


The TBackgroundPanel constructor builds the user interface, including the pair of buttons that communicate with the background thread. The listeners are designed to start and stop the thread by calling TBackgroundPanel’s methods startTheThread() and stopTheThread(). The class TBackgroundThread is an inner class, so it has access to all the instance variables of TBackgroundPanel. Like most threads, it has only one method: run().

When the user clicks fStartButton, startTheThread() instantiates a new TBackgroundThread and tells it to start(). When the thread starts, its run() method immediately sets the priority to the minimum value; the user interface has a higher priority, so the application feels responsive to the user. The TBackgroundThread sits in a tight loop doing whatever work it does—that work has been left as a comment in this version. You could download a file, query a database, or do anything else that takes so much time that you don’t want the user to have to wait.

As it runs, TBackgroundThread continually reports its status to fProgressBar. Then it checks the state of fNeedsToStop. If the user has clicked fStopButton, then fNeedsToStop is true and run() breaks out of its loop.


The boolean variable fNeedsToStop is shared between the two threads. If you’re not careful, it’s possible that both threads could be using the variable at once. To prevent this, we use the Object fLock as a semaphore so that only one thread can work with fNeedsToStop at a time.

Adding Toolbars and ToolTips

It’s easy to add toolbars and tooltips to your Swing components. If you have a JButton named fButton, just write

fButton.setToolTipText(“This is the tooltip”);

to add a ToolTip to the button. You can use this technique on any JComponent.

A JToolBar is simply another JComponent. You can write code such as

JToolBar theToolBar = new JToolBar();
JButton aButton = new JButton(“One”);
theToolBar.add(aButton);
JButton anotherButton = new JButton(“Two”);
theToolBar.add(anotherButton);

to build a toolbar.


You can add JButtons and other components to your toolbar; use an ActionListener to handle mouse clicks, the same as you would with any other button. ActionListeners are part of the delegation event model described earlier in this chapter in the section, “Using the Abstract Windowing Toolkit.”

The Long-Awaited Tabbed Pane

The Windows32 user interface (implemented in Windows95 and Windows NT 4.0) included a tabbed pane, which is commonly used throughout Windows applications. Unfortunately, the AWT interface did not include a tabbed pane—early Java programmers often cobbled one together by using the CardLayout.

Now, with Swing, we get our own JTabbedPane. You can add one to a BorderLayout with just a few lines of code, as shown in Listing 38.13.

Listing 38.13 TTabbedPanel.javaAdd Tabs to a JTabbedPanel


import javax.swing.*;
public class TTabbedPanel extends JPanel {
{
  private JTabbedPane fTabbedPane;
  public TTabbedPanel() {
    setLayout (new java.awt.BorderLayout());
    fTabbedPane = new JTabbedPane();
    fTabbedPane.addTab(“One”, null, makePane(“One”));
    fTabbedPane.addTab(“Two”, null, makePane(“Two”));
    fTabbedPane.addTab(“Three”, null, makePane(“Three”));
    fTabbedPane.setSelectedIndex(0);
    add (fTabbedPane, java.awt.BorderLayout.CENTER);
  }
  protected void makePane(String theString) {
    // customize makePane to display the exact info you want
    // on each panel
    JPanel thePanel = new JPanel();
    thePanel.setBackground(SystemColor.control);
    thePanel.add(new JLabel(theString));
    return thePanel;
  }
}

Other Swing Components

Swing is a huge component library; in addition to the components described here, JSliders, JComboBoxes, JLists, and many other widgets are included. A JSlider resembles a JScrollBar, but you can add major and minor tick marks and display a Border around the slider. A JComboBox resembles the AWT’s Choice component but has more capability. (You can use a JComboBox to supply a list of default choices, for example, and then enable the users to enter their own values if none of the defaults are appropriate.) Use JList the same as you would use List in the AWT. You can listen for ListSelectionEvents the same as you do other events—just add a ListSelectionListener.


Unlike its AWT counterpart, JList doesn’t support scrolling directly. That’s not a problem, though—just add it to a ScrollPane or JScrollPane to restore that capability.

Writing Swing Applets

Swing introduces a new class—JApplet—that is derived from Applet. Many of the new methods you’ll find in JApplet have to do with accessibility for people with disabilities. The other new capability is called JRootPane.


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.