Swing (Java): Difference between revisions

Content deleted Content added
History: Remove url-status=unfit
Undo vandalism
 
(4 intermediate revisions by 3 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)&nbsp;&ndash; an [[Application programming interface|API]] for providing a [[graphical user interface]] (GUI) for Java programs.
 
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 booleanBoolean parameter <code>'''true'''</code>, which causes the frame to be displayed.
 
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.