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!
===Window with Button=== [[File:Swing example on Windows 7.png|thumb|right|The basic example code running on [[Windows 7]]]] The following is a rather simple Swing-based program. It displays a window (a {{Javadoc|module=java.desktop|package=javax.swing|class=JFrame|text=JFrame|monotype=y}}) containing a label and a button. <syntaxhighlight lang="java"> import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.WindowConstants; import javax.swing.SwingUtilities; public class SwingExample implements Runnable { private JFrame f; public SwingExample() { // Create the window f = new JFrame("Hello World!"); // Sets the behavior for when the window is closed f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // Add a layout manager so that the button is not placed on top of the label f.setLayout(new FlowLayout()); // Add a label and a button f.add(new JLabel("Hello, world!")); f.add(new JButton("Press me!")); } @Override public void run() { // Arrange the components inside the window f.pack(); // By default, the window is not visible. Make it visible. f.setVisible(true); } public static void main(String[] args) { // Schedules the application to be run at the correct time in the event queue. SwingUtilities.invokeLater(new SwingExample()); } } </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>{{Cite web|url=https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html|title=The Event Dispatch Thread|website=docs.oracle.com}}</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)