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 reverse the text of on a JLabel
int delay = 2000;//specify the delay for the timer
Timer timer = new Timer( delay, e -> {
//The following code will be executed once the delay is reached
String revertedText = new StringBuilder( label.getText() ).reverse().toString();
label.setText( revertedText );
} );
timer.setRepeats( false );//make sure the timer only runs once
A complete runnable example which uses this Timer
is given below: the UI contains a button and a label. Pressing the button will reverse the text of the label after a 2 second delay
import javax.swing.*;
import java.awt.*;
public final class DelayedExecutionExample {
public static void main( String[] args ) {
EventQueue.invokeLater( () -> showUI() );
}
private static void showUI(){
JFrame frame = new JFrame( "Delayed execution example" );
JLabel label = new JLabel( "Hello world" );
JButton button = new JButton( "Reverse text with delay" );
button.addActionListener( event -> {
button.setEnabled( false );
//Instead of directly updating the label, we use a timer
//This allows to introduce a delay, while keeping the EDT free
int delay = 2000;
Timer timer = new Timer( delay, e -> {
String revertedText = new StringBuilder( label.getText() ).reverse().toString();
label.setText( revertedText );
button.setEnabled( true );
} );
timer.setRepeats( false );//make sure the timer only runs once
timer.start();
} );
frame.add( label, BorderLayout.CENTER );
frame.add( button, BorderLayout.SOUTH );
frame.pack();
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}
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 update the label at a fixed interval
int delay = 2000;
Timer timer = new Timer( delay, e -> {
String revertedText = new StringBuilder( label.getText() ).reverse().toString();
label.setText( revertedText );
} );
timer.start();
A complete runnable example which uses this Timer
is given below: the UI contains a label, and the text of the label will be reverted each two seconds.
import javax.swing.*;
import java.awt.*;
public final class RepeatTaskFixedIntervalExample {
public static void main( String[] args ) {
EventQueue.invokeLater( () -> showUI() );
}
private static void showUI(){
JFrame frame = new JFrame( "Repeated task example" );
JLabel label = new JLabel( "Hello world" );
//Use a timer to update the label at a fixed interval
int delay = 2000;
Timer timer = new Timer( delay, e -> {
String revertedText = new StringBuilder( label.getText() ).reverse().toString();
label.setText( revertedText );
} );
timer.start();
frame.add( label, BorderLayout.CENTER );
frame.pack();
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}
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() {
private int counter = 0;
@Override
public void actionPerformed( ActionEvent e ) {
counter++;//keep track of the number of times the Timer executed
label.setText( counter + "" );
if ( counter == 5 ){
( ( Timer ) e.getSource() ).stop();
}
}
});
A complete runnable example which uses this Timer
is given below: it shows a UI where the text of the label will count from zero to five. Once five is reached, the Timer
is stopped.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public final class RepeatFixedNumberOfTimes {
public static void main( String[] args ) {
EventQueue.invokeLater( () -> showUI() );
}
private static void showUI(){
JFrame frame = new JFrame( "Repeated fixed number of times example" );
JLabel label = new JLabel( "0" );
int delay = 2000;
Timer timer = new Timer( delay, new ActionListener() {
private int counter = 0;
@Override
public void actionPerformed( ActionEvent e ) {
counter++;//keep track of the number of times the Timer executed
label.setText( counter + "" );
if ( counter == 5 ){
//stop the Timer when we reach 5
( ( Timer ) e.getSource() ).stop();
}
}
});
timer.setInitialDelay( delay );
timer.start();
frame.add( label, BorderLayout.CENTER );
frame.pack();
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}
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 makes sure that happens.
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
//JFrames will not display without size being set
frame.setSize(500, 500);
JLabel label = new JLabel("Hello World");
frame.add(label);
frame.setVisible(true);
});
}
}
As you may notice if you run this code, the label is position in a very bad place. This is difficult to change in a good manner using the add
method. To allow more dynamic and flexible placing check out Swing Layout Managers.
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);
//See link below for more info on FlowLayout
this.setLayout(new FlowLayout());
JLabel label = new JLabel(labelText);
add(label);
//Tells the JFrame what to do when it's closed
//In this case, we're saying to "Dispose" on remove all resources
//associated with the frame on close
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void addLabel(String labelText) {
JLabel label = new JLabel(labelText);
add(label);
this.validate();
}
public static void main(String args[]) {
//All Swing actions should be run on the Event Dispatch Thread (EDT)
//Calling SwingUtilities.invokeLater makes sure that happens.
SwingUtilities.invokeLater(() -> {
CustomFrame frame = new CustomFrame("Hello Jungle");
//This is simply being done so it can be accessed later
statFrame = frame;
frame.setVisible(true);
});
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
//Handle error
}
SwingUtilities.invokeLater(() -> statFrame.addLabel("Oh, hello world too."));
}
}
For more information on FlowLayout here.
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, 500);
//See link below for more info on FlowLayout
this.setLayout(new FlowLayout());
//Tells the JFrame what to do when it's closed
//In this case, we're saying to "Dispose" on remove all resources
//associated with the frame on close
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Add a button
JButton btn = new JButton("Hello button");
//And a textbox
JTextField field = new JTextField("Name");
field.setSize(150, 50);
//This next block of code executes whenever the button is clicked.
btn.addActionListener((evt) -> {
JLabel helloLbl = new JLabel("Hello " + field.getText());
add(helloLbl);
validate();
});
add(btn);
add(field);
}
public static void main(String args[]) {
//All Swing actions should be run on the Event Dispatch Thread (EDT)
//Calling SwingUtilities.invokeLater makes sure that happens.
SwingUtilities.invokeLater(() -> {
CustomFrame frame = new CustomFrame("Hello Jungle");
//This is simply being done so it can be accessed later
frame.setVisible(true);
});
}
}
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..."), BorderLayout.CENTER);
loading.setUndecorated(true);
loading.getContentPane().add(p1);
loading.pack();
loading.setLocationRelativeTo(parentComponent);
loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
loading.setModal(true);
SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws InterruptedException
/** Execute some operation */
}
@Override
protected void done() {
loading.dispose();
}
};
worker.execute(); //here the process thread initiates
loading.setVisible(true);
try {
worker.get(); //here the parent thread waits for completion
} catch (Exception e1) {
e1.printStackTrace();
}
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 Jbutton...
public static void main(String[] args) {
JFrame frame = new JFrame(); //creates the frame
frame.setSize(300, 300);
frame.setVisible(true);
//////////////////////////ADDING BUTTON BELOW//////////////////////////////
JButton B = new JButton("Say Hello World");
B.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent arg0) {
System.out.println("Hello World");
}
});
B.setBounds(0, 0,frame.getHeight(), frame.getWidth());
B.setVisible(true);
frame.add(B);
////////////////////////////////////////////////////////////////////////////
}
By Executing/Compiling this code you should get something like this...
When the button is clicked... "Hello World" should also appear in your console.