This lesson explains the concepts you need to use Swing components in building a user interface. First we examine the simplest Swing application you can write. Then we present several progressively complicated examples of creating user interfaces using components in the
This section has not yet been updated to reflect features and conventions of the latest release, JDK 6.0.
javax.swing
package. We cover several Swing components, such as buttons, labels, and text areas. The handling of events is also discussed, as are layout management and accessibility. This lesson ends with a set of questions and exercises so you can test yourself on what you’ve learned.
Note: Most of the topics this lesson discusses are covered in depth in later lessons--for example, you can get detailed information on how to use components from Using Swing Components. This lesson is designed for readers who want to learn the basics quickly by examining some simple examples.
Note: You can download all the examples (including images) used in this trail from the Example Index.
- Example One: Your First Swing Program
- This small example has all the code that every Swing application must have.
- Example Two:
SwingApplication
- This section takes you through the code for a small Swing application. The topics covered include: look and feel, setting up buttons and labels, handling events, and adding borders.
- Example Three:
CelsiusConverter
- The third example, a program that converts a given temperature from Celsius to Fahrenheit, illustrates how to add an event listener to a text field and display the converted temperature on a label.
- Example Four: An Improved
CelsiusConverter
- The fourth example improves our temperature conversion example and illustrates how to add HTML and icons to components, setting the default button and how to create a formatted text field.
- Example Five:
LunarPhases
- The fifth example covers how to set up multiple panels, compound borders, combo boxes, and how to load multiple images.
- Example Six:
VoteDialog
- You can vote for your favorite political candidate in the final example,
VoteDialog
. This application illustrates the use of radio buttons and dialogs.- Summary
- A summary of important concepts covered in this section.
- Questions and Exercises
- Try these questions and exercises to test what you've learned in this lesson.