Keyboard focus held (tenaciously) by a JDialog?

When I display your average Dialog Window (and it closes, returning a value) it seems to greedily keep the keyboard focus!

Object[] options ={"Yes","No"};

int nAnswer = JOptionPane.showOptionDialog(frame,

"Delete all selected files?",

"Confirm File Delete",

JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE,

null,//don't use a custom Icon

options,//the titles of buttons

options[1]);//default button

I say that after investigating the problem quite a bit.

I implemented a KeyEventDispatcher which directs ALL keyboard input to one component -- a JList.

It works fine, except that after I display (and close) ANY dialog box, the next keypress causes an error:

"Exception occurred during event dispatching:

java.lang.ClassCastException: javax.swing.JDialog cannot be cast to javax.swing.JList"

The JDialog certainly has something to do with the problem. In fact, the keypress IS directed to the component in question! So the code actually works -- yet it causes this error. Should I just "catch" the error and ignore it? That doesn't seem like a good idea.

The error occurs here in the Java (read-only) code BasicListUI.java:

publicvoid keyTyped(KeyEvent e){

>JList src = (JList)e.getSource();

ListModel model = src.getModel();

if (model.getSize() == 0 || e.isAltDown() || e.isControlDown() || e.isMetaDown() ||

isNavigationKey(e)){

// Nothing to select

return;

}

Any ideas?

Thanks,

Matthew

[2098 byte] By [CathInfoa] at [2007-11-27 5:31:15]
# 1
your problem is here:JList src = (JList)e.getSource();You are assuming that the source is a JList, and that is not an assumption you should be making.
bsampieria at 2007-7-12 14:56:16 > top of Java-index,Desktop,Core GUI APIs...
# 2
error correctedMessage was edited by: petes1234
petes1234a at 2007-7-12 14:56:16 > top of Java-index,Desktop,Core GUI APIs...
# 3

I think you misunderstand --

The error is happening in the Java code written by Sun -- not my own code.

I was just demonstrating the nature of the problem (namely that a JDialog has keyboard focus AFTER it disappears from the screen - which is causing this code to try to cast a JDialog to a JList.

Matthew

CathInfoa at 2007-7-12 14:56:16 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I think you misunderstand --

>

> The error is happening in the Java code written by

> Sun -- not my own code.

This is similar to your post here: [url=http://forum.java.sun.com/thread.jspa?threadID=5176269&messageID=9681394#9681394]Java/Netbeans BUG? -- Conditional declaration of array[/url]

For information on both of these posts, I recommend you check out [url=http://en.wikipedia.org/wiki/Hubris]this site[/url].

Good luck and happy coding

Message was edited by:

petes1234

petes1234a at 2007-7-12 14:56:16 > top of Java-index,Desktop,Core GUI APIs...
# 5

While we're posting links...

http://www.worth1000.com/entries/228000/228047VbDA_w.jpg

(I knew I'd regret that post later...I should have apologized)

I admit I was wrong about the "Java BUG" post -- to make a long story short, I was frustrated and not thinking entirely clearly. As opposed to most people who have completely mastered their emotions...

Anyhow, now that I've deflected that personal attack, let's get back to the question at hand.

To clarify, I am not accusing Sun or Java of having a bug in their code. The tone of this post is completely different from my last post (re: Java "bug").

What I'm saying is: "For some reason, a JDialog SEEMS to be in possession of the focus after it has disappeared from the screen, because in Sun's Java code, it gives me an error which points to that fact."

The error has to do with casting a JDialog to a JList -- and shows me the place in Sun's code where it's crashing.

Of course I am led to believe it has to do with the JDialog that I just closed in my program -- because the problem never happens outside of that.

I hope this clarifies things a bit.

Matthew

null

CathInfoa at 2007-7-12 14:56:16 > top of Java-index,Desktop,Core GUI APIs...
# 6

Your best bet for a solution here is to create a SSCE or short self-contained correct (compilable) example so that folks here can play with your code. For details on what this means, please look at [url=http://homepage1.nifty.com/algafield/sscce.html]this site[/url]. In fact, sometimes distilling your code down to the bare minimum that will reproduce the problem makes seeing the problem obvious to you, and you won't even need any advice from here.

Good luck.

petes1234a at 2007-7-12 14:56:17 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I think you misunderstand --

>

No you misunderstand.

> The error is happening in the Java code written by

> Sun -- not my own code.

>

That is a very narrow and incorrect interpretation.

The error is because you are asking code written by Sun to do something incorrectly. The stacktrace may start with Sun's code but the error begins with yours.

I think you should give up programming you do not seem to have the aptitude for it. That's not a personal attack, that's just a fact. You have learned nothing from the schmozzle of your previous thread.

cotton.ma at 2007-7-12 14:56:17 > top of Java-index,Desktop,Core GUI APIs...