Re: How to add time delay in JFrame ?
On 10/20/2013 07:11, Khoa Bach Tran wrote:
First of all, sorry for my bad english.
Now I have to demo how the sort code work on Java JFrame but the problem is I dont know how to make the delay between the switch.
How can I add the delay when mark the column with color ? Already searched on many threads by googles but i'm too newbie to know how the code work with Swing timer.
public void MarkColumn(JLabel column) {
column.setBackground(new Color(255, 153, 0));
//Delay 1.5 seconds;
}
public void UnmarkColumn(JLabel column) {
column.setBackground(new Color(51, 153, 255));
//Delay 1.5 seconds;
}
void sort()
{
for (int i = 0; i < list.size() - 1; i++) {
MarkColumn(columns.get(i));
for (int j = list.size() - 1; j > i; j--) {
MarkColumn(columns.get(j));
if (list.get(j).getPoint() < list.get(j - 1).getPoint()) {
SinhVien tg = list.get(j - 1);
list.set(j - 1, list.get(j));
list.set(j, tg);
}
UnmarkColumn(columns.get(j));
}
UnmarkColumn(columns.get(i));
}
}
Very appreciated for your helps.
With very few exceptions, you must modify Swing components from the
Event Dispatch Thread (EDT). You cannot block the EDT or your GUI will
not update either. And by block I specifically mean calling
Thread.sleep() on the EDT.
So in your case I would put the sorting code in one thread and then wrap
the GUI updates into the EDT. That way you can control the speed of the
sort and still see the updates as they happen.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class test extends JPanel {
final static int N = 10;
final static Color BG = new Color(170,170,255);
int[] nums = new int[N];
JLabel[] labels = new JLabel[N];
JButton start = new JButton("start");
Random rand = new Random(System.currentTimeMillis());
public test() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
for (int i=0; i<N; i++) {
synchronized(nums) {
nums[i] = rand.nextInt(N);
labels[i] = new JLabel(Integer.toString(nums[i]));
}
labels[i].setOpaque(true);
c.gridy = i;
add(labels[i],c);
}
++c.gridy;
add(start,c);
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// disable the start button
start.setEnabled(false);
// runnable for the sort code
Runnable r = new Runnable() {
public void run() {
// bubble sort
for (int i=0; i<N-1; i++) {
for (int j=0; j<N-1-i; j++) {
// highlight the two cells being compared
highlight(j,true);
try {
Thread.sleep(333);
} catch (InterruptedException ie) { }
synchronized (nums) {
if (nums[j] > nums[j+1]) {
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
update(j);
}
}
try {
Thread.sleep(333);
} catch (InterruptedException ie) { }
// remove the highlight
highlight(j,false);
}
}
// re-enable start button
EventQueue.invokeLater(new Runnable() {
public void run() {
start.setEnabled(true);
}
});
}
};
new Thread(r).start();
}
});
}
public void highlight(final int n, final boolean state) {
EventQueue.invokeLater(new Runnable() {
public void run() {
labels[n].setBackground(state ? BG : Color.WHITE);
labels[n+1].setBackground(state ? BG : Color.WHITE);
}
});
}
public void update(final int n) {
EventQueue.invokeLater(new Runnable() {
public void run() {
synchronized (nums) {
labels[n].setText(Integer.toString(nums[n]));
labels[n+1].setText(Integer.toString(nums[n+1]));
}
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new test(),BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}