JavaRanch Home    
This page:         last edited 02 November 2014         What's Changed?         Edit

Swing Faq   

SwingIsAProperNoun - Avoiding a common mistake when referring to Swing in writing or speech.




PlotTest - simple example of how to create an X/Y graph; can be changed to use AWT easily

PlotSineCurve - simple example of how to draw a curve; can be changed to use AWT easily

NumericTextField - how to create a text field that only accepts numbers as input

ComponentDoesntUpdatePromptly - Usage of SwingWorker? for background tasks

Q. I created a JOptionPane.showConfirmDialog and no matter which button I have focused, when I hit enter, I always get the OK result.

A. Default button behavior is dependent on the Look and Feel you have specified. To get the desired result in Windows, use the Windows Look and Feel for your application.

Q. Why are Swing components called lightweight, while AWT components are heavyweight?

A. Swing components are considered lightweight because they draw themselves and do not rely on native calls to the OS. AWT (And SWT for that matter) make calls to the OS and say "Hey, I need a button" and the OS throws them back a button. Swing uses the Graphics2D API to draw its widgets.

Note that not all components in the javax.swing package are lightweight. Namely, top level containers like JFrame are still considered heavyweight components because they extend java.awt.Frame.

While mixing AWT and Swing used to be fraught with problem, updates to Java 6 and 7 have made that less troublesome. See this article for more information on that.

(The above was posted in a Saloon discussion. Further information can be found in this thread and on jGuru.)

Q. The GridBagLayout class is too complex for my tastes; is anything simpler available?

A. There is indeed. If all you're looking for is a GridLayout with variable row and column sizes, then GridLayout2 may be for you. If you're looking for even more options, check out SGLayout.



Links to further information


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton