Skip to main content

focus subsystem on cvm

5 replies [Last post]
wrappingduke
Offline
Joined: 2008-01-23

hello,

attempting to set focus for components. i'm currently running requestFocus in a thread but to no avail. for example,

class FocusRequester implements Runnable
{
private Component comp;
public FocusRequester(Component comp)
{
this.comp = comp;
try
{
java.awt.EventQueue.invokeLater(this);
} catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
comp.requestFocus();
}
}

I have also tried requestFocusInWindow() in lieu of requestFocus().

curious to how can i get a component to gain focus programmatically?

any help is appreciated.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> hello,
>
> attempting to set focus for components. i'm currently running requestFocus in a thread but to no avail. for example,
>
> ...
>
> I have also tried requestFocusInWindow() in lieu of requestFocus().
>
> curious to how can i get a component to gain focus programmatically?
>
> any help is appreciated.
>

Hi wrappingduke,

Usually this type of problem needs some low-level debugging first, since
you are using multiple threads.

First you might want to read over more info on the AWT Focus Subsystem:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/doc-files/FocusSpec.htm...

After you read the above info, you will want to do some basic low-level
troubleshooting by adding a FocusListener to your component in the main
thread (NOTE: important that you do this in the main app thread and NOT
in your FocusRequester thread).

In the FocusListener, do some System.out.printlin() statements to debug
whether you are receiving any Focus events. If you are, then it is
working properly. If you are not, you should see if your main app
thread is properly still running in parallel to your FocusRequester
thread when you execute your program (in order that it keeps processing
AWT events), or if somehow the main app thread has suspended or
otherwise is not processing your AWT events anymore.

Hinkmond

---------------------------------------------------------------------
To unsubscribe, e-mail: advanced-unsubscribe@phoneme.dev.java.net
For additional commands, e-mail: advanced-help@phoneme.dev.java.net

wrappingduke
Offline
Joined: 2008-01-23

Hello Hinkmond,

thx. for the response. It's appreciated. FocusListeners are implemented w/ the desired components. Here's the scenario. Just using some basic input validation where in the lost_focus event the FocusRequester is invoked. For example, when user attempts to leave a textbox, validate entry. If entry is invalid, then attempt to gain focus of the component via FocusRequester. The focusrequester appears to be running parallel because I also highlight the text in the textbox through the focusrequester. the text is highlighted in the textbox but the focus is not set.

any help is appreciated

Message was edited by: wrappingduke

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> Hello Hinkmond,
>
> thx. for the response. It's appreciated. FocusListeners are implemented w/ the desired components. Here's the scenario. Just using some basic input validation where in the lost_focus event the FocusRequester is invoked. For example, when user attempts to leave a textbox, validate entry. If entry is invalid, then attempt to gain focus of the component via FocusRequester. The focusrequester appears to be running parallel because I also highlight the text in the textbox through the focusrequester.
>

Hi wrappingduke,

You'll have to continue debugging further, since the info you give above
is not enough to attempt to diagnose the problem.

Try adding a more general ComponentListener to your TextBox in your main
app thread and have it print every AWT Component event that it receives
(not just the FocusEvent). This way, you can make sure that all AWT
events are being processed correctly during your troubleshooting.

Then, you will be on your own to debug further, since it is too
difficult to debug from the limited info you have given so far. This
type of problem doesn't have a straightforward answer, since you are
running multithreaded.

Hinkmond

---------------------------------------------------------------------
To unsubscribe, e-mail: advanced-unsubscribe@phoneme.dev.java.net
For additional commands, e-mail: advanced-help@phoneme.dev.java.net

wrappingduke
Offline
Joined: 2008-01-23

hi Hinkmond,

thx for your helpful suggestions. The thread is being used as an alternative since neither requestFocus or requestFocusInWindow worked in the main thread or app since it didn't work in either a j2se nor j2me enviro regardless of the jdk level. However, I have tried setting focus both via a single-thread and now as a multi-thread for j2me.

i'm not sure what other info. i can provide that can help you help me. Pls. let me know.
the only thing i didn't mention is that a messagebox is shown prior to attempting to gain focus for the component. The messagebox also runs another thread to display an awt dialog w/ a button and image. Here's sample code of the lost_focus event

if( txtBox.getLength > 2 )
{
MsgBox.useImageCanvas("Critical.gif");
MsgBox.message("Field length can not greater than 2. Please try entry again.");
MsgBox.show();
// requestFocusInWindow(); // doesn't work here
new FocusRequester(txtBox);
}

the end result is the messagebox is shown and then disposed when the user clicks on ok button and the text is highlighted.

Message was edited by: wrappingduke

Hinkmond Wong

> {
> MsgBox.message("Field length can not greater than 2. Please try entry again.");
> MsgBox.show();
>
^^^^^^^^^^^^^^^^ Here's one problem: the method call MsgBox.show() does
not guarantee that the MsgBox is actually being shown yet at the point,
because you are in a different thread than the main app thread. You
need to wait until the main app thread processes the show event before
proceeding any further to try to request focus.

> // requestFocusInWindow(); // doesn't work here
> new FocusRequester(txtBox);
>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Your requestFocus() will not work here if
you have not allowed your main app thread to allow the AWT event loop to
process the show event of the MsgBox yet. You might want to move this
line out of this method to occur somewhere else in your app, like in
your main app thread instead, so that you allow time for the
MsgBox.show() to properly happen in the main app thread.
> }
>

Hinkmond

---------------------------------------------------------------------
To unsubscribe, e-mail: advanced-unsubscribe@phoneme.dev.java.net
For additional commands, e-mail: advanced-help@phoneme.dev.java.net