Tutorial by Examples

All Swing-related operations happen on a dedicated thread (the EDT - Event Dispatch Thread). If this thread gets blocked, the UI becomes non-responsive. Therefore, if you want to delay an operation you cannot use Thread.sleep. Use a javax.swing.Timer instead. For example the following Timer will re...
Updating the state of a Swing component must happen on the Event Dispatch Thread (the EDT). The javax.swing.Timer triggers its ActionListener on the EDT, making it a good choice to perform Swing operations. The following example updates the text of a JLabel each two seconds: //Use a timer to updat...
In the ActionListener attached to a javax.swing.Timer, you can keep track of the number of times the Timer executed the ActionListener. Once the required number of times is reached, you can use the Timer#stop() method to stop the Timer. Timer timer = new Timer( delay, new ActionListener() { priv...
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class FrameCreator { public static void main(String args[]) { //All Swing actions should be run on the Event Dispatch Thread (EDT) //Calling SwingUtilities.invokeLater ma...
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class CustomFrame extends JFrame { private static CustomFrame statFrame; public CustomFrame(String labelText) { setSize(500, 500); ...
import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class CustomFrame extends JFrame { public CustomFrame(String labelText) { setSize(500...
This code can be added to any event like a listener, button, etc. A blocking JDialog will appear and will remain until the process is complete. final JDialog loading = new JDialog(parentComponent); JPanel p1 = new JPanel(new BorderLayout()); p1.add(new JLabel("Please wait..."), BorderLa...
Assuming that you have successfully created a JFrame and that Swing has been imported... You can import Swing entirely import javax.Swing.*; or You can import the Swing Components/Frame that you intend to use import javax.Swing.Jframe; import javax.Swing.JButton; Now down to adding the J...

Page 1 of 1