Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Introduction to Swing

Michael Abernethy, Team Lead, IBM
Author photo
Michael Abernethy is currently the test team lead on the IBM WebSphere System Management team based in Austin, TX. Prior to this assignment, he was a Swing UI developer at multiple customer locations.

Summary:  This hands-on introduction to Swing, the first in a two-part series on Swing programming, walks through the essential components in the Swing library. Java developer and Swing enthusiast Michael Abernethy guides you through the basic building blocks and then assists as you build basic but functional Swing application. Along the way you'll learn how to use models to ease the process of dealing with the data.

Date:  29 Jun 2005
Level:  Introductory

Activity:  35201 views
Comments:  

Summary

Summary

This tutorial introduced you to Swing and the concepts you need to know to create a basic, functional user interface. After completing this tutorial, you should have accomplished several things:

  • You should be familiar with the Swing components. You should be able to recognize the components when you see them on the screen and apply them in your own applications by using their basic functions. In addition, you should have a general understanding of when you should use the components (for example, when to use a JCheckBox vs. when to use a JRadioButton).

  • You learned how to lay out these components on the screen with layout managers. Layout managers have existed since Java first came out, and unfortunately, the most powerful layout manager is also the most difficult to use. However, visual editors (such as the one found in Eclipse) make the layout process incredibly easy by creating all the layout code automatically.

  • You learned about the event model, mainly how Swing uses the event/listener model in all of its components to allow one component to accept user interaction and pass this interaction on to other classes. These other classes register for events from the component and take appropriate action when they receive events. The event/listener model is used throughout Swing, and you should learn it in depth to better work with every Swing component.

  • You learned about data models and how they fit into the MVC architecture in Swing. Models allow components to display data without knowing anything about the data itself. They also let you reuse models, allowing multiple components that display similar data to use the same model, eliminating the need to create an original model for each instance of the component. In large-scale applications, models serve as the "translation" between server-side data objects and client-side Swing components.

It's important and vital to understand that this tutorial is not all-encompassing. It's not even meant to cover all the basics of Swing. There's just far too much to squeeze into this tutorial to give beginners a thorough introduction to Swing. I've hopefully pointed out the crucial elements, including the most commonly used components and their most commonly used functions. You should be aware that probably about twice as many components exist in Swing, and I encourage you to look through Swing documentation to cover the ones I missed. Also, I've barely touched upon the functions of most of the components; each Swing component has dozens, even up to a hundred, functions you can potentially use.

But I don't want to rain on your parade. By finishing this tutorial you've learned quite enough to build a majority of your Swing applications, and the knowledge gained from this tutorial serves as a solid foundation if you explore the other functionalities and components Swing offers.


Next steps

As you know by now, this tutorial has a companion called "Intermediate Swing," which will build upon the knowledge you've gained in this introduction with an examination the more difficult concepts you need to understand to make your application more polished and powerful. These concepts include:

  • More advanced JTable features including the table properties, more sophisticated TableModel management, the TableRenderer to change the appearance of the JTable, and how to sort the table columns.

  • Threading and how it fits into Swing. Because users cannot accept an interface that locks up when it hits the database, Swing must use separate threads for longer operations.

  • How to create custom components. If you feel limited by what Swing offers you now, I'll show you how to create components that might look or behave differently than the ones built into Swing.

  • Custom look-and-feels. I will discuss how to completely change the look and feel of an application through two methods: one creates a new custom UI look-and-feel and the other uses Synth, a skinnable look-and-feel.

7 of 9 | Previous | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=131672
TutorialTitle=Introduction to Swing
publish-date=06292005
author1-email=mabernet@us.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).