Last updated 2004-06-30 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : S words : Swing.
The biggest gotcha is that you no longer add directly to your Swing containers. Any content inside a JFrame should be placed inside its contentPane. You must do a:
Container pane = swingContainer.getContentPane();
and add to it. Unfortunately the add method is still defined for the Swing Containers to entrap the unwary.
In Swing, override JComponent.paintComponent instead of paint.
Swing's JComboBox is misnamed. It does not let you select a combination of choices. It is a combination Choice and write-in TextField allowing the user to add new possible choices on the fly. Swing classes live in package javax.swing.* . They used to live in com.sun.java.swing.*.
FormattedTextField does not verify input the way you might expect. To learn how to do that you need to read up on custom Documents and InputVerifier.
There are a few exceptions to this general rule, most notably it is safe to call repaint and revalidate, JTextComponent.setText and JTextArea.append from any thread.
| Equivalent Components in AWT and Swing | |
|---|---|
| Applet | JApplet |
| Button | JButton |
| Canvas | JPanel |
| Checkbox | JCheckBox
or JRadioButton |
| Choice | JComboBox |
| Color | Color |
| none | JColorChooser |
| Component | JComponent |
| Container | Container |
| Dialog | JDialog or JOptionPane |
| FileDialog | JFileChooser |
| none | JFormattedTextField |
| Frame | JFrame |
| Label | JLabel |
| List | JList + JScrollPane |
| LayoutManager | LayoutManager |
| Menu | JMenu |
| Panel | JPanel |
| ScrollPane | JScrollPane |
| none | JSlider |
| none | JSpinner |
| TextArea | JTextArea |
| TextField | JTextField |
| Window | JWindow |
When the following table suggests a Component is for AWT but not Swing, it means there is an improved Swing component. In theory you could use the old AWT component, but normally you would not want to.
When it asks if Components are visible, it means, are the visible by default when first created. Obviously you set do a setVisible( true ) later. The rule of thumb is, freestanding windows start out invisible and everything else starts out visible. Another way of looking at it is, everything derived from Window starts out invisible.
The paned refers to whether you use getContentPane.setLayout or simply setLayout.
| Differences Between Various Types of Windows | |||||||
|---|---|---|---|---|---|---|---|
| class | What Is It | AWT | Swing | Visible | Paned | Derived From | Default Layout |
| Applet | represents the featureless Window provided by the browser for an Applet to run in. | Y | N | true | N | Panel | FlowLayout |
| Button | Not a window. It is here is an example of a Component. | Y | N | true | N | Component | n/a |
| Canvas | an area you can use the low level drawing tools on, rather than dropping in components placed with a LayoutManager. Raw material for creating your own components. Just a place you can draw. It cannot contain Components. For Swing, use a JPanel instead. | Y | N | true | N | Component | n/a |
| Component | Not a window. It is an abstract class underlying Buttons etc. | Y | N | true | N | Object | n/a |
| Container | These are the basis on which all the other windows are built. They manage the child Components and LayoutManager. They are missing an addNotify method to create the peer object, so can't appear on screen. You don't normally instantiate Containers directly, but some subclass of them. | Y | Y | true | N | Component | null |
| Dialog | A pop-up box to deliver an error message or alert. Temporary Window for displaying information or requesting keystrokes. It requires a parent Frame, thus in cannot be used inside an Applet which has no Frame. It can be modal, which means it blocks input to all other Windows until it is dismissed. It must have a Frame mentioned in the constructor. | Y | N | false | N | Window | BorderLayout |
| Frame | A resizable, movable window with title bar and close button. Usually it contains Panels. | Y | N | false | N | Window | BorderLayout |
| JApplet | represents the featureless Window provided by the browser for an Applet to run in. | N | Y | true | Y | Applet | FlowLayout |
| JButton | Not a window. It is here is an example of a JComponent. | N | Y | true | N | AbstractButton, JComponent | n/a |
| JComponent | Not a window. It is an abstract class underlying JButtons etc. | N | Y | true | N | Container, not Component, Container! | n/a |
| JContainer | There is no such beast! Since Containers have no on screen aspect, the ordinary AWT Container suffices. | n/a | n/a | n/a | n/a | n/a | n/a |
| JDialog | A pop-up box to deliver an error message or alert. Usually created with JOptionPane methods. Temporary Window for displaying information or requesting keystrokes. It requires a parent JFrame, thus in cannot be used inside an JApplet which has no JFrame. It can be modal, which means it blocks input to all other JWindows until it is dismissed. You can place complex arrays of Components on JDialogs, not just simple error messages. | N | Y | false | Y | Dialog | BorderLayout |
| JFrame | A resizable, movable window with title bar and close button. Usually it contains JPanels. The entire application is usually a JFrame. | N | Y | false | Y | Frame | BorderLayout |
| JInternalFrame | Independent windows that the user can resized and move, but only within an enclosing JFrame. | N | Y | false | Y | JComponent | BorderLayout |
| JOptionPane | A modal pop-up box to deliver an error message or alert. Temporary Window for displaying information or requesting keystrokes. It requires a parent JFrame, thus in cannot be used inside an JApplet which has no JFrame. Unlike a JDialog, JOptionPanes are always modal, which means they block input to all other JWindows until they are dismissed. | N | Y | false | Y | JDialog | BorderLayout |
| JPanel | A region internal to a JFrame or another JPanel. Used for grouping components together. Optionally bounded by a visible border. Lives inside some enclosing Container. | N | Y | true | N | JComponent | FlowLayout |
| JWindow | A window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. | N | Y | false | Y | Window | BorderLayout |
| Panel | A region internal to a Frame or another Panel. Used for grouping components together. Not bounded by a visible border. You can change background colour of a panel to delimit it though. Lives inside some enclosing Container. | Y | N | true | N | Container | FlowLayout |
| Window | A window without a title bar or move controls. The program can move and resize it, but the user cannot. Free standing Window, not inside any other Window. It must have Frame mentioned in the constructor. | Y | N | false | N | Container | BorderLayout |
![]() | Java Swing | ||||||
| 1-56592-455-X | |||||||
| Robert Eckstein, Marc Loy and Dave Wood | |||||||
| |||||||
![]() | John Zukowski's Definitive Guide to Swing for Java 2 with CD-ROM | ||||||
| 1-893115-02-X | |||||||
| John Zukowski | |||||||
| very clear writer | |||||||
| |||||||
![]() | Core Swing Advanced Programming | ||||||
| 0-13-083292-8 | |||||||
| Kim Topley | |||||||
| On reading the Amazon reviews, this book looks best for HTML rendering. | |||||||
| |||||||
![]() | Swing, Second Edition | ||||||
| 1-930110-88-X | |||||||
| Matthew Robinson, Pavel Vorobiev, David Anderson | |||||||
| A relatively deep book. Also covers printing. | |||||||
| |||||||
cd J:\j2sdk1.4.2_04\demo\jfc\swingset2] java -jar Swingset2.jar
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 3721. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/swing.html | J:\mindprod\jgloss\swing.html | |||