Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Swing (Java)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Architecture== Swing is a platform-independent, "[[model–view–controller]]" [[GUI]] framework for Java, which follows a single-[[thread (computing)|thread]]ed programming model.<ref>{{cite web |last1=Zukowski |first1=John |date=2007-08-21 |df=mdy |url=https://www.infoworld.com/article/2077754/swing-threading-and-the-event-dispatch-thread.html |title=Swing threading and the event-dispatch thread |work=[[JavaWorld]] |access-date=2020-07-26}}</ref> Additionally, this framework provides a layer of abstraction between the code structure and graphic presentation of a Swing-based GUI. ===Foundations=== Swing is platform-independent because it is completely written in Java. Complete documentation for all Swing classes can be found in the [https://docs.oracle.com/javase/6/docs/api/ Java API Guide] for Version 6 or the [https://docs.oracle.com/javase/8/docs/api/ Java Platform Standard Edition 8 API Specification] for Version 8. ====Extensible==== 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 via {{Javadoc|module=java.desktop|package=javax.swing|class=LookAndFeel|text=LookAndFeel|monotype=y}}. Swing is a '''component-based framework''', whose components are all ultimately derived from the {{Javadoc|module=java.desktop|package=javax.swing|class=JComponent|text=JComponent|monotype=y}} class. Swing objects asynchronously fire events, have bound properties, and respond to a documented set of methods specific to the component. Swing components are [[JavaBeans]] components, compliant with the [https://www.oracle.com/java/technologies/javase/javabeans-spec.html JavaBeans specification]. ====Configurable==== 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 of [[hot 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==== Swing's high level of flexibility is reflected in its inherent ability to override the native host [[operating 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 an [[Abstract Window Toolkit|AWT]] container, since (Swing's) {{Javadoc|module=java.desktop|package=javax.swing|class=JComponent|text=JComponent|monotype=y}} 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 the {{Javadoc|module=java.desktop|package=javax.swing|class=JComponent|text=JComponent|monotype=y}} model. ====Loosely coupled and MVC==== The Swing library makes heavy use of the [[model–view–controller]] software [[design pattern (computer science)|design pattern]],<ref>{{cite web |last1=Fowler |first1=Amy |url=https://www.oracle.com/java/technologies/a-swing-architecture.html |title=A Swing Architecture Overview |publisher=[[Sun Microsystems]] |access-date=2020-07-26}}</ref> which conceptually decouples the data being viewed from the user interface controls through which it is viewed. Because of this, most Swing components have associated ''models'' (which are specified in terms of Java [[interface (computer science)|interfaces]]), 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 corresponding {{Javadoc|module=java.desktop|package=javax.swing|class=JComponent|text=JComponent|monotype=y}} 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 over [[Data Access Object|DAO]] and/or [[Ejb|EJB]] 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 JComponent [[Subclass (computer science)#Subclasses and superclasses|specialization]] into a meaningful event for the GUI component. For example, the {{Javadoc|module=java.desktop|package=javax.swing|class=JTable|text=JTable|monotype=y}} has a model called {{Javadoc|module=java.desktop|package=javax.swing.table|class=TableModel|text=TableModel|monotype=y}} that describes an interface for how a table would access tabular data. A default implementation of this operates on a two-dimensional [[Array data structure|array]]. 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 to [[Java version history#Java SE 6|Java 6 Update 10]], this distinction was a source of complications when mixing AWT controls, which use native controls, with Swing controls in a GUI (see [[Abstract Window Toolkit#Mixing AWT and Swing components|Mixing AWT and Swing components]]). Finally, in terms of visual composition and management, Swing favors [[Layout manager|relative 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 the [[Java applet|applet]] 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=== [[File:AWTSwingClassHierarchy.png|thumb|AWT and Swing class hierarchy]] Since early versions of Java, a portion of the [[Abstract 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 as ''lightweight'' because they do not require allocation of native resources in the operating system's windowing toolkit. The AWT components are referred to as ''heavyweight components''.<ref>{{cite web |last1=Zakhour |first1=Sharon |last2=Petrov |first2=Anthony |date=April 2010 |df=mdy |url=https://www.oracle.com/technical-resources/articles/java/mixing-components.html |title=Mixing Heavyweight and Lightweight Components |publisher=[[Oracle Corporation|Oracle]] |access-date=2020-07-26}}</ref> 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 ({{Javadoc|module=java.desktop|package=javax/swing|class=JApplet|text=JApplet|monotype=y}}, {{Javadoc|module=java.desktop|package=javax.swing|class=JDialog|text=JDialog|monotype=y}}, {{Javadoc|module=java.desktop|package=javax.swing|class=JFrame|text=JFrame|monotype=y}}, and {{Javadoc|module=java.desktop|package=javax.swing|class=JWindow|text=JWindow|monotype=y}}) extend an AWT top-level container. Prior to [[Java version history#Java SE 6|Java 6 Update 10]], the use of both lightweight and heavyweight components within the same window was generally discouraged due to [[Z-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 by [[Java 2D]], another part of JFC. {{Off topic|date=May 2012}} ===Relationship to SWT=== The [[Standard Widget Toolkit]] (SWT) is a competing toolkit originally developed by [[IBM]] and now maintained by the [[Eclipse (software)|Eclipse]] [[Free software community|community]]. 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 on [[Java Native Interface|JNI]] 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.<ref>{{cite web |last1=Strenn |first1=Stephen |date=2006-03-03 |df=mdy |url=http://www.javalobby.org/java/forums/t65168.html |title=Swing vs. SWT Performance - Have a Look at the Call Stacks |work=Javalobby |url-status=usurped |archive-url=https://web.archive.org/web/20170917172003/http://www.javalobby.org/java/forums/t65168.html |archive-date=2017-09-17}}</ref> A fairly thorough set of benchmarks in 2005 concluded that neither Swing nor SWT clearly outperformed the other in the general case.<ref>{{cite web |last1=Žagar |first1=Klemen |last2=Križnar |first2=Igor |date=2006-03-03 |df=mdy |url=http://public.cosylab.com/CSS/DOC-SWT_Vs._Swing_Performance_Comparison.pdf |title=SWT Vs. Swing Performance Comparison |edition=1.4 |publisher=Cosylab |url-status=dead |archive-url=https://web.archive.org/web/20150526225341/http://public.cosylab.com/CSS/DOC-SWT_Vs._Swing_Performance_Comparison.pdf |archive-date=2015-05-26 |quote=''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.'' }}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)