|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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 TBackgroundPanels 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 doesthat 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 dont 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.
Adding Toolbars and ToolTipsIts 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.
The Long-Awaited Tabbed PaneThe 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 paneearly 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 ComponentsSwing 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 AWTs 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 eventsjust add a ListSelectionListener.
Writing Swing AppletsSwing introduces a new classJAppletthat is derived from Applet. Many of the new methods youll find in JApplet have to do with accessibility for people with disabilities. The other new capability is called JRootPane.
|
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. |