So, basic concept, you want to generate some animation, where by the UI is updated over a period of time. Not an uncommon request, with plenty of examples.
Things to keep in mind:
- Swing is single threaded
- Don't do anything which might block the Event Dispatching Thread, this will prevent the UI from been update until such time that control is passed back to it
- Don't update the UI from outside the context of the EDT
Okay, that sounds a little contradictive, but Swing provides a couple of tools to make your life easier
You could...
Use a SwingWorker
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public interface StepContainer {
public void addStep(int index);
}
public class TestPane extends JPanel implements StepContainer {
private int step = 0;
public TestPane() {
setLayout(new GridBagLayout());
PopulateWorker worker = new PopulateWorker(10, this);
worker.execute();
}
@Override
public void addStep(int step) {
JButton btn = new JButton(Integer.toString(step));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
revalidate();
repaint();
}
}
public class PopulateWorker extends SwingWorker<Void, Integer> {
private int steps;
private StepContainer parent;
public PopulateWorker(int steps, StepContainer parent) {
this.steps = steps;
this.parent = parent;
}
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(1000);
for (int index = 0; index < steps; index++) {
publish(index);
Thread.sleep(1000);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
for (int step : chunks) {
parent.addStep(step);
}
}
}
}
Use a Swing Timer
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public interface StepContainer {
public void addStep(int index);
}
public class TestPane extends JPanel implements StepContainer {
private int step = 0;
public TestPane() {
setLayout(new GridBagLayout());
Timer timer = new Timer(1000, new PopulateAction(10, this));
timer.start();
}
@Override
public void addStep(int step) {
JButton btn = new JButton(Integer.toString(step));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
revalidate();
repaint();
}
}
public class PopulateAction implements ActionListener {
private int steps;
private StepContainer parent;
private int step;
public PopulateAction(int steps, StepContainer parent) {
this.steps = steps;
this.parent = parent;
}
@Override
public void actionPerformed(ActionEvent e) {
parent.addStep(step);
if (step == steps - 1) {
((Timer) e.getSource()).stop();
step = 0;
}
step++;
}
}
}
Which you use will depend on the complexity of your problem, SwingWorker
uses it's own Thread
to call doInBackground
, allowing you to process time consuming tasks, but provides the ability to update the UI safely.
Timer
triggers it's updates within the context of the EDT, making it save to update the UI from, but not to perform time consuming tasks in
SwingWorker
or a SwingTimer
to inject some kind of delay between updates