Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.gui Subject: Re: How to add time delay in JFrame ? Date: Sun, 20 Oct 2013 13:44:51 -0700 Organization: A noiseless patient Spider Lines: 158 Message-ID: References: <8f1ddeb0-aebc-435d-a8d9-f0fbe69d4e5f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 20 Oct 2013 20:44:45 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="0d7bb36aefa9cd15fbd945d36d7130e8"; logging-data="31284"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oE84RETlfpjPNIrkMTptF" User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0 In-Reply-To: <8f1ddeb0-aebc-435d-a8d9-f0fbe69d4e5f@googlegroups.com> Cancel-Lock: sha1:MgLXqnk2KtcleunJLKR01FDq8Rs= Xref: csiph.com comp.lang.java.gui:5384 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 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); } }); } }