AWT -> Swing, blocked dispatch thread

  • Thread starter Miss Elaine Eos
  • Start date
M

Miss Elaine Eos

Ok, so I'm finally modernizing my code and switching everything from AWT
to Swing. I keep getting errors like the one below. What might I be
doing that would be blocking the event dispatch thread? Or maybe I'm
doing something else? Is there some common noob mistake that causes
this?

Thanks.


Event
java.awt.event.MouseEvent[MOUSE_PRESSED,(17,93),mods=16,clickCount=1] on
frame0 not handled after 30 seconds. Are you blocking the event
dispatch thread?
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1004)
at
com.apple.mrj.internal.awt.basepeers.EventHandlingComponentPeer.pumaWorka
roundPostEvent(EventHandlingComponentPeer.java:931)
at
com.apple.mrj.internal.awt.basepeers.EventHandlingComponentPeer.mousePres
sed(EventHandlingComponentPeer.java:680)
at
com.apple.mrj.internal.awt.frames.SimpleFramePeer.handleClick(SimpleFrame
Peer.java:1729)
at
com.apple.mrj.internal.awt.framehosts.CarbonWindowFrameHost.handleClick(C
arbonWindowFrameHost.java:1113)
at
com.apple.mrj.internal.awt.framehosts.CarbonWindowFrameHost.handleClickCo
ntentRgn(CarbonWindowFrameHost.java:1073)
at
com.apple.buckyball.app.WindowEventHandler.handleEvent(WindowEventHandler
..java:101)
at
com.apple.buckyball.app.EventHandler$Adapter.EventHandler(EventHandler.ja
va:248)
 
J

Jim Sculley

Miss said:
Ok, so I'm finally modernizing my code and switching everything from AWT
to Swing. I keep getting errors like the one below. What might I be
doing that would be blocking the event dispatch thread?

Calling Thread.sleep() in event handling code is one possibility.

A busy loop running on the EDT is another possibility:

while (someCondition) {
//do stuff that will never cause someCondition to be true
}

Doing any sort of time consuming work on the EDT is another possibility.

See SwingUtilities.invokeLater() for information on how to avoid such
problems.

Jim S.
 
M

Miss Elaine Eos

Calling Thread.sleep() in event handling code is one possibility.

Oh, is that bad? ;) Hmmm...
A busy loop running on the EDT is another possibility:

while (someCondition) {
//do stuff that will never cause someCondition to be true
}

Combined with Thread.sleep(), that's probably my problem.
Doing any sort of time consuming work on the EDT is another possibility.
See SwingUtilities.invokeLater() for information on how to avoid such
problems.

Yeah, I'm all over invokeLater(), but it seems I'm not quite doing it
right. Here's the object-call I want to make:

new OkCancelDialog (this, "Misc's OKCancelDialog", false).Go ("This
is the dialog text");

[or...]

if (new OkCancelDialog (this, "Misc's OKCancelDialog", true).Go ("How
about some fries with that?"))
{
AddFries();
}

and below the OkCancelDialog object. The question is: how to change it
to do what I want?

Thanks!

//
// OkCancelDialog.java
// SwingDialogTest

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class OkCancelDialog
{
private boolean ok = false;
private boolean cancelable = true;
private String title = null;
private boolean waitingOnSwing = false;
private Component parent = null;


public OkCancelDialog (Component par, String ttl, boolean doCancel)
{
this (par, ttl, doCancel, false);
}


public OkCancelDialog (Component par, String ttl, boolean doCancel,
boolean kill)
{
super();
parent = par;
title = ttl;
cancelable = doCancel;
//? Kill is depricated
}


public boolean Go (String msg)
{
waitingOnSwing = true;
final String theMsg = msg;
SwingUtilities.invokeLater (new Runnable() { public void run() {
DisplayOkCancelDialog (theMsg);
} });

while (waitingOnSwing)
{
try { Thread.sleep (222); }
catch (Exception ex) { }
}

return (ok);
}


private void DisplayOkCancelDialog (String msg)
{
if (cancelable)
{
ok = (JOptionPane.showConfirmDialog (parent,
msg,
title,
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION);
}
else
{
JOptionPane.showMessageDialog (parent, msg, title,
JOptionPane.PLAIN_MESSAGE);
ok = true;
}
waitingOnSwing = false;
}
}
 

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top