Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit (AWT). Swing provides a look and feel that emulates the look and feel of several platforms, and also supports a pluggable 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 the CSS / FXML based framework that it intended to be the successor to Swing, called JavaFX.[3]
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 Standard Edition since release 1.2.[5] The Swing classes and components are contained in the javax.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, having CSS styling, sleek design controls, and the use of FXML and Scene Builder.[7] In 2018, JavaFX was made a part of the OpenJDK under the OpenJFX project to increase the pace of its development.[8]
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,[9] Tim Prinzing, Jonni Kanerva, and Jeannette Hung & Jim Graham (2D Graphics).[10]
Architecture
Swing is a platform-independent, "model–view–controller" GUI framework for Java, which follows a single-threaded programming model.[11] Additionally, this framework provides a layer of abstraction between the code structure and graphic presentation of a Swing-based GUI.
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 LookAndFeel.
Swing is a component-based framework, whose components are all ultimately derived from the JComponent 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 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 AWT 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 the JComponent model.
Loosely coupled and MVC
The Swing library makes heavy use of the model–view–controller software design pattern,[12] 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 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 JComponent 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 DAO and/or 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 specialization into a meaningful event for the GUI component.
For example, the JTable has a model called TableModel that describes an interface for how a table would access tabular data. A default implementation of this operates on a two-dimensional 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 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 Mixing AWT and Swing components).
Finally, in terms of visual composition and management, Swing favors 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 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
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.[13]
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, and JWindow) extend an AWT top-level container. Prior to 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.
The Standard Widget Toolkit (SWT) is a competing toolkit originally developed by IBM and now maintained by the Eclipsecommunity. 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 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.[14] A fairly thorough set of benchmarks in 2005 concluded that neither Swing nor SWT clearly outperformed the other in the general case.[15]
Examples
Hello World
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 first import includes all the public classes and interfaces from the javax.swing package.
The Hello class extends the JFrame class; the JFrame class implements a window with a title bar and a close control.
The Hello()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 the setDefaultCloseOperation(int) method inherited from JFrame to set the default operation when the close control on the title bar is selected to WindowConstants.EXIT_ON_CLOSE – this causes the JFrame 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 JLabel is created for the string "Hello, world!" and the add(Component) method inherited from the Container superclass is called to add the label to the frame. The pack() method inherited from the Window superclass is called to size the window and lay out its contents. The setVisible(boolean) method inherited from the Component superclass is called with the Boolean parameter true, which causes the frame to be displayed.
The main() method is called by the Java virtual machine when the program starts. It instantiates a new Hello frame. The code uses the invokeLater(Runnable) 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 main 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
The following is a rather simple Swing-based program. It displays a window (a JFrame) 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{privateJFramef;publicSwingExample(){// Create the windowf=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!"));}@Overridepublicvoidrun(){// 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 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 SwingUtilities.invokeLater(Runnable)), created in the main method (see 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-safe, meaning that invoking resources from multiple threads can result in thread interference and memory consistency errors.[16]
Text Field
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){// 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.[17]
Another example
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{privatefinalJButtonb=newJButton();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(){b.setText("Click me!");b.setBounds(40,40,100,30);b.addActionListener(newActionListener(){publicvoidactionPerformed(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 the Container.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. Of course, most real world GUI applications would prefer to use a layout-manager instead of placing everything on absolute co-ordinates.[18]
^"SUN DELIVERS NEXT VERSION OF THE JAVA PLATFORM". Sun Microsystems. August 2007. Archived from the original on August 16, 2007. Retrieved 2012-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
^Žagar, Klemen; Križnar, Igor (March 3, 2006). "SWT Vs. Swing Performance Comparison"(PDF) (1.4 ed.). Cosylab. Archived from the 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.
Artikel ini sebatang kara, artinya tidak ada artikel lain yang memiliki pranala balik ke halaman ini.Bantulah menambah pranala ke artikel ini dari artikel yang berhubungan atau coba peralatan pencari pranala.Tag ini diberikan pada Oktober 2022. Noel Evelyn Norris (25 Desember 1918 - 15 Februari 2014) adalah pendidik berkewarganegaraan Singapura yang dikenal terkait erat dengan Raffles Girls' Primary School. Norris adalah kepala sekolah Crescent Girls' School. Norris adalah relawan di Angkatan...
Ukiran kompas al-Qūhī's untuk menggambar bagian berbentuk kerucut Abu Sahal Wayjan Ibnu Rustam al-Quhī (Persia: ابوسهل بیژن کوهیcode: fa is deprecated , alih aksara: Abusahl Bijan-e Koohi) adalah matematikawan, fisikawan, dan astronom berkebangsaan Persia.[1][2] Dia berasal dari Kuh (atau Quh), sebuah wilayah di Tabaristan, Amol, dan kemudian dia pindah ke Bagdad, Kekhalifahan Abbasiyah pada abad ke-10. Dia dianggap sebagai salah satu ahli geometri terhebat, d...
العلاقات الأذربيجانية اللوكسمبورغية أذربيجان لوكسمبورغ أذربيجان لوكسمبورغ تعديل مصدري - تعديل العلاقات الأذربيجانية اللوكسمبورغية هي العلاقات الثنائية التي تجمع بين أذربيجان ولوكسمبورغ.[1][2][3][4][5] مقارنة بين البلدين هذه مقارنة عام�...
The following is a timeline of the history of Jersey City, New Jersey, United States. This is a dynamic list and may never be able to satisfy particular standards for completeness. You can help by adding missing items with reliable sources. Prior to 19th century Main article: Bergen, New Netherland History ofNew Jersey Colonial period American Revolution Nineteenth century Twentieth century Twenty-first century vte 19th century Map of Jersey City, New Jersey, 1848 1802 - Population of Paulus...
North American professional box lacrosse league For the unrelated defunct league, see National Lacrosse League (1974–75). Major Indoor Lacrosse League redirects here. Not to be confused with Major League Lacrosse or Premier Lacrosse League. National Lacrosse LeagueCurrent season, competition or edition: 2024 NLL seasonFormerlyEagle Pro Box Lacrosse League (1986–1988) Major Indoor Lacrosse League (1988–1997)SportBox lacrosseFounded1986; 38 years ago (1986)First season19...
lítMột lít là thể tích của một hình lập phương có cạnh 10 cm.Thông tin đơn vịHệ thống đơn vịHệ đo lường quốc tếĐơn vị củaThể tíchKí hiệuL hoặc l hoặc (ℓ)[1] Được đặt tên theolitronChuyển đổi đơn vị 1 L trong ...... bằng ... Đơn vị cơ sở SI 10−3 m3 Hoa Kỳ đơn vị thông lệ ≈ 0264...
ReleaseEpisode The X-FilesNomor episodeMusim 9Episode 17SutradaraKim MannersTeleplayDavid AmannPengarangJohn ShibanDavid AmmanKode produksi9ABX16Tanggal siar5 Mei 2002Durasi44 menitKronologi episode ← SebelumnyaWilliam Selanjutnya →Sunshine Days Release adalah episode ketujuh belas dari musim kesembilan dari serial televisi fiksi ilmiah Amerika Serikat The X-Files. Episode tersebut aslinya disiarkan dalam saluran Fox pada 5 Mei 2002. Daftar pustaka Kessenich, Tom (2002). Ex...
Cet article est une ébauche concernant un footballeur bulgare. Vous pouvez partager vos connaissances en l’améliorant (comment ?) selon les recommandations des projets correspondants. Mladen Vasilev Biographie Nom Младен Василев Nationalité Bulgare Naissance 29 juillet 1947 (76 ans) Slivnitsa (Bulgarie) Poste Milieu de terrain Parcours professionnel1 AnnéesClub 0M.0(B.) 1965-1968 Slivnishki Geroi 1968-1969 PFK Levski Sofia 12 (2) 1969-1977 Akademik Sofia 209 (68)...
Extinct genus of dinosaurs GeranosaurusTemporal range: Early Jurassic, ~190–182 Ma PreꞒ Ꞓ O S D C P T J K Pg N Holotype jaw Scientific classification Domain: Eukaryota Kingdom: Animalia Phylum: Chordata Clade: Dinosauria Clade: †Ornithischia Family: †Heterodontosauridae Genus: †GeranosaurusBroom, 1911 Type species †Geranosaurus atavusBroom, 1911 Geranosaurus (meaning crane reptile) is a genus of heterodontosaurid ornithischian dinosaur from the Early Jurassic. The type and ...
5°03′32″S 119°33′42″E / 5.0589033°S 119.5616603°E / -5.0589033; 119.5616603 Bonto Mate'neDesaKantor Desa Bonto Mate'ne di Dusun BorongloeNegara IndonesiaProvinsiSulawesi SelatanKabupatenMarosKecamatanMandaiKode pos90552[1]Kode Kemendagri73.09.01.2005 Luas12,69 km² tahun 2017Jumlah penduduk7.074 jiwa tahun 2017Kepadatan557,45 jiwa/km² tahun 2017Jumlah RT40Jumlah RW8 Untuk pengertian lain, lihat Bonto Matene/Bontomatene. Bonto Mate'ne (Bugis...
Bugle call Sunset (bugle call)Notation of the bugle call.GenreBugle callPublished1932 (1932) Sunset, also known as the Retreat Call, is a bugle call played in United Kingdom and British Commonwealth countries to signal the end of the official military day.[1] In common with all bugle calls, it consists only of notes from a single overtone series. This allows it to be playable on a bugle or equivalently on a trumpet without moving the valves. The call is regularly heard performed ...
Ships in the Royal Australian Navy Supply class Stalwart in Cockburn Sound, December 2021 Class overview NameSupply class BuildersNavantia, Ferrol Operators Royal Australian Navy Preceded byHMAS Sirius, HMAS Success Cost € 420 million (US$465 million) for two ships € 210 million (US$232 million) per unit Built2018–present Planned2 Completed2 Active2 General characteristics TypeReplenishment oiler Displacement19,500 tonnes Length173.9 m (570 ft 6 i...
Phönizisches Stammland, grün unterlegt Handelsrouten und wichtige Städte der Phönizier; das Stammland rot unterlegt Phönizier (auch Phönikier oder Phöniker, altgriechisch Φοίνικες Phoínikes) ist ein Name für die semitischsprachige Bevölkerung, die im Altertum (im 1. Jahrtausend v. Chr.) die Levante besiedelte. Der als Phönizien (altgriechisch Φοινίκη Phoiníkē) bezeichnete geografische Raum erstreckt sich entlang der östlichen Mittelmeerküste und ihrem Hinterland,...
1996 Armenian presidential election ← 1991 22 September 1996 1998 → Nominee Levon Ter-Petrosyan Vazgen Manukyan Party HHSh NDU Popular vote 646,888 516,129 Percentage 51.34% 40.97% President before election Levon Ter-Petrosyan HHSh Elected President Levon Ter-Petrosyan HHSh Presidential elections were held in Armenia on 22 September 1996.[1] The result was a victory for Levon Ter-Petrosyan, who received 51% of the vote. Turnout was 60%.[2] Ba...
French appellate court, 1200s–1790 This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Parlement of Paris – news · newspapers · books · scholar · JSTOR (November 2022) (Learn how and when to remove this message) Lit de justice held by Louis XV of France at the Parlement of Paris in 1715 The Parlement of Paris ...
Garment worn by women of Ancient Greece The Mourning Athena relief with Athena wearing a plain doric overfold chiton, c. 460 BC So-called Exaltation de la Fleur (exaltation of the flower), fragments from a secondary grave stele: two women wearing a peplos and kekryphalos (hairnet), hold poppy or pomegranate flowers, and maybe a small bag of seeds. Parian marble, c. 470–460 BC. From Pharsalos, Thessaly. A peplos (Greek: ὁ πέπλος) is a body-length garment established as ty...
Questa voce o sezione sull'argomento cristianesimo non cita le fonti necessarie o quelle presenti sono insufficienti. Puoi migliorare questa voce aggiungendo citazioni da fonti attendibili secondo le linee guida sull'uso delle fonti. Segui i suggerimenti del progetto di riferimento. Lo scisma tricapitolino o scisma dei tre capitoli è stata una divisione all'interno della Chiesa avvenuta tra i secoli VI e VII, quando un folto gruppo di vescovi, per lo più occidentali, interruppe le rel...
It has been suggested that Oeil-de-boeuf be merged into this article. (Discuss) Proposed since August 2024. Circular opening in the center of a dome or in a wall Oculi redirects here. For the third Sunday in Lent, see Oculi Sunday. Renaissance oculus on the west facade of the Cour Carrée of the Louvre Palace, with figures of war and peace, sculpted by Jean Goujon and designed by Pierre Lescot, 1548[1] An oculus (from Latin oculus 'eye'; pl.: oculi) is a circular opening...