Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Swing (Java)

From Wikipedia, the free encyclopedia
Java-based GUI toolkit
Example Swing widgets in Java

Swing is aGUIwidget toolkit forJava.[1] It is part ofOracle'sJava Foundation Classes (JFC) – anAPI for providing agraphical user interface (GUI) for Java programs.

Swing was developed to provide a more sophisticated set of GUIcomponents than the earlierAbstract Window Toolkit (AWT). Swing provides alook and feel that emulates the look and feel of several platforms, and also supports apluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check boxes and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables, and lists.[2]

Unlike AWT components, Swing components are not implemented by platform-specific code. Instead, they are written entirely in Java and therefore are platform-independent.

In December 2008,Sun Microsystems (Oracle's predecessor) released theCSS /FXML based framework that it intended to be the successor to Swing, calledJavaFX.[3]

History

[edit]

TheInternet Foundation Classes (IFC) were agraphics library for Java originally developed byNetscape Communications Corporation and first released on December 16, 1996. On April 2, 1997,Sun Microsystems andNetscape Communications Corporation announced their intention to incorporate IFC with other technologies to form theJava Foundation Classes.[4] The "Java Foundation Classes" were later renamed "Swing."[clarification needed]

Swing introduced a mechanism that allowed thelook and feel of every component in an application to be altered without making substantial changes to the application code. The introduction of support for apluggable look and feel allows Swing components to emulate the appearance of native components while still retaining the benefits of platform independence. Originally distributed as a separately downloadable library, Swing has been included as part of theJava Standard Edition since release 1.2.[5] The Swing classes and components are contained in thejavax.swingpackage hierarchy.

Development of Swing's successor,JavaFX, started in 2005, and it was officially introduced two years later at JavaOne 2007.[6] JavaFX was open-sourced in 2011 and, in 2012, it became part of the Oracle JDK download. JavaFX is replacing Swing owing to several advantages, including being more lightweight, havingCSS styling, sleek design controls, and the use ofFXML and Scene Builder.[3] In 2018, JavaFX was made a part of the OpenJDK under the OpenJFX project to increase the pace of its development.[7]

Members of the Java Client team that was responsible for Swing included James Gosling (Architect), Rick Levenson (manager), Amy Fowler & Hans Muller (co-technical leads), Tom Ball, Jeff Dinkins, Georges Saab,[8] Tim Prinzing, Jonni Kanerva, and Jeannette Hung & Jim Graham (2D Graphics).[9]

Architecture

[edit]

Swing is a platform-independent, "model–view–controller"GUI framework for Java, which follows a single-threaded programming model.[10] Additionally, this framework provides a layer of abstraction between the code structure and graphic presentation of a Swing-based GUI.

Foundations

[edit]

Swing is platform-independent because it is completely written in Java. Complete documentation for all Swing classes can be found in theJava API Guide for Version 6 or theJava Platform Standard Edition 8 API Specification for Version 8.

Extensible

[edit]

Swing is a highly modular-based architecture, which allows for the "plugging" of various custom implementations of specified framework interfaces: Users can provide their own custom implementation(s) of these components to override the default implementations using Java's inheritance mechanism viaLookAndFeel.

Swing is acomponent-based framework, whose components are all ultimately derived from theJComponent class. Swing objects asynchronously fire events, have bound properties, and respond to a documented set of methods specific to the component. Swing components areJavaBeans components, compliant with theJavaBeans specification.

Configurable

[edit]

Swing's heavy reliance on runtime mechanisms and indirect composition patterns allows it to respond at run time to fundamental changes in its settings. For example, a Swing-based application is capable ofhot swapping its user-interface during runtime. Furthermore, users can provide their own look and feel implementation, which allows for uniform changes in the look and feel of existing Swing applications without any programmatic change to the application code.

Lightweight UI

[edit]

Swing's high level of flexibility is reflected in its inherent ability to override the native hostoperating system (OS)'s GUI controls for displaying itself. Swing "paints" its controls using the Java 2D APIs, rather than calling a native user interface toolkit. Thus, a Swing component does not have a corresponding native OS GUI component, and is free to render itself in any way that is possible with the underlying graphics GUIs.

However, at its core, every Swing component relies on anAWT container, since (Swing's)JComponent extends (AWT's) Container. This allows Swing to plug into the host OS's GUI management framework, including the crucial device/screen mappings and user interactions, such as key presses or mouse movements. Swing simply "transposes" its own (OS-agnostic) semantics over the underlying (OS-specific) components. So, for example, every Swing component paints its rendition on the graphic device in response to a call to component.paint(), which is defined in (AWT) Container. But unlike AWT components, which delegated the painting to their OS-native "heavyweight" widget, Swing components are responsible for their own rendering.

This transposition and decoupling is not merely visual, and extends to Swing's management and application of its own OS-independent semantics for events fired within its component containment hierarchies. Generally speaking, the Swing architecture delegates the task of mapping the various flavors of OS GUI semantics onto a simple, but generalized, pattern to the AWT container. Building on that generalized platform, it establishes its own rich and complex GUI semantics in the form of theJComponent model.

Loosely coupled and MVC

[edit]

The Swing library makes heavy use of themodel–view–controller softwaredesign pattern,[11] which conceptually decouples the data being viewed from the user interface controls through which it is viewed. Because of this, most Swing components have associatedmodels (which are specified in terms of Javainterfaces), and the programmers can use various default implementations or provide their own. The framework provides default implementations of model interfaces for all of its concrete components. The typical use of the Swing framework does not require the creation of custom models, as the framework provides a set of default implementations that are transparently, by default, associated with the correspondingJComponent child class in the Swing library. In general, only complex components, such as tables, trees and sometimes lists, may require the custom model implementations around the application-specific data structures. To get a good sense of the potential that the Swing architecture makes possible, consider the hypothetical situation where custom models for tables and lists are wrappers overDAO and/orEJB services.

Typically, Swing component model objects are responsible for providing a concise interface defining events fired, and accessible properties for the (conceptual) data model for use by the associated JComponent. Given that the overall MVC pattern is a loosely coupled collaborative object relationship pattern, the model provides the programmatic means for attaching event listeners to the data model object.wat these events are model centric (ex: a "row inserted" event in a table model) and are mapped by the JComponentspecialization into a meaningful event for the GUI component.

For example, theJTable has a model calledTableModel that describes an interface for how a table would access tabular data. A default implementation of this operates on a two-dimensionalarray.

The view component of a Swing JComponent is the object used to graphically represent the conceptual GUI control. A distinction of Swing, as a GUI framework, is in its reliance on programmatically rendered GUI controls (as opposed to the use of the native host OS's GUI controls). Prior toJava 6 Update 10, this distinction was a source of complications when mixing AWT controls, which use native controls, with Swing controls in a GUI (seeMixing AWT and Swing components).

Finally, in terms of visual composition and management, Swing favorsrelative layouts (which specify the positional relationships between components) as opposed to absolute layouts (which specify the exact location and size of components). This bias towards "fluid"' visual ordering is due to its origins in theapplet operating environment that framed the design and development of the original Java GUI toolkit. (Conceptually, this view of the layout management is quite similar to that which informs the rendering of HTML content in browsers, and addresses the same set of concerns that motivated the former.)

Relationship to AWT

[edit]
AWT and Swing class hierarchy

Since early versions of Java, a portion of theAbstract Window Toolkit (AWT) has provided platform-independent APIs for user interface components. In AWT, each component is rendered and controlled by a native peer component specific to the underlying windowing system.

By contrast, Swing components are often described aslightweight because they do not require allocation of native resources in the operating system's windowing toolkit. The AWT components are referred to asheavyweight components.[12]

Much of the Swing API is generally a complementary extension of the AWT rather than a direct replacement. In fact, every Swing lightweight interface ultimately exists within an AWT heavyweight component because all of the top-level components in Swing (JApplet,JDialog,JFrame, andJWindow) extend an AWT top-level container. Prior toJava 6 Update 10, the use of both lightweight and heavyweight components within the same window was generally discouraged due toZ-order incompatibilities. However, later versions of Java have fixed these issues, and both Swing and AWT components can now be used in one GUI without Z-order issues.

The core rendering functionality used by Swing to draw its lightweight components is provided byJava 2D, another part of JFC.

This sectionmay contain materialunrelated to the topic of the article. Please helpimprove this section or discuss this issue on thetalk page.(May 2012) (Learn how and when to remove this message)

Relationship to SWT

[edit]

TheStandard Widget Toolkit (SWT) is a competing toolkit originally developed byIBM and now maintained by theEclipsecommunity. SWT's implementation has more in common with the heavyweight components of AWT. This confers benefits such as more accurate fidelity with the underlying native windowing toolkit, at the cost of an increased exposure to the native platform in the programming model.

There has been significant debate and speculation about the performance of SWT versus Swing; some hinted that SWT's heavy dependence onJNI would make it slower when the GUI component and Java need to communicate data, but faster at rendering when the data model has been loaded into the GUI, but this has not been confirmed either way.[13] A fairly thorough set of benchmarks in 2005 concluded that neither Swing nor SWT clearly outperformed the other in the general case.[14]

Examples

[edit]

Hello World

[edit]

This example Swing application creates a single window with "Hello, world!" inside:

// Hello.java (Java SE 8)importjavax.swing.*;publicclassHelloextendsJFrame{publicHello(){super("Hello World");setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);add(newJLabel("Hello, world!"));pack();setVisible(true);}publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(Hello::new);}}

The firstimport includes all the public classes and interfaces from thejavax.swing package.

TheHello classextends theJFrame class; theJFrame class implements awindow with atitle bar and a closecontrol.

TheHello()constructor initializes the frame by first calling the superclass constructor, passing the parameter"Hello World", which is used as the window's title. It then calls thesetDefaultCloseOperation(int) method inherited fromJFrame to set the default operation when the close control on the title bar is selected toWindowConstants.EXIT_ON_CLOSE – this causes theJFrame to be disposed of when the frame is closed (as opposed to merely hidden), which allows the Java virtual machine to exit and the program to terminate. Next, aJLabel is created for the string"Hello, world!" and theadd(Component) method inherited from theContainer superclass is called to add the label to the frame. Thepack() method inherited from theWindow superclass is called to size the window and lay out its contents. ThesetVisible(boolean) method inherited from theComponent superclass is called with the Boolean parametertrue, which causes the frame to be displayed.

Themain() method is called by the Java virtual machine when the program starts. Itinstantiates a newHello frame. The code uses theinvokeLater(Runnable) method to invoke the constructor from the AWTevent dispatching thread in order to ensure the code is executed in athread-safe manner. Once the frame is displayed, exiting themain method does not cause the program to terminate because the event dispatching thread remains active until all of the Swing top-level windows have been disposed.

Window with Button

[edit]
The basic example code running onWindows 7

The following is a rather simple Swing-based program. It displays a window (aJFrame) containing a label and a button.

importjava.awt.FlowLayout;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.WindowConstants;importjavax.swing.SwingUtilities;publicclassSwingExampleimplementsRunnable{@Overridepublicvoidrun(){// Create the windowJFramef=newJFrame("Hello World!");// Sets the behavior for when the window is closedf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// Add a layout manager so that the button is not placed on top of the labelf.setLayout(newFlowLayout());// Add a label and a buttonf.add(newJLabel("Hello, world!"));f.add(newJButton("Press me!"));// Arrange the components inside the windowf.pack();// By default, the window is not visible. Make it visible.f.setVisible(true);}publicstaticvoidmain(String[]args){// Schedules the application to be run at the correct time in the event queue.SwingUtilities.invokeLater(newSwingExample());}}

Notice how all instantiation and handling of Swing components are done by implementing the Runnable interface. This is then run on theEvent Dispatch Thread by use of the methodSwingUtilities.invokeLater(Runnable)), created in the main method (seeSwing and thread safety). Although Swing code can be run without using this technique (for instance, by not implementing Runnable and moving all commands from the run method to the main method), it is considered to be good form, as Swing is notthread-safe, meaning that invoking resources from multiple threads can result in thread interference and memory consistency errors.[15]

Text Field

[edit]

Text fields enable users to input text or data into your application. Creating a text field in Swing is straightforward – instantiate a JTextField object and add it to a container.

importjavax.swing.*;publicclassTextFieldExample{publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(()->{// Create a JFrameJFrameframe=newJFrame("Text Field Example");// Create a JTextFieldJTextFieldtextField=newJTextField(20);// Add the text field to the JFrameframe.add(textField);// Set the size of the JFrame and make it visibleframe.setSize(300,200);frame.setVisible(true);});}}

Enhancing functionality in text fields improves user interaction. By attaching DocumentListener interfaces, you can dynamically monitor changes in the text content, enabling real-time validation, formatting, or auto-completion of input data.

Validating text field input is crucial for ensuring data integrity and preventing errors. Swing provides multiple validation techniques, including regular expressions, input masks, or custom validation logic. By implementing InputVerifier interfaces, you can define specific validation rules and offer immediate feedback to users when input is invalid.[16]

Another example

[edit]

In this example let javax.swing.JFrame be super class and add our own widget(s) to it (in this case, a JButton).

importjavax.swing.JFrame;importjavax.swing.JButton;importjavax.swing.JOptionPane;importjavax.swing.SwingUtilities;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;publicclassSampleextendsJFrame{publicSample(){super();this.setTitle("HelloApp");this.getContentPane().setLayout(null);this.setBounds(100,100,180,140);this.add(makeButton());this.setVisible(true);this.setDefaultCloseOperation(EXIT_ON_CLOSE);}privateJButtonmakeButton(){finalJButtonb=newJButton();b.setText("Click me!");b.setBounds(40,40,100,30);b.addActionListener((ActionEvente)->JOptionPane.showMessageDialog(b,"Hello World!"));returnb;}publicstaticvoidmain(String[]args)throwsInvocationTargetException,InterruptedException{// Swing calls must be run by the event dispatching thread.SwingUtilities.invokeAndWait(()->newSample());}}

The layout is set to null using theContainer.setLayout(LayoutManager) method since JFrame uses java.awt.BorderLayout as its default layout-manager. With BorderLayout anything which is added to the container is placed in the center and stretched to accommodate any other widgets. Most real world GUI applications would prefer to use a layout-manager instead of placing everything on absolute co-ordinates.[17]

See also

[edit]

References

[edit]

Citations

[edit]
  1. ^"What is Java Swing? - Definition from Techopedia".Techopedia Inc. Retrieved2018-11-03.
  2. ^Yap, Chee (2003-04-30)."JAVA SWING GUI TUTORIAL".New York University (NYU). Retrieved2018-11-03.
  3. ^ab"Developing a basic GUI application using JavaFX in Eclipse". July 2017.
  4. ^"Sun and Netscape to jointly develop Java Foundation Classes".Netscape Communications Corporation. 1997-04-02. Archived fromthe original on 2012-05-09. Retrieved2011-08-08.
  5. ^"SUN DELIVERS NEXT VERSION OF THE JAVA PLATFORM".Sun Microsystems. August 2007. Archived fromthe original on August 16, 2007. Retrieved2012-01-08.The Java Foundation Classes are now core to the Java 2 platform and includes:The Project Swing set of GUI components, Drag & Drop, Java 2D API which provides new 2D and AWT graphics capabilities as well as printing support, The Java look and feel interface, A new Accessibility API
  6. ^Gabriela Motroc (March 8, 2018)."JDK 11 update: JavaFX will be decoupled from the JDK". Archived fromthe original on 2019-03-26.
  7. ^Smith, Donald (March 7, 2018)."The Future of JavaFX and Other Java Client Roadmap Updates".
  8. ^Zakhour, Sharon."Why is Swing Called Swing?".The Java Tutorials Blog. Retrieved24 September 2020.
  9. ^John, Yu (27 August 2003)."Rich clients emerge as alternatives for Web applications".ComputerWorld. Retrieved24 September 2020.
  10. ^Zukowski, John (August 21, 2007)."Swing threading and the event-dispatch thread".JavaWorld. Retrieved2020-07-26.
  11. ^Fowler, Amy."A Swing Architecture Overview".Sun Microsystems. Retrieved2020-07-26.
  12. ^Zakhour, Sharon; Petrov, Anthony (April 2010)."Mixing Heavyweight and Lightweight Components".Oracle. Retrieved2020-07-26.
  13. ^Strenn, Stephen (March 3, 2006)."Swing vs. SWT Performance - Have a Look at the Call Stacks".Javalobby. Archived from the original on 2017-09-17.
  14. ^Žagar, Klemen; Križnar, Igor (March 3, 2006)."SWT Vs. Swing Performance Comparison"(PDF) (1.4 ed.). Cosylab. Archived fromthe original(PDF) on 2015-05-26.It is hard to give a rule-of-thumb where SWT would outperform Swing, or vice versa. In some environments (e.g., Windows), SWT is a winner. In others (Linux,VMware hosting Windows), Swing and its redraw optimization outperform SWT significantly. Differences in performance are significant: factors of 2 and more are common, in either direction.
  15. ^"The Event Dispatch Thread".docs.oracle.com.
  16. ^https://geeksprogramming.com/java-swing-tutorial-for-beginners/ The Event Dispatch Thread
  17. ^Eckel, Bruce (2006).Thinking in Java(PDF) (4 ed.). Prentice Hall. p. 942.ISBN 978-0131872486. Archived fromthe original(PDF) on 14 May 2016. Retrieved13 May 2016.
  18. ^"JavaFX Developer Home".www.oracle.com.

Sources

[edit]

External links

[edit]
Java desktop
APIs
Deprecated APIs
Open-source
Low-level platform-specific
OnAmigaOS
OnClassic Mac OS,macOS
OnWindows
OnUnix
OnBeOS,Haiku
OnAndroid
CLI
Low Level Cross-platform
CLI
C
Java
High-level, platform-specific
OnAmigaOS
OnClassic Mac OS,macOS
Object Pascal
Objective-C,Swift
C++
CLI
OnWindows
CLI
C++
Object Pascal
OnUnix andX11
High-level, cross-platform
C
C++
Objective-C
CLI
Adobe Flash
Go
Haskell
Java
JavaScript
Common Lisp
Lua
Pascal
Object Pascal
Perl
PHP
Python
Ruby
Tcl
XML
shell
Dart
Retrieved from "https://en.wikipedia.org/w/index.php?title=Swing_(Java)&oldid=1319459696"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp