Skip to main content

focus problems

5 replies [Last post]
azurianarcher
Offline
Joined: 2006-02-17

heres my code:

<br />
protected void processFocusEvent(FocusEvent e)<br />
{<br />
   FocusListener arry[] = getFocusListeners();<br />
   FocusEvent fe = new FocusEvent(parent,FocusEvent.FOCUS_GAINED,true,e.getOppositeComponent());</p>
<p>   if(isFocusOwner())<br />
   {<br />
      for(int x = 0; x < arry.length; x++)<br />
      {<br />
         arry[x].focusGained(fe);<br />
      }<br />
   }else{<br />
      for(int x = 0; x < arry.length; x++)<br />
      {<br />
         arry[x].focusGained(fe);<br />
      }<br />
   }<br />
}<br />

when i run teh program and focus the componant i get this:

<br />
java.lang.ClassCastException<br />
        at com.sun.java.swing.plaf.gtk.SynthButtonListener.focusGained(SynthButt<br />
onListener.java:160)<br />
        at Cabinet$innerButton.processFocusEvent(FileCabinet.java:819)<br />
        at java.awt.Component.processEvent(Component.java:4888)<br />
        at java.awt.Container.processEvent(Container.java:1569)<br />
        at java.awt.Component.dispatchEventImpl(Component.java:3615)<br />
        at java.awt.Container.dispatchEventImpl(Container.java:1627)<br />
        at java.awt.Component.dispatchEvent(Component.java:3477)<br />
        at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.ja<br />
va:1713)<br />
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeybo<br />
ardFocusManager.java:768)<br />
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFoc<br />
usManager.java:453)<br />
        at java.awt.Component.dispatchEventImpl(Component.java:3506)<br />
        at java.awt.Container.dispatchEventImpl(Container.java:1627)<br />
        at java.awt.Component.dispatchEvent(Component.java:3477)<br />
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)<br />
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh<br />
read.java:201)<br />
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre<br />
ad.java:151)<br />
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)</p>
<p>        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)</p>
<p>        at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)<br />
java.lang.ClassCastException<br />
        at com.sun.java.swing.plaf.gtk.SynthButtonListener.focusGained(SynthButt<br />
onListener.java:160)<br />
        at Cabinet$innerButton.processFocusEvent(FileCabinet.java:824)<br />
        at java.awt.Component.processEvent(Component.java:4888)<br />
        at java.awt.Container.processEvent(Container.java:1569)<br />
        at java.awt.Component.dispatchEventImpl(Component.java:3615)<br />
        at java.awt.Container.dispatchEventImpl(Container.java:1627)<br />
        at java.awt.Component.dispatchEvent(Component.java:3477)<br />
        at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.ja<br />
va:1713)<br />
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeybo<br />
ardFocusManager.java:777)<br />
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFoc<br />
usManager.java:497)<br />
        at java.awt.Component.dispatchEventImpl(Component.java:3506)<br />
        at java.awt.Container.dispatchEventImpl(Container.java:1627)<br />
        at java.awt.Component.dispatchEvent(Component.java:3477)<br />
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)<br />
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh<br />
read.java:201)<br />
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre<br />
ad.java:151)<br />
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)</p>
<p>        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)</p>
<p>        at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)<br />

can anyone help me? why does this happen?

thanks
~Amx

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
shan-man
Offline
Joined: 2006-02-17

Hi Amx,

Amx wrote:
> heres my code:
>
> [code]
> protected void processFocusEvent(FocusEvent e)
> {
> FocusListener arry[] = getFocusListeners();
> FocusEvent fe = new
> new
> FocusEvent(parent,FocusEvent.FOCUS_GAINED,true,e.getOp
> positeComponent());
[/code]The question is: what is "parent" here? From the stack trace, I can tell that one of the listener's you're notifying is a listener on a button. This listener assumes that the source of the FocusEvent is the button that has gained focus. In general, this should be the case. You might want to take another look at what you're doing here and make sure that you're notifying the right listeners about focus events on the right components.

Thanks!
Shannon
[code]
> if(isFocusOwner())
> {
> for(int x = 0; x < arry.length; x++)
> {
> arry[x].focusGained(fe);
> }
> }else{
> for(int x = 0; x < arry.length; x++)
> {
> arry[x].focusGained(fe);
> }
> }
> }
> [/code]> when i run teh program and focus the componant i get
> this:
> [code]
> java.lang.ClassCastException
> at
> com.sun.java.swing.plaf.gtk.SynthButtonListener.focusG
> ained(SynthButt
> onListener.java:160)
> [/code]

azurianarcher
Offline
Joined: 2006-02-17

parent is a JPanel, which is what was causing the error, i foud a way to call the gainedFocus(FocusEvent) method explicitaly though

~Amx

zander
Offline
Joined: 2003-06-13

The stacktrace says the 'parent' you pass as the source object for the first argument in FocusEvent is wrong.

Unpack sources zipfile from your JSDK to find out what the first line of your stacktrace really expects.

Anonymous

I don't really know anything about focus events but why does your if() have the exact same code for both the if and the else? Could this be causing your problem?

-Millie

azurianarcher
Offline
Joined: 2006-02-17

millie, thanks for catching that for me, just a typo i as on autopilot

~Amx

Message was edited by: azurianarcher