Introduction to Swing – Java Tutorial

In this chapter you will learn

1. What is Swing in Java?
2. Advantage and Disadvantages of Swing
3. Hierarchy of Swing
4. Commonly Used Methods of Swing

What is Swing in Java?

Swing lets you create platform independent GUI application in Java. It has rich set of libraries that allows you to create beautiful and robust GUI application.

Advantage of Swing

There are so many advantages of Swing that is listed below:

  1. Swing provides rich set of controls or components that are purely written in Java.
  2. Swing Application is platform independent whereas AWT is windows based.
  3. Swing also provides all the functionality of AWT.
  4. Swing component inherits look and feel and changes their appearance according to your base theme.
  5. It follows Model-View-Controller (MVC) architecture.
  6. Swing provides “EXTRA” for components, such as:
    1. Icons on many components
    2. Decorative border for components
    3. Tooltip for components
  7. Swing components are lightweight, flexible and faster than AWT.
  8. It provides Built-in double buffering.
  9. Swing provides paint debugging support for when you build your own components.
  10. Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc

Disadvantage of Swing

  1. Swing Requires Java 2 or a separate jar file.
  2. You need to careful while drawing components otherwise it might be slower than AWT.
  3. Swing looks like native components but it might not act exactly like native components.

Hierarchy of Swing

Swing Commonly Used Methods

setToolTiptext(String s)Add a tool tip that appears when the cursor is over the component
setFont(Font f)
Font getFont()
Set or get the font of the text on the component
boolean isVisible()
Set or get whether the component is visible using true or false as the argument
boolean isEnabled()
Enable or disable the component using true or false as the argument.
Check to see if a component is enabled or disabled.
boolean isOpaque()
Set the component so it is filled with a background color.
Check to see if a component is opaque.
setBackground (Color c)
Color getBackground ()
Set or get the component’s background color.
setForeground (Color c)
Color getForeground ()
Set or get the text color.
add (component)
Used to add and remove components
public void setSize(int width,int height)sets size of the component.
public void setLayout(LayoutManager m)sets the layout manager for the component.
public void add(Component c)add a component on another component.
public void setVisible(boolean b)sets the visibility of the component. It is by default false.

Don’t worry, you will use all these methods throughout this tutorial and very soon these methods will be memorize to you.

Next –
Next chapter is Where to Develop Swing Application. In this chapter you will know how to start with Swing and Where to write swing code.

Leave a Reply

Your email address will not be published. Required fields are marked *