| | |
    Java Beginner Home
    Table of Contents
    Swing Tutorial
     JFrame
     JInternalFrame
     JWindow
     JOptionPane
     JLabel
     JTextField
     JPasswordField
     JTextArea
     JButton
     JRadioButton
     JCheckBox
     JComboBox
     Jlist
     JTabbedPane
     JMenuBar
     Scrollable JPopupMenu
     JToolBar
     BorderLayout
     FlowLayout
     GridLayout
     GridBagLayout
     Java Look and Feel
     Swing Calculator
     Swing Address book
     Shuffle Game
    Download Java Software
    Advertise
    Feedback
    Java books
    Eclipse Plugin Site

Java Swing Tutorial



This site contains brief tutorials for java swing programming along with java swing examples and source code.

Java Swings Tutorial

What is Swings in java ?

  • A part of The JFC
  • Swing Java consists of
    Look and feel
    Accessibility
    Java 2D
    Drag and Drop, etc
  • Compiling & running programs
  • ‘javac <program.java>’ && ‘java <program>’
    Or JCreator / IDE

  • if you do not explicitly add a GUI component to a container, the GUI component will not be displayed when the container appears on the screen.
  • Swing, which is an extension library to the AWT, includes new and improved components that enhance the look and functionality of GUIs. Swing can be used to build Standalone swing gui Apps as well as Servlets and Applets. It employs a model/view design architecture. Swing is more portable and more flexible than AWT.

Swing Model/view design: The “view part” of the MV design is implemented with a component object and the UI object. The “model part” of the MV design is implemented by a model object and a change listener object.

Swing is built on top of AWT and is entirely written in Java, using AWT’s lightweight component support. In particular, unlike AWT, t he architecture of Swing components makes it easy to customize both their appearance and behavior. Components from AWT and Swing can be mixed, allowing you to add Swing support to existing AWT-based programs. For example, swing components such as JSlider, JButton and JCheckbox could be used in the same program with standard AWT labels, textfields and scrollbars. You could subclass the existing Swing UI, model, or change listener classes without having to reinvent the entire implementation. Swing also has the ability to replace these objects on-the-fly.

  • 100% Java implementation of components
  • Pluggable Look & Feel
  • Lightweight components
  • Uses MVC Architecture
    Model represents the data
    View as a visual representation of the data
    Controller takes input and translates it to changes in data
  • Three parts
    Component set (subclasses of JComponent)
    Support classes
    Interfaces

In Swing, classes that represent GUI components have names beginning with the letter J. Some examples are JButton, JLabel, and JSlider. Altogether there are more than 250 new classes and 75 interfaces in Swing — twice as many as in AWT.

Java Swing class hierarchy

The class JComponent, descended directly from Container, is the root class for most of Swing’s user interface components.

Swing contains components that you’ll use to build a GUI. I am listing you some of the commonly used Swing components. To learn and understand these swing programs, AWT Programming knowledge is not required.

Java Swing Examples

Below is a java swing code for the traditional Hello World program.

Basically, the idea behind this Hello World program is to learn how to create a java program, compile and run it. To create your java source code you can use any editor( Text pad/Edit plus are my favorites) or you can use an IDE like Eclipse.

import javax.swing.JFrame;
import javax.swing.JLabel;

//import statements
//Check if window closes automatically. Otherwise add suitable code
public class HelloWorldFrame extends JFrame {

	public static void main(String args[]) {
		new HelloWorldFrame();
	}
	HelloWorldFrame() {
		JLabel jlbHelloWorld = new JLabel("Hello World");
		add(jlbHelloWorld);
		this.setSize(100, 100);
		// pack();
		setVisible(true);
	}
}

Output

Note: Below are some links to java swing tutorials that forms a helping hand to get started with java programming swing.


  • JPanel is Swing’s version of the AWT class Panel and uses the same default layout, FlowLayout. JPanel is descended directly from JComponent.
  • JFrame is Swing’s version of Frame and is descended directly from that class. The components added to the frame are referred to as its contents; these are managed by the contentPane. To add a component to a JFrame, we must use its contentPane instead.
  • JInternalFrame is confined to a visible area of a container it is placed in. It can be iconified , maximized and layered.
  • JWindow is Swing’s version of Window and is descended directly from that class. Like Window, it uses BorderLayout by default.
  • JDialog is Swing’s version of Dialog and is descended directly from that class. Like Dialog, it uses BorderLayout by default. Like JFrame and JWindow,
    JDialog contains a rootPane hierarchy including a contentPane, and it allows layered and glass panes. All dialogs are modal, which means the current
    thread is blocked until user interaction with it has been completed. JDialog class is intended as the basis for creating custom dialogs; however, some
    of the most common dialogs are provided through static methods in the class JOptionPane.
  • JLabel, descended from JComponent, is used to create text labels.
  • The abstract class AbstractButton extends class JComponent and provides a foundation for a family of button classes, including

    JButton.

  • JTextField allows editing of a single line of text. New features include the ability to justify the text left, right, or center, and to set the text’s font.
  • JPasswordField (a direct subclass of JTextField) you can suppress the display of input. Each character entered can be replaced by an echo character.
    This allows confidential input for passwords, for example. By default, the echo character is the asterisk, *.
  • JTextArea allows editing of multiple lines of text. JTextArea can be used in conjunction with class JScrollPane to achieve scrolling. The underlying JScrollPane can be forced to always or never have either the vertical or horizontal scrollbar;
    JButton is a component the user clicks to trigger a specific action.
  • JRadioButton is similar to JCheckbox, except for the default icon for each class. A set of radio buttons can be associated as a group in which only
    one button at a time can be selected.
  • JCheckBox is not a member of a checkbox group. A checkbox can be selected and deselected, and it also displays its current state.
  • JComboBox is like a drop down box. You can click a drop-down arrow and select an option from a list. For example, when the component has focus,
    pressing a key that corresponds to the first character in some entry’s name selects that entry. A vertical scrollbar is used for longer lists.
  • JList provides a scrollable set of items from which one or more may be selected. JList can be populated from an Array or Vector. JList does not
    support scrolling directly, instead, the list must be associated with a scrollpane. The view port used by the scroll pane can also have a user-defined
    border. JList actions are handled using ListSelectionListener.
  • JTabbedPane contains a tab that can have a tool tip and a mnemonic, and it can display both text and an image.
  • JToolbar contains a number of components whose type is usually some kind of button which can also include separators to group related components
    within the toolbar.
  • FlowLayout when used arranges swing components from left to right until there’s no more space available. Then it begins a new row below it and moves
    from left to right again. Each component in a FlowLayout gets as much space as it needs and no more.
  • BorderLayout places swing components in the North, South, East, West and center of a container. You can add horizontal and vertical gaps between
    the areas.
  • GridLayout is a layout manager that lays out a container’s components in a rectangular grid. The container is divided into equal-sized rectangles,
    and one component is placed in each rectangle.
  • GridBagLayout is a layout manager that lays out a container’s components in a grid of cells with each component occupying one or more cells,
    called its display area. The display area aligns components vertically and horizontally, without requiring that the components be of the same size.
  • JMenubar can contain several JMenu’s. Each of the JMenu’s can contain a series of JMenuItem ‘s that you can select. Swing provides support for
    pull-down and popup menus.
  • Scrollable JPopupMenu is a scrollable popup menu that can be used whenever we have so many items in a popup menu that exceeds the screen visible height.

    Java Swing Projects

  • Java Swing Calculator developed using Java Swing. It is a basic four-function calculator java program source code.
  • Java Swing Address Book demonstrates how to create a simple free address book program using java swing and jdbc. Also you will learn to use
    the following swing components like Jbuttons, JFrames, JTextFields and Layout Manager (GridBagLayout).

 
 
    Java is a trademark of Sun Microsystems, Inc.
| | |
© Copyright 2007-08 javabeginner.com