Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

Problem with focus

Thread view: 
John Stivenson - 09 Jan 2005 15:39 GMT
I have two JButtons b1 and b2 on JPanel. Initially, b1 is enabled and b2
is disabled.
When the user clicks on b1 the following code is executed:

 b1.setEnabled(false);
 b2.setEnabled(true);
 b2.requestFocusInWindow();

But b2 don't receive the focus. It seems that b1 is still focus owner.
Neighter of them now receives keyboard input. Maybe parent JPanel lost focus?

Where is the problem?
Andrey Kuznetsov - 09 Jan 2005 18:58 GMT
>  b1.setEnabled(false);
>  b2.setEnabled(true);
>  b2.requestFocusInWindow();

I don't know JButton.requestFocusInWindow();

> Where is the problem?
may be in your code

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Andrew Thompson - 09 Jan 2005 20:00 GMT
>> Where is the problem?
> may be in your code

Speaking of which, here are some handy tips in case you cannot
resolve the problem and need to post code.
<http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

John McGrath - 11 Jan 2005 02:50 GMT
> I don't know JButton.requestFocusInWindow();

The requestFocusInWindow() method is defined in the Component class.
It was part of the focus model changes in JDK 1.4.

Signature

Regards,

John McGrath

Andrey Kuznetsov - 11 Jan 2005 02:58 GMT
>> I don't know JButton.requestFocusInWindow();
>
> The requestFocusInWindow() method is defined in the Component class.
> It was part of the focus model changes in JDK 1.4.

this explains why i didn't found it in javadoc 1.3

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

John McGrath - 11 Jan 2005 02:50 GMT
> I have two JButtons b1 and b2 on JPanel. Initially, b1 is enabled and b2
> is disabled.  When the user clicks on b1 the following code is executed:
[quoted text clipped - 6 lines]
> Neighter of them now receives keyboard input. Maybe parent JPanel lost
> focus?

That looks like it should work.  Can you post a runnable example of the
problem?

Signature

Regards,

John McGrath

Andrey Kuznetsov - 11 Jan 2005 03:05 GMT
>>   b1.setEnabled(false);
>>   b2.setEnabled(true);
>>   b2.requestFocusInWindow();

in most cases it helps to pack it into Runnable and run with
SwingUtilities.invokeLater();
if it works with invokeLater then you can be sure that your sequence worked,
however _after_ it was ready something else changed focus back
so you couldn't see the result - our eyes are too slow;-)

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

John McGrath - 11 Jan 2005 03:48 GMT
> >>   b1.setEnabled(false);
> >>   b2.setEnabled(true);
> >>   b2.requestFocusInWindow();
>
> in most cases it helps to pack it into Runnable and run with
> SwingUtilities.invokeLater();

That was often required when using requestFocus() prior to JDK 1.4,
but I do not think it has been necessary since the 1.4 focus rewrite.

Signature

Regards,

John McGrath

Andrey Kuznetsov - 11 Jan 2005 09:31 GMT
>> >>   b1.setEnabled(false);
>> >>   b2.setEnabled(true);
[quoted text clipped - 5 lines]
> That was often required when using requestFocus() prior to JDK 1.4,
> but I do not think it has been necessary since the 1.4 focus rewrite.

I know at least one app where it was nesessery (yes it was JDK 1.4)

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

John McGrath - 11 Jan 2005 23:00 GMT
> > That was often required when using requestFocus() prior to JDK 1.4,
> > but I do not think it has been necessary since the 1.4 focus rewrite.
>
> I know at least one app where it was nesessery (yes it was JDK 1.4)

Can you explain the conditions under which it was necessary?  Or better
yet, provide a runnable example of this?

Signature

Regards,

John McGrath

Andrey Kuznetsov - 12 Jan 2005 10:22 GMT
>> > That was often required when using requestFocus() prior to JDK 1.4,
>> > but I do not think it has been necessary since the 1.4 focus rewrite.
[quoted text clipped - 3 lines]
> Can you explain the conditions under which it was necessary?  Or better
> yet, provide a runnable example of this?

you can download jgui source and search for SwingUtilities.invokeLater()
however it is used not for focus issues,
but for repacking components after undocking.

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

John McGrath - 12 Jan 2005 23:04 GMT
> you can download jgui source and search for SwingUtilities.invokeLater()
> however it is used not for focus issues, but for repacking components
> after undocking.

Oh, OK.  Yes, I have run into many cases where posting an action to the
event queue is necessary, including focus-shifting code before Java 1.4.
But since the 1.4 focus changes, I have not found it necessary for dealing
with focus.

Signature

Regards,

John McGrath



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.