Swing : Java Glossary
home S words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2008-03-10 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 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 (all)
Swing
Sun’s set of lightweight GUI components that give much fancier screen displays than the raw AWT. Since they are written in pure Java, they run the same on all platforms, unlike the AWT. They are part of the JFC. They support pluggable look and feel — not by using the native platform’s facilities but by roughly emulating them. This means you can get any supported look and feel on any platform. The disadvantage of lightweight components is slower execution. The advantage is uniform behaviour on all platforms.
Gotchas Books
Swing Threads Learning More
AWT vs Swing Links
Difference Between Window Types

Gotchas

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

and InputVerifier.

The good news is, even though you are using Runnable, you don’t have the overhead of actually creating a new Thread since you are just calling the run method on the already existing Swing Thread.

You are not on the Swing event thread when main first starts up! You are on it in your event listeners. You are tell if you are on it with:

// Either method will work in
    Java 1.3+
// to determine if you are running on the
dispatch thread,
// the same for AWT or Swing.

// in Swing
boolean onSwingThread 
= javax.
swing.SwingUtilities.
isEventDispatchThread();

// in AWT
boolean onDispatchThread 
= java.
awt.EventQueue.
isDispatchThread();

// Dump info about the current thread, name,
priority, group.
System.
out.println
( Thread.
currentThread() 
);

// Dump name of the current thread
System.
out.println
( Thread.
currentThread().
getName() 
);

One of the most common errors is to tie up the AWT/Swing event thread with some long running computation or even a sleep. Everything freezes up. The GUI can’t respond to mouse clicks and no repainting happens. You have to spin the time-consuming task off on its own thread or use a Timer.

is a Sun class that is not part of the JDK. It lets you convert some long-running event handling code into a separate thread with just a line of extra code.

If you violate these rules, the entire system starts to behave unpredicably and irrationally. If by violating these rules, you manage to create two event-processing threads, life gets really interesting as they unpredictably fight with each other handling Swing events.

AWT vs Swing

Which should you use, AWT or Swing?

Roughly Equivalent Components in AWT and Swing
AWT Swing Purpose
Applet JApplet Run inside a browser.
none Border decorate with a fancy edge.
Button JButton User clicks button to make something happen.
Canvas JPanel or JLayeredPane Place to roll your own low-level drawing
Checkbox JCheckBox (checked) or JRadioButton (round) or JToggleButton (full button) Checkbox is a small box, the user can tick or not to indicate a desired option. A RadioButton displays as a round button. JToggleButton displays as a rectangular button, usually with a custom icon. By default, the background changes colour slightly when selected. You can use these in isolation, or in clusters controlled by a CheckboxGroup or ButtonGroup.
CheckboxGroup ButtonGroup Used in conjunction with Checkbox, JCheckBox, JRadioButton and JToggleButton to allow only on of group of options to be selected.
Choice JComboBox Allows user to pick one from a drop-down menu of choices. JComboBox has a write-in feature. Ironically, JComboxBox does not support combinations of choices. For that you need a List of JList.
Color Color Describes a colour with 8 bits each of red, green, blue and transparency.
none JColorChooser Let’s user choose a colour from a pallette, or numerically, but not using hex.
Component JComponent Used to roll your own widgets.
Container Container Used to manage the Chinese boxes of panels within panels.
Dialog JDialog or JOptionPane Pop up a box to ask the user a question. JOptionPane is a simplified JDialog to handle common cases.
FileDialog JFileChooser Let the user select a directory or file from a tree.
none JFormattedTextField Let the user key in a field where certain characters must be alphabetic or numeric.
Frame JFrame or JInternalFrame Normal top level moveable window with widgets on it for minimize and close.
Label JLabel Used to label fields. Displays text. User cannot type into it. It tends to blend into the background.
List JList + JScrollPane JList lets the user make a selection, or many selections from a presented list which is always expanded. For long lists, you scroll.
LayoutManager LayoutManager Lets you design your layouts so they automatically reflow with different amounts of screen real estate, different text, difference translations, different available fonts etc.
Menu with MenuBar and MenuItem JMenu with JMenuBar, JMenuItem, JRadioButtonMenuItem and JPopupMenu Drop down menus to select functions to perform. JRadioButtonMenuItem allows toggling option selections in the menus. JPopupMenu lets the user pop up a menu when she right clicks a component.
Panel JPanel or JToolBar or JSplitPane or JTabbedPane, or JLayeredPane. Panels collect components together into rectangles to simplify layout and enable encapsulation. A panel can be treated like an atomic component for most purposes. JToolBar is just a panel specialised for holding a row of buttons. JSplitPane is a split panel where the user can move the divide. JTabbedPane lets you cram many panels in small space. The user selects a tab, much as on a file folder, to bring one of them into full view. JLayeredPane is an advanced feature for composing transparent overlays, e.g. sprites.
none JPasswordField For entering passwords.
ScrollPane JScrollPane For scrolling a panel where there is not enough room to see it all at once.
none JSeparator A decorative horizontal or vertical line. Note the spelling: JSeparator.
none JSlider The user uses the mouse to move an analog slider to select a value.
none JSpinner The user uses arrow keys or a mouse to select a value by making it go higher or lower.
TextArea JTextArea or JEditorPane or JTextPane TextArea and JTextArea allows multi-line text display, and data entry. JEditoKit allows display of formatted HTML or RTF with colours and fonts. JTextPane is like JEditorKit with the additional ability to edit styles and character attributes.
TextField JTextField Allow display or entering of one line of unformatted text. They always have a box around the text unlike labels which blend into the background. For more than one line see TextArea and JTextArea.
none JTree Displaying and editing tree-structured data.
Window JWindow Like a frame without the top bar widgets.

Difference Between Window Types

What are the differences between a Applet, Canvas, Component, Container, Dialog, Frame, JApplet, JComponent, JContainer, JDialog, JInternalFrame, JPanel, JFrame, JWindow, Panel and Window?

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. Panel FlowLayout
Button Not a window. It is here is an example of a Component. 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. Component n/a
Component Not a window. It is an abstract class underlying Buttons etc. 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. 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. Window BorderLayout
Frame A resizable, movable window with title bar and close button. Usually it contains Panels. Window BorderLayout
JApplet represents the featureless Window provided by the browser for an Applet to run in. Applet FlowLayout
JButton Not a window. It is here is an example of a JComponent. AbstractButton, JComponent n/a
JComponent Not a window. It is an abstract class underlying JButtons etc. 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. Dialog BorderLayout
JFrame A resizable, movable window with title bar and close button. Usually it contains JPanels. The entire application is usually a JFrame. Frame BorderLayout
JInternalFrame Independent windows that the user can resized and move, but only within an enclosing JFrame. 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. 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. 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. It optionally has a parent JFrame 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. 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 a parent Frame mentioned in the constructor. Container BorderLayout

For a great overview what is possible with Swing, see the SwingSet2 demo that comes part of the JDK download. Run it with this command:

cd J:\Program Files\java\jdk1.6.0_06\demo\jfc\swingset2]
java -jar Swingset2.jar

Books

book_cover recommend book⇒Java Swing
 paperback
ISBN10:1-56592-455-X
ISBN13:978-1-56592-455-0
publisher:O’Reilly recommended
published:1998-09-01
by:Robert Eckstein, Marc Loy and Dave Wood
Canadian flag amazon.ca. amazon.com. American flag
Canadian flag chapters.indigo.ca . powells.com American flag
French flag amazon.fr. barnesandnoble.com American flag
German flag amazon.de. download O’Reilly Safari American flag
UK flag amazon.co.uk.   
book_cover recommend book⇒John Zukowski’s Definitive Guide to Swing for Java 2 with CD-ROM
 paperback
ISBN10:1-893115-02-X
ISBN13:978-1-893115-02-6
publisher:Apress
published:1999-06-15
by:John Zukowski
very clear writer
Canadian flag amazon.ca. amazon.com. American flag
Canadian flag chapters.indigo.ca . powells.com American flag
French flag amazon.fr. barnesandnoble.com American flag
German flag amazon.de. amazon.co.uk. UK flag
book_cover recommend book⇒Core Swing Advanced Programming
 paperback
ISBN10:0-13-083292-8
ISBN13:978-0-13-083292-4
publisher:Pearson Education
published:1999-12-20
by:Kim Topley
On reading the Amazon reviews, this book looks best for HTML rendering.
Canadian flag amazon.ca. amazon.com. American flag
Canadian flag chapters.indigo.ca . powells.com American flag
French flag amazon.fr. barnesandnoble.com American flag
German flag amazon.de. amazon.co.uk. UK flag

book_cover recommend book⇒Swing, Second Edition
 paperback
ISBN10:1-930110-88-X
ISBN13:978-1-930110-88-5
publisher:Manning Publications
published:2003-02
by:Matthew Robinson, Pavel Vorobiev, David Anderson
A relatively deep book. Also covers printing.
Canadian flag amazon.ca. amazon.com. American flag
Canadian flag chapters.indigo.ca . powells.com American flag
French flag amazon.fr. barnesandnoble.com American flag
German flag amazon.de. amazon.co.uk. UK flag
book_cover recommend book⇒Swing Hacks: Tips and Tools for Killer GUIs
ISBN10:0-596-00907-0
ISBN13:978-0-596-00907-6
publisher:O’Reilly recommended
published:2005-06-01
by:Joshua Marinacci & Chris Adamson
Weird, fun, tricky, non-obvious things you can do with Swing.
Canadian flag amazon.ca. amazon.com. American flag
Canadian flag chapters.indigo.ca . powells.com American flag
French flag amazon.fr. barnesandnoble.com American flag
German flag amazon.de. download O’Reilly Safari American flag
UK flag amazon.co.uk.   

Learning Even More

Sun’s Javadoc on the Swing component summary class : available:
Sun’s Javadoc on the AWT component Summary class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 80,119. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/swing.html J:\mindprod\jgloss\swing.html