"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!
You should update your list in a separate thread otherwise you end up blocking the event dispatch thread.
Up vote 4 down vote favorite share g+ share fb share tw.
I have a function that executes when a button is clicked. Suppose there is a loop to add 1 to 10 to a JList. I add that data to DefaultListModel.
It works perfectly and the numbers get added. Then I added a Thread. Sleep(1000) within the loop.
But the output is different. I wanted to add 1 element every second. But now it waits for 10secs and the add all 1 to 10 together at the end of 10th second.
Am I wrong anywhere? List processList = listNumbers. GetSelectedValuesList(); DefaultListModel resultList = new DefaultListModel(); listResult.
SetModel(resultList); for (int I = 0; I Sleep(1000); } catch (InterruptedException ex) { } } java swing jlist link|improve this question edited Sep 16 '11 at 9:06 asked Sep 16 '11 at 8:56Akhil K Nambiar360113 65% accept rate.
You should update your list in a separate thread otherwise you end up blocking the event dispatch thread. Try the following: final DefaultListModel model = new DefaultListModel(); final JList list = new JList(model); //another thread to update the model final Thread updater = new Thread() { /* (non-Javadoc) * @see java.lang. Thread#run() */ @Override public void run() { for (int I = 0; I AddElement(i); try { Thread.
Sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; updater.start().
I still think that model. AddElement(i) must be wrapped into invokeLater, please update your post – mKorbel Sep 16 '11 at 9:20 @mKorbel really? I am updating the model, which will in turn fire an event to a handler which will call repaint on the list.
I believe this repaint call will queue a request for the event-dispatching thread to call paint, so it should be safe. – dogbane Sep 16 '11 at 9:34 2 true is that there are no updates on EDT, all are done past Thread. Sleep(1000);, because all fireXxxXxx are out of EDT – mKorbel Sep 16 '11 at 9:41 I'll edit my post with examples for programaticaly block EDT with Thread.
Sleep(int) for correct and expected output to the GUI – mKorbel Sep 16 '11 at 9:48 @mKorbel in my code, the model is updated out of the EDT, but the repaint calls which are performed by the model listeners are performed on the EDT. If you run my code, you will see that it produces the right output. – dogbane Sep 16 '11 at 9:51.
Don't use, really don't use Thread. Sleep(int) during EventDispashThread, because sleep locked current Thread and in this case EventDispashThread, with un-expection output to the GUI, more in Concurency in Swing, if you needed something to dealay, then add Items wrappend into Runneble#Thread, with output to the GUI wrapped into invokeLater, or the best way is using javax.swing. Timer EDIT 1st.
Example how to programatically block EDT with Thread. Sleep(int), because othewise doesn't works with expecting output to the GUI (code is very loonnnger as I want to code) import java.awt. *; import java.awt.event.
*; import javax.swing. *; public class ShakingButtonDemo implements Runnable { private JButton button; private JRadioButton radioWholeButton; private JRadioButton radioTextOnly; public static void main(String args) throws Exception { SwingUtilities. InvokeLater(new ShakingButtonDemo()); } @Override public void run() { radioWholeButton = new JRadioButton("The whole button"); radioTextOnly = new JRadioButton("Button text only"); radioWholeButton.
SetSelected(true); ButtonGroup bg = new ButtonGroup(); bg. Add(radioWholeButton); bg. Add(radioTextOnly); button = new JButton(" Shake with this Button "); button.
AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { shakeButton(radioWholeButton.isSelected()); } }); JPanel p1 = new JPanel(); p1. SetBorder(BorderFactory. CreateTitledBorder("Shake Options")); p1.
SetLayout(new GridLayout(0, 1)); p1. Add(radioWholeButton); p1. Add(radioTextOnly); JPanel p2 = new JPanel(); p2.
SetLayout(new GridLayout(0, 1)); p2. Add(button); JFrame frame = new JFrame(); frame. SetTitle("Shaking Button Demo"); frame.
SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame. Add(p1, BorderLayout.
NORTH); frame. Add(p2, BorderLayout. SOUTH); frame.
SetSize(240, 160); frame. SetLocationRelativeTo(null); frame. SetVisible(true); } private void shakeButton(final boolean shakeWholeButton) { final Point point = button.getLocation(); final Insets margin = button.getMargin(); final int delay = 75; Runnable r = new Runnable() { @Override public void run() { for (int I = 0; I 5) { new Thread(new Runnable() { @Override public void run() { try { Thread.
Sleep(750); changeBtnForegroung(Color. DarkGray); Thread. Sleep(750); count = 0; Thread.
Sleep(750); } catch (Exception e) { System.out. Println(e); } } }).start(); } else { new Thread(new Runnable() { @Override public void run() { try { runs = true; if (noColor items = new Vector(); if (chkA.isSelected()) { items. Add("A"); } if (chkB.isSelected()) { items.
Add("B"); } if (chkC.isSelected()) { items. Add("C"); } cboItems. SetModel(new DefaultComboBoxModel(items)); try { new Thread().
Sleep(2500); // simulate a long transaction } catch (InterruptedException ex) { } setCursor(Cursor. GetPredefinedCursor(Cursor. DEFAULT_CURSOR)); } public static void main(String args) { JFrame f = new DelayedComboBoxTest(); f.
SetVisible(true); } private class MyPopupMenuListener implements PopupMenuListener { @Override public void popupMenuCanceled(PopupMenuEvent e) { } @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { } @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { int items = cboItems.getItemCount(); rebuildList(); if (items! = cboItems.getItemCount()) { cboItems.hidePopup(); cboItems.showPopup(); } } } } but for all examples there must EDT exist and with Events in the EventQueue otherwise simple doesn't works.
2 for Great tips and -1 for some typos (just kidding). – asgs Sep 16 '11 at 9:46 hmmm yes I have problem with speaking languages English and Deutch too :-) , – mKorbel Sep 16 '11 at 9:58.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.