Swing (Java)

from Wikipedia, the free encyclopedia
The Widgets of swing with the Ocean look and feel (standard since Java 1.5)
Class hierarchy of AWT and Swing

Swing is a GUI toolkit for the Java programming language from Sun Microsystems . Since Java version 1.2 (1998) it has been part of the Java runtime. Swing is one of the Java Foundation Classes (JFC), which provide a collection of libraries for programming graphical user interfaces. These libraries include Java 2D , the Java Accessibility API (JAAPI), the Drag-and-Drop API, and the Abstract Window Toolkit (AWT). Swing builds on the older AWT and is interwoven with the other APIs.

Swing's main competitor is the SWT developed for Eclipse . The designated successor to Swing is the JavaFX framework , which has been included in the scope of delivery of the Oracle JDK since March 2014, but which has not yet been specified as an official Java standard by the Java Community Process .

features

construction

Swing has a modular and object-oriented structure, so that it is particularly suitable for developing complex applications. Due to the platform independence, a lot of development and test work is also eliminated.

Pluggable look and feel

The appearance ( look , see skin ) and the behavior ( feel ) of swing components can be adjusted using so-called look-and-feel . Both the Motif look and feel and the metal look and feel (from Java 5 the ocean look and feel) are available on all platforms , and on Linux Gtk, Mac and Windows an additional system standard customized own look and feel. There are a number of independently developed appearances, most of which are metal based. Since Java 1.5 the synth look and feel has been added, the appearance of which is composed of images and is described in an XML file so that no Java code has to be written. With Java 1.6, Update 10, the completely vector-based Nimbus look and feel was introduced.

Lightweight components

Swing components are rendered directly by Java and do not depend on native operating system components . This means that all Swing components work in the same way on all platforms, regardless of whether the respective platform provides a component or not. The disadvantage is that a Swing application doesn't look like an application designed for the operating system. However, this can be compensated for by a selection of appropriate pluggable look-and-feel. This property is described with the English word Lightweight UI (lightweight = all-Java language).

comparison

Compared to AWT , Swing has the following additional components that can be used to create surfaces:

Multithreading

Swing is not thread-safe , so unexpected interactions between different threads can occur if not programmed carefully. Instead, the Swing components are implemented in such a way that their methods always have to be executed in a so-called Event Dispatch Thread of the AWT , which carries out the entire event processing of graphically interactive Java applications. To simplify this, the helper class provides SwingUtilitiestwo methods that can be called from other threads and take an executable object of the type Runnableas a parameter.

  • invokeLaterputs the executable object in the AWT event queue and returns to the calling code before it is executed. So the calling thread is not blocked.
  • invokeAndWaitenqueues the executable object in the AWT event queue and waits until it has been processed. So the calling thread is blocked.

Violations of these conventions are not reported to the programmer, so that associated difficulties remain undetected for a long time or are not resolved at all. UI frameworks such as SWT or .NET WinForms handle this differently and acknowledge faulty calls immediately.

Outside of the JRE, Sun provided a base class called SwingWorker , which offers two methods to be overloaded: One that is called in a separate thread and can perform a long-lasting operation and another that is executed in the event dispatch thread after this operation has ended and manipulate swing components. There is also a mechanism that allows intermediate steps (e.g. in a progress indicator) to be visualized. Since Java 1.6 it has been included in the JRE as javax.swing.SwingWorker.

history

Swing was first delivered as an external library with JDK 1.1.5 at the end of 1997 and has been an integral part of the Java runtime environment since JDK 1.2 ("Java 2") at the end of 1998. Swing soon had a reputation for poor performance and unsuitable for "serious" applications. The standard style ( look and feel ) of swing windows did not find that many friends either.

Sun then paid great attention to improving the appearance of the supplied Look and Feel , as well as the performance of Swing. In the meantime, the performance has improved significantly through improved hardware support for the acceleration functions of graphics cards, as well as through performance improvements in the class libraries themselves and in the Java Runtime. Since Java version 1.4 2002 at the latest, Swing has not exhibited any noticeable performance differences to native GUIs. Performance comparisons between Swing and SWT also show no performance differences.

example

The Hello World program is given here as an example of a Swing application :

import javax.swing.*;

public class HelloWorldSwing {
    public static void main(String[] args) {
        // Verpacke den auszuführenden Quellcode in ein eigenes
        // Runnable-Objekt, um diesen nachher im Event Dispatching
        // Thread ausführen zu können
        Runnable guiCreator = new Runnable() {
            public void run() {
                // Erstellt das Swing-Fenster
                JFrame fenster = new JFrame("Hallo Welt mit Swing");
                // Swing anweisen, das Programm zu beenden, wenn das Fenster
                // geschlossen wird
                fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                // Fügt den "Hallo Welt"-Text hinzu
                JLabel label = new JLabel("Hallo Welt");
                fenster.add(label);

                // Zeigt das Fenster an
                fenster.setSize(300, 200);
                fenster.setVisible(true);
            }
        };

        // Führe den obigen Quellcode im Event-Dispatch-Thread aus
        SwingUtilities.invokeLater(guiCreator);
    }
}

Parts of this source code are executed by different threads . The thread that executes the main method is usually not the event dispatch thread (EDT). Swing components should generally only be created and changed by EDT (unless the documentation expressly states otherwise). The source code for the GUI is therefore packaged in its own runnable object. The thread that executes the main method only creates the runnable object and instructs Swing to execute this source code later in EDT.

Extensions

Swing can be expanded at various points using the plug-in concept built into Swing. Typical extensions relate to Look & Feels, layout managers and other components.

The SUN open source project SwingLabs offers a number of extensions for Swing and serves as a test environment for future extensions of Swing. Among other things, the classes GroupLayout, SystemTray and Desktop introduced in Java 6 were hosted in SwingLabs. Further extensions are collected by the java.net Java Desktop Community in the Java Desktop Swing Depot. JGoodies by Karsten Lentzsch also offers a range of swing extensions, such as the FormLayout.

The Spring Richclient project offers an integration of Swing into the Spring Framework. However, it has not been further developed since mid-2009.

Swing editors

There are a number of mostly paid WYSIWYG GUI design tools for Swing. The following are the most popular:

  • JFormDesigner - Commercial editor, both standalone and as Eclipse, IntelliJ-IDEA and JBuilder plug-in
  • Jigloo - Commercial Eclipse plugin for Swing and SWT GUIs, free for non-commercial purposes
  • Window Builder Pro - Formerly a commercial Eclipse plug-in for Swing, SWT and GWT GUIs, is currently being transferred to an Eclipse project.
  • Matisse4MyEclipse - Port of Matisse , the GUI builder of the NetBeans IDE , for Eclipse and Swing, only runs under MyEclipse
  • Visual Editor - Open Source Eclipse Plugin for Swing and SWT GUIs (the associated development project has not been active since April 2012)

literature

  • German-language books:
    • David Geary: Graphic Java 2.0 Volume II. Mastering the JFC (Swing). 3. Edition. Markt + Technik, November 15, 1999, ISBN 3-8272-9590-4 (translation of the English original)
    • David Geary: Graphic Java 2.0. Master the JFC (AWT). 3. Edition. Markt + Technik, July 15, 1999, ISBN 3-8272-9585-8 (translation of the English original)
    • Paul Fischer: Graphic programming with Java Swing. Addison-Wesley, September 15, 2001, ISBN 3-8273-1910-2
  • English language books:
    • Marc Loy, Robert Eckstein, Dave Wood: Java Swing. 2nd Edition. O'Reilly, November 2002, ISBN 0-596-00408-7
    • Kathy Walrath, Mary Campione, Alison Huml: The JFC Swing Tutorial. 2nd Edition. Addison-Wesley Professional, February 2004, ISBN 0-201-91467-0 (reference)
    • Kim Topley: Core Swing Advanced Programming. Prentice Hall, 2000, ISBN 0-13-083292-8
    • Kim Topley: Core, Java Foundation Classes. Prentice Hall, 1998, ISBN 0-13-080301-4
    • David M. Geary: Mastering the JFC - Volume II Swing. 3. Edition. Prentice Hall PTR, 1998, ISBN 0-13-079667-0 (Reference)
    • David M. Geary: Mastering the JFC - Volume I AWT. 3. Edition. Prentice Hall PTR, 1999, ISBN 0-13-079666-2 (Reference)

Web links

Wikibooks: Java Standard: Graphic interfaces with Swing  - learning and teaching materials

Individual evidence

  1. Swing Enhancements in the Java Standard Edition 6.0 and SE 6 Update 10 Release Notes at Oracle
  2. Christian Ullenboom: Swing is not thread-safe . ( online [accessed March 9, 2008]). online ( Memento of the original from May 10, 2008 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice.  @1@ 2Template: Webachiv / IABot / www.tutego.com
  3. ^ AWT Threading Issues . ( online [accessed April 18, 2007]).
  4. SwingUtilities (Java Platform SE 6) . 2006 ( online [accessed April 18, 2007] Java API documentation).
  5. Križnar Igor: SWT Vs. Swing Performance Comparison (PDF) cosylab.com. March 3, 2006. Archived from the original on July 4, 2008. Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. Retrieved September 16, 2009: “ Initial expectation before performing this benchmark was to find SWT outperform Swing. This expectation stemmed from greater responsiveness of SWT-based Java applications (eg, Eclipse IDE) compared to Swing-based applications. However, this expectation could not be quantitatively confirmed. " @1@ 2Template: Webachiv / IABot / cosylib.cosylab.com
  6. Java Desktop ( Memento of the original from July 29, 2005 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / community.java.net
  7. Swing Depot: Component Suites ( Memento of the original from June 9, 2009 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / www.javadesktop.org
  8. JGoodies Homepage
  9. Computerwoche : GUI-Builder for Eclipse ( Memento of July 16, 2012 in the Internet Archive ).
  10. eclipse.org