Content deleted Content added
mNo edit summary |
Undo vandalism |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 1:
{{Short description|Java-based GUI toolkit}}
[[File:Gui-widgets.png|thumb|Example Swing widgets in Java]]
'''Swing''' is a [[Graphical user interface|GUI]] [[widget toolkit]] for [[Java (programming language)|Java]].<ref>{{Cite news|url=https://www.techopedia.com/definition/26102/java-swing|title=What is Java Swing? - Definition from Techopedia|work=Techopedia Inc.|access-date=2018-11-03|language=en}}</ref> It is part of [[Oracle Corporation|Oracle]]'s [[Java Foundation Classes]] (JFC) – an [[Application programming interface|API]] for providing a [[graphical user interface]] (GUI) for Java programs.
Line 25 ⟶ 26:
Swing introduced a mechanism that allowed the [[look and feel]] of every component in an application to be altered without making substantial changes to the application code. The introduction of support for a [[pluggable 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 the [[Java Platform, Standard Edition|Java Standard Edition]] since release 1.2.<ref>{{cite web|url=http://www.sun.com/smi/Press/sunflash/1998-12/sunflash.981208.9.xml |title=SUN DELIVERS NEXT VERSION OF THE JAVA PLATFORM |date=August 2007 |publisher=[[Sun Microsystems]] |quote=''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 '' |access-date=2012-01-08
Development of Swing's successor, [[JavaFX]], started in 2005, and it was officially introduced two years later at JavaOne 2007.<ref>{{Cite web|url=https://jaxenter.com/jdk-11-javafx-separate-module-142186.html|archive-url=https://web.archive.org/web/20190326211553/https://jaxenter.com/jdk-11-javafx-separate-module-142186.html|archive-date=2019-03-26|title=JDK 11 update: JavaFX will be decoupled from the JDK|date=March 8, 2018 |author=Gabriela Motroc}}</ref> 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, having [[Cascading Style Sheets|CSS]] styling, sleek design controls, and the use of [[FXML]] and Scene Builder.<ref>{{Cite web|url=https://opensourceforu.com/2017/07/developing-basic-gui-application-using-javafx-eclipse/|title=Developing a basic GUI application using JavaFX in Eclipse|date=July 2017 }}</ref> In 2018, JavaFX was made a part of the OpenJDK under the OpenJFX project to increase the pace of its development.<ref>{{Cite web|url=https://blogs.oracle.com/java-platform-group/the-future-of-javafx-and-other-java-client-roadmap-updates|title=The Future of JavaFX and Other Java Client Roadmap Updates|last=Smith|first=Donald|date=March 7, 2018}}</ref>
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,<ref>{{cite web |last1=Zakhour |first1=Sharon |title=Why is Swing Called Swing? |url=https://blogs.oracle.com/thejavatutorials/why-is-swing-called-swing |website=The Java Tutorials Blog |access-date=24 September 2020}}</ref> Tim Prinzing, Jonni Kanerva, and Jeannette Hung & Jim Graham (2D Graphics).<ref>{{cite web |last1=John |first1=Yu |title=Rich clients emerge as alternatives for Web applications |url=https://www.computerworld.com/article/2571634/rich-clients-emerge-as-alternatives-for-web-applications.html |website=ComputerWorld |date=27 August 2003 |access-date=24 September 2020}}</ref>
Line 78 ⟶ 79:
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
Line 119 ⟶ 120:
The <code>'''Hello'''</code> class <code>'''extends'''</code> the '''{{Javadoc|module=java.desktop|package=javax.swing|class=JFrame|text=JFrame|monotype=y}}''' class; the <code>JFrame</code> class implements a [[window (computing)|window]] with a [[title bar]] and a close [[Graphical control element|control]].
The <code>'''Hello()'''</code> [[constructor (object-oriented programming)|constructor]] initializes the frame by first calling the superclass constructor, passing the parameter <code>"Hello World"</code>, which is used as the window's title. It then calls the '''{{Javadoc|module=java.desktop|package=javax.swing|class=JFrame|member=setDefaultCloseOperation(int)|text=setDefaultCloseOperation(int)|monotype=y}}''' method inherited from <code>JFrame</code> to set the default operation when the close control on the title bar is selected to '''{{Javadoc|module=java.desktop|package=javax.swing|class=WindowConstants|member=EXIT_ON_CLOSE|text=WindowConstants.EXIT_ON_CLOSE|monotype=y}}'''{{snd}} this causes the <code>JFrame</code> 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, a '''{{Javadoc|module=java.desktop|package=javax.swing|class=JLabel|text=JLabel|monotype=y}}''' is created for the string '''"Hello, world!"''' and the '''{{Javadoc|module=java.desktop|package=java.awt|class=Container|member=add(java.awt.Component)|text=add(Component)|monotype=y}}''' method inherited from the {{Javadoc|module=java.desktop|package=java.awt|class=Container|text=Container|monotype=y}} superclass is called to add the label to the frame. The '''{{Javadoc|module=java.desktop|package=java.awt|class=Window|text=pack()|member=pack()|monotype=y}}''' method inherited from the {{Javadoc|module=java.desktop|package=java.awt|class=Window|text=Window|monotype=y}} superclass is called to size the window and lay out its contents. The '''{{Javadoc|module=java.desktop|package=java.awt|class=Component|member=setVisible(boolean)|text=setVisible(boolean)|monotype=y}}''' method inherited from the {{Javadoc|module=java.desktop|package=java.awt|class=Component|text=Component|monotype=y}} superclass is called with the
The <code>'''main()'''</code> method is called by the Java virtual machine when the program starts. It [[Object (computer science)|instantiates]] a new '''<code>Hello</code>''' frame. The code uses the '''{{Javadoc|module=java.desktop|package=javax.swing|class=SwingUtilities|member=invokeLater(java.lang.Runnable)|text=invokeLater(Runnable)|monotype=y}}''' method to invoke the constructor from the AWT [[event dispatching thread]] in order to ensure the code is executed in a [[thread-safe]] manner. Once the frame is displayed, exiting the <code>main</code> 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.
Line 166 ⟶ 167:
</syntaxhighlight>
Notice how all instantiation and handling of Swing components are done by creating an instance of the class,
which implements the Runnable interface. This is then run on the [[Event Dispatch Thread]] by use of the method {{Javadoc:SE|member=invokeLater(Runnable)|javax/swing|SwingUtilities|invokeLater(java.lang.Runnable)|module=java.desktop}}), created in the main method (see [[Event dispatching thread#Swing and thread safety|Swing 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 not [[Thread safety|thread-safe]], meaning that invoking resources from multiple threads can result in thread interference and memory consistency errors.<ref>
===Text Field===
|