ensure new messages are added from the awt thread

This commit is contained in:
fros4943 2008-10-02 21:19:47 +00:00
parent a965293488
commit c5181a4a8e

View file

@ -26,14 +26,14 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: MessageList.java,v 1.5 2008/09/20 08:05:29 fros4943 Exp $ * $Id: MessageList.java,v 1.6 2008/10/02 21:19:47 fros4943 Exp $
* *
* ----------------------------------------------------------------- * -----------------------------------------------------------------
* *
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne, Fredrik Osterlind * Author : Adam Dunkels, Joakim Eriksson, Niclas Finne, Fredrik Osterlind
* Created : 2006-06-14 * Created : 2006-06-14
* Updated : $Date: 2008/09/20 08:05:29 $ * Updated : $Date: 2008/10/02 21:19:47 $
* $Revision: 1.5 $ * $Revision: 1.6 $
*/ */
package se.sics.cooja.dialogs; package se.sics.cooja.dialogs;
import java.awt.Color; import java.awt.Color;
@ -50,6 +50,7 @@ import javax.swing.DefaultListModel;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.ListModel; import javax.swing.ListModel;
import javax.swing.ListSelectionModel; import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
public class MessageList extends JList { public class MessageList extends JList {
@ -126,13 +127,17 @@ public class MessageList extends JList {
addMessage(message, NORMAL); addMessage(message, NORMAL);
} }
public void addMessage(String message, int type) { public void addMessage(final String message, final int type) {
boolean scroll = getLastVisibleIndex() >= getModel().getSize() - 1; SwingUtilities.invokeLater(new Runnable() {
MessageContainer msg = new MessageContainer(message, type); public void run() {
((DefaultListModel) getModel()).addElement(msg); boolean scroll = getLastVisibleIndex() >= getModel().getSize() - 2;
if (scroll) { MessageContainer msg = new MessageContainer(message, type);
ensureIndexIsVisible(getModel().getSize() - 1); ((DefaultListModel) getModel()).addElement(msg);
} if (scroll) {
ensureIndexIsVisible(getModel().getSize() - 1);
}
}
});
} }
public void clearMessages() { public void clearMessages() {