Swing
Swing
Swing is a Java-based graphical user interface (GUI) toolkit that is part of the Java Foundation Classes (JFC). It provides a set of "lightweight" (all-Java language) components that work the same on all platforms, allowing developers to create cross-platform applications with a consistent look and feel.
Overview[edit | edit source]
Swing was developed by Sun Microsystems and is now maintained by Oracle Corporation as part of the Java Standard Edition. It was introduced in 1997 as part of the Java Development Kit (JDK) 1.2. Swing is built on top of the Abstract Window Toolkit (AWT), which is the original Java GUI toolkit.
Swing provides a richer set of GUI components than AWT, including advanced components like trees, tables, and text areas. It also supports pluggable look and feel, allowing applications to mimic the appearance of native applications on different platforms or to have a unique look.
Features[edit | edit source]
- Lightweight Components: Unlike AWT components, which are "heavyweight" and rely on the native system's GUI components, Swing components are "lightweight" and written entirely in Java.
- Pluggable Look and Feel: Swing allows developers to change the appearance of their applications by switching between different "look and feel" themes, such as the default "Metal" look, "Nimbus", or the native look of the operating system.
- MVC Architecture: Swing components follow the Model-View-Controller (MVC) design pattern, which separates the data (model), the user interface (view), and the interaction logic (controller).
- Customizable Components: Developers can create custom components by extending existing Swing components or by creating new ones from scratch.
- Event Handling: Swing uses the event delegation model for handling user interactions, which is more efficient and flexible than the event handling model used in AWT.
Components[edit | edit source]
Swing provides a wide range of components, including:
- JButton: A push button that can trigger an action when clicked.
- JLabel: A display area for a short text string or an image.
- JTextField: A single-line text input field.
- JTextArea: A multi-line area to display/edit text.
- JTable: A component that displays data in a two-dimensional table format.
- JTree: A component that displays a hierarchical tree of data.
- JPanel: A generic container for holding other components.
Usage[edit | edit source]
To use Swing in a Java application, developers typically import the `javax.swing` package and create a class that extends `JFrame` or another top-level container. They then add components to the frame and set up event listeners to handle user interactions.
Example:
```java import javax.swing.*; import java.awt.event.*;
public class HelloWorldSwing {
public static void main(String[] args) { // Create a new JFrame JFrame frame = new JFrame("Hello World Swing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label JLabel label = new JLabel("Hello, World!"); frame.getContentPane().add(label);
// Display the window frame.pack(); frame.setVisible(true); }
} ```
Also see[edit | edit source]
{{{1}}}
Template:Software
Search WikiMD
Ad.Tired of being Overweight? Try W8MD's physician weight loss program.
Semaglutide (Ozempic / Wegovy and Tirzepatide (Mounjaro / Zepbound) available.
Advertise on WikiMD
WikiMD's Wellness Encyclopedia |
Let Food Be Thy Medicine Medicine Thy Food - Hippocrates |
Translate this page: - East Asian
中文,
日本,
한국어,
South Asian
हिन्दी,
தமிழ்,
తెలుగు,
Urdu,
ಕನ್ನಡ,
Southeast Asian
Indonesian,
Vietnamese,
Thai,
မြန်မာဘာသာ,
বাংলা
European
español,
Deutsch,
français,
Greek,
português do Brasil,
polski,
română,
русский,
Nederlands,
norsk,
svenska,
suomi,
Italian
Middle Eastern & African
عربى,
Turkish,
Persian,
Hebrew,
Afrikaans,
isiZulu,
Kiswahili,
Other
Bulgarian,
Hungarian,
Czech,
Swedish,
മലയാളം,
मराठी,
ਪੰਜਾਬੀ,
ગુજરાતી,
Portuguese,
Ukrainian
Medical Disclaimer: WikiMD is not a substitute for professional medical advice. The information on WikiMD is provided as an information resource only, may be incorrect, outdated or misleading, and is not to be used or relied on for any diagnostic or treatment purposes. Please consult your health care provider before making any healthcare decisions or for guidance about a specific medical condition. WikiMD expressly disclaims responsibility, and shall have no liability, for any damages, loss, injury, or liability whatsoever suffered as a result of your reliance on the information contained in this site. By visiting this site you agree to the foregoing terms and conditions, which may from time to time be changed or supplemented by WikiMD. If you do not agree to the foregoing terms and conditions, you should not enter or use this site. See full disclaimer.
Credits:Most images are courtesy of Wikimedia commons, and templates Wikipedia, licensed under CC BY SA or similar.
Contributors: Prab R. Tumpati, MD