Java Programming/Swing

From Wikiversity
Jump to navigation Jump to search

About Swing Swing library is an official Java GUI toolkit released by Sun Microsystems. Swing is a lightweight component and does not use the native API's of the OS. The main characteristics of the Swing toolkit:

  • platform independent
  • customizable
  • extensible
  • configurable
  • lightweight

Swing is no longer the latest GUI toolkit for use with Java, being replaced by JavaFX. However, it will remain included in Java for the forseeable future. For more information, see the JavaFX Frequently Asked Questions.

Packages[edit | edit source]

  • javax.swing
  • javax.swing.border
  • javax.swing.colorchooser
  • javax.swing.event
  • javax.swing.filechooser
  • javax.swing.plaf
  • javax.swing.plaf.basic
  • javax.swing.plaf.metal
  • javax.swing.plaf.multi
  • javax.swing.plaf.synth
  • javax.swing.table
  • javax.swing.text
  • javax.swing.text.html
  • javax.swing.text.html.parser
  • javax.swing.text.rtf
  • javax.swing.tree
  • javax.swing.undo

Java Swing first programs[edit | edit source]

A basic test application for Swing is as follows:

import javax.swing.JFrame;

public class Test extends JFrame {
   public Test() {
        setSize(300, 200);
        setTitle("Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
  public static void main(String[] args) {
       Test test = new Test();
       test.setVisible(true);
     } 
}

The constructor contains three function calls. The first two set the size and title of the window. The third statement is used to terminate the application once the window is closed; by default, the program will keep running even though the main function finishes. This is a change from non-graphical Java applications.

Display[edit | edit source]

Project: Introduction to Programming in Java
Previous: Introduction to Programming in Java/Arrays — Java Programming/Swing — Next: