Skip to main content

Focus problem on phoneME

19 replies [Last post]
m4nti5
Offline
Joined: 2010-04-12
Points: 0

Hi, i'm a computer engineer developing a java aplication for a mobile device and i'm using phoneME personal profile b157 for windows Mobile 5. Everytime i try to give an java.awt.Textfield the focus using the function requestFocus() or using requestFocusInWindow() it doesn't give the focus to the textfield.Here is a code that doesn't work:

public class Main {
public static void main(String args[]){
Test t = new Test();
t.setVisible(true);
}
}

class Test extends java.awt.Frame{
private java.awt.TextField tf;
private java.awt.Button b;
private java.awt.Button f;
private java.awt.List l;
private java.awt.Panel p;

public Test(){

initComponents();

}
private void initComponents(){

this.setBounds(0, 0, 240, 300);
b = new java.awt.Button();
l = new java.awt.List();
tf = new java.awt.TextField();
f = new java.awt.Button();
p = new java.awt.Panel();

p.setBackground(new java.awt.Color(242, 242, 238));
p.setLayout(null);
p.setBounds(0, 0, 240, 320);

l.setBackground(new java.awt.Color(204, 204, 204));
l.setFont(new java.awt.Font("Courier New", 0, 12));
l.setBounds(0, 30, 170, 160);
p.add(l);
b.setBackground(new java.awt.Color(226, 226, 222));
b.setFont(new java.awt.Font("Arial", 0, 12));
b.setLabel("Close");
b.setBounds(170, 240, 70, 25);
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
});
p.add(b);
f.setBackground(new java.awt.Color(226, 226, 222));
f.setFont(new java.awt.Font("Arial", 0, 12));
f.setLabel("focus");
f.setBounds(170, 30, 70, 25);
f.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tf.requestFocus();
tf.requestFocusInWindow();
}
});
p.add(f);
tf.setFont(new java.awt.Font("Arial", 0, 12));
tf.setVisible(true);
tf.setBounds(60, 200, 110, 19);
p.add(tf);

add(p);
pack();
}

}

If you press focus the textfield doesn't receive focus. Am i doing something wrong with the focus?

Best regards,

M4nti5

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
m4nti5
Offline
Joined: 2010-04-12
Points: 0

Hi, thanks for the quick response, i tried the code and a console is shown that says:

"actionPerformed called with ActionEvent = java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=focus,when=0,modifiers=] on button0"

What does this mean? and how is related with the focus problem?

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> Hi, i'm a computer engineer developing a java aplication for a mobile device and i'm using phoneME personal profile b157 for windows Mobile 5. Everytime i try to give an java.awt.Textfield the focus using the function requestFocus() or using requestFocusInWindow() it doesn't give the focus to the textfield.Here is a code that doesn't work:
>

Hi M4anti5,

Did you try putting a debug line inside your actionPerformed(), such as
this:

f.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tf.requestFocus();
tf.requestFocusInWindow();
System.out.println("actionPerformed called with ActionEvent = "+evt);
}
});

Do you see anything strange when you put in the above debug line? (I
think the problem might be that you are trying to call requestFocus()
from the ActionListener() thread, but you need to call requestFocus()
from your main app thread instead)

Hinkmond

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

davyp
Offline
Joined: 2007-01-03
Points: 0

I tested the application on a regular Java SDK and focus works. On my Windows Mobile
Personal Profile builds, setting the focus indeed does not work.

requestFocus() and requestFocusInWindow() pretty much do the same, but the latter returns
some extra info:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#requestFocus()
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#requestFocusInWindow()

As you may have noticed, the requestFocusInWindow on Windows Mobile returns false, and
this means that the change focus request is guaranteed to fail. There are various reasons why
this may be the case. If you are interested, the Java implementation of setting the focus is handled in:

https://phoneme.dev.java.net/source/browse/phoneme/components/cdc/trunk/...

The method requestFocusHelper handles focus change requests, and there are various
method calls that may cause to change in focus to fail.

Davy

m4nti5
Offline
Joined: 2010-04-12
Points: 0

Hi, thanks for the quick response, i've seen the code and i've been trying to discard the reasons of the false, so, i tried the next code:

package Test;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Window;

public class Main {
public static void main(String args[]){
Test t = new Test();
t.setVisible(true);
}
}

class Test extends java.awt.Frame{
public java.awt.Window parent;
private java.awt.Frame fr;
private java.awt.TextField tf;
private java.awt.Button b;
private java.awt.Button f;
private java.awt.List l;
private java.awt.Panel p;

public Test(){

initComponents();

}
public void initComponents(){

this.setBounds(0, 0, 240, 300);
b = new java.awt.Button();
l = new java.awt.List();
tf = new java.awt.TextField();
f = new java.awt.Button();
p = new java.awt.Panel();

p.setBackground(new java.awt.Color(242, 242, 238));
p.setLayout(null);
p.setBounds(0, 0, 240, 320);

l.setBackground(new java.awt.Color(204, 204, 204));
l.setFont(new java.awt.Font("Courier New", 0, 12));
l.setBounds(0, 30, 170, 160);
p.add(l);
b.setBackground(new java.awt.Color(226, 226, 222));
b.setFont(new java.awt.Font("Arial", 0, 12));
b.setLabel("Close");
b.setBounds(170, 240, 70, 25);
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
});
p.add(b);
f.setBackground(new java.awt.Color(226, 226, 222));
f.setFont(new java.awt.Font("Arial", 0, 12));
f.setLabel("focus");
f.setBounds(170, 30, 70, 25);
f.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(""+tf.isFocusable()+" "+tf.isVisible());
tf.requestFocus();
tf.setText("focus: "+tf.requestFocusInWindow());
tf.selectAll();
}
});
p.add(f);
tf.setFont(new java.awt.Font("Arial", 0, 12));
tf.setVisible(true);
tf.setBounds(60, 200, 110, 19);
p.add(tf);

add(p);
pack();

parent = new Window(this);
parent.setFocusable(true);
}

}

I still have the same problem. The code above tested the verifications: if (isFocusable() && isVisible()) and if (window == null || !((Window)window).isFocusableWindow()) UNLESS i programed bad the window part the function requestFocusHelper is finding null in the other variables(peer and heavyweight), so, what can i do in my program to change this behavior?.

I also noticed that the tab button doesn't change focus either, thanks in advance,

M4nti5

Hinkmond Wong

Hi M4nti5,

phonemeadvanced@mobileandembedded.org wrote:
> ...
> f.setBounds(170, 30, 70, 25);
> f.addActionListener(new java.awt.event.ActionListener() {
> public void actionPerformed(java.awt.event.ActionEvent evt) {
> System.out.println(""+tf.isFocusable()+"
> "+tf.isVisible());
> tf.requestFocus();
> tf.setText("focus: "+tf.requestFocusInWindow());
> tf.selectAll();
> }
> });
> p.add(f);
> tf.setFont(new java.awt.Font("Arial", 0, 12));
> tf.setVisible(true);
> tf.setBounds(60, 200, 110, 19);
> p.add(tf);
> add(p);
> pack();
> parent = new Window(this);
> parent.setFocusable(true);
> }
>
> }
>
> I still have the same problem. The code above tested the
> verifications: if (isFocusable() && isVisible()) and if (window ==
> null || !((Window)window).isFocusableWindow()) UNLESS i programed bad
> the window part the function requestFocusHelper is finding null in the
> other variables(peer and heavyweight), so, what can i do in my program
> to change this behavior?.
>
> I also noticed that the tab button doesn't change focus either, thanks
> in advance,
>

It does look suspicious to me that you have your requestFocus() inside
the forked thread for the new ActionListener(), actionPerformed()
method. On Windows Mobile (not Linux or other platforms), you must make
UI calls only (such as calling requestFocus()) from the application's
main thread, not from inside a forked or created other thread (such
inside the new ActionListener() of your code).

So, without writing all the code for you, I would suggest that you just
set a flag inside your actionPerformed() and do not call requestFocus()
there. Instead, somewhere in your main app code (where you know you are
not in a separately created thread), check the flag (making sure it is
synchronized) and when it is set to true, call the requestFocus() on the
TextField.

Hinkmond

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

m4nti5
Offline
Joined: 2010-04-12
Points: 0

Hi Hinkmond, thanks again for your patience and quick response. I did what you proposed, and it's not working yet, here is the code:

public class Main {
public static void main(String args[])throws Exception{
Test t = new Test();
t.setVisible(true);
while(true){
Thread.sleep(100);
if(t.focusTF())
t.focusTextField();
}
}
}

class Test extends java.awt.Frame{
public java.awt.Window parent;
private java.awt.TextField tf;
private java.awt.Button b;
private java.awt.Button f;
private java.awt.List l;
private java.awt.Panel p;
private boolean focusText;

public Test(){
focusText = false;
initComponents();

}

public void focusTextField(){
tf.setText("focus: "+tf.requestFocusInWindow());
focusText = false;
}

public boolean focusTF(){
return focusText;
}

public void initComponents(){

this.setBounds(0, 0, 240, 300);
b = new java.awt.Button();
l = new java.awt.List();
tf = new java.awt.TextField();
f = new java.awt.Button();
p = new java.awt.Panel();

p.setBackground(new java.awt.Color(242, 242, 238));
p.setLayout(null);
p.setBounds(0, 0, 240, 320);

l.setBackground(new java.awt.Color(204, 204, 204));
l.setFont(new java.awt.Font("Courier New", 0, 12));
l.setBounds(0, 30, 170, 160);
p.add(l);
b.setBackground(new java.awt.Color(226, 226, 222));
b.setFont(new java.awt.Font("Arial", 0, 12));
b.setLabel("Close");
b.setBounds(170, 240, 70, 25);
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
});
p.add(b);
f.setBackground(new java.awt.Color(226, 226, 222));
f.setFont(new java.awt.Font("Arial", 0, 12));
f.setLabel("focus");
f.setBounds(170, 30, 70, 25);
f.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
focusText = true;
//tf.requestFocus();
//tf.setText("focus: "+tf.requestFocusInWindow());
//tf.selectAll();
}
});
p.add(f);
tf.setFont(new java.awt.Font("Arial", 0, 12));
tf.setVisible(true);
tf.setBounds(60, 200, 110, 19);
p.add(tf);

add(p);
pack();

parent = new java.awt.Window(this);
parent.setFocusable(true);
}

}

Greetings,

M4nti5

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> Hi Hinkmond, thanks again for your patience and quick response. I did what you proposed, and it's not working yet, here is the code:
>

Hi M4nti5,

> public class Main {
> public static void main(String args[])throws Exception{
> Test t = new Test();
> t.setVisible(true);
> while(true){
> Thread.sleep(100);
> if(t.focusTF())
> t.focusTextField();
> }
> }
> }
>
> ...
>
>
> public void focusTextField(){
> tf.setText("focus: "+tf.requestFocusInWindow());
> focusText = false;
> }
>

What is your current result? Do you see the the setText() being called
and your TextField then contains the debug info in it? What is the
value of the TextField when you click on the Button?

You don't have a tf.requestFocus() call any more. Is that on purpose?
You should try tf.requestFocus() instead of tf.requestFocusInWindow() in
your focusTextField() method.

If you don't see the values you expect, you should put more debug lines
in your code to track if actionPerformed is really doing what you think
it is, and if focusTextField() is being called correctly and has the
correct value for tf you think you should have in that thread (tf value
in focusTextField() needs to be the same as the tf value in your
actionPerformed() call).

Hinkmond

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

davyp
Offline
Joined: 2007-01-03
Points: 0

Hi M4nti5, Hinkmond,

I would also like to add it could also be an issue in my Windows Mobile port. As I have
not looked into it, I cannot rule out that setting the focus is not working as expected.
While the Personal Profile port works well for many people, there are still several
issues that need to be addressed and this could be another one.

Davy

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> Hi M4nti5, Hinkmond,
>
> I would also like to add it could also be an issue in my Windows Mobile port. As I have
> not looked into it, I cannot rule out that setting the focus is not working as expected.
> While the Personal Profile port works well for many people, there are still several
> issues that need to be addressed and this could be another one.
>

Good point. Thanks, Davy!

Hinkmond

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

m4nti5
Offline
Joined: 2010-04-12
Points: 0

Hi,

@Hinkmond:

>What is your current result? Do you see the the setText() being called
>and your TextField then contains the debug info in it? What is the
>value of the TextField when you click on the Button?

Currently after you press the "focus" button it doesn't give the focus to the textfield and it prints false on it.

>You don't have a tf.requestFocus() call any more. Is that on purpose?
>You should try tf.requestFocus() instead of tf.requestFocusInWindow() in
>your focusTextField() method.

After i saw the code of component.java i realized that they both call the function requestFocusHelper(boolean,boolean) so they're actually almost the same. The difference is that requestFocusInWindows returns a value, requestFocus doesn't.

>If you don't see the values you expect, you should put more debug lines
>in your code to track if actionPerformed is really doing what you think
>it is, and if focusTextField() is being called correctly and has the
>correct value for tf you think you should have in that thread (tf value
>in focusTextField() needs to be the same as the tf value in your
>actionPerformed() call).

Well, it prints "focus: false" on the textfield, so it looks like everything is doing it's job except requestFocusInWindow().

@davy:

Well, if the problem is in the port of windows mobile, i don't think it will be helpfull to continue debugging the code i gave.

@all who can help:

I think there is one quick solution to this thread, do you have a code that automaticaly give the focus to a textfield using personal profile and windows mobile 5? if you do, could you please post it?, if you don't, should i report this as a bug?. Can you figure out any walkarround?, thanks for all the support,

M4nti5

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> ...
> Well, it prints "focus: false" on the textfield, so it looks like everything is doing it's job except requestFocusInWindow().
>
> @davy:
>
> Well, if the problem is in the port of windows mobile, i don't think it will be helpfull to continue debugging the code i gave.
>
> @all who can help:
>
> I think there is one quick solution to this thread, do you have a code that automaticaly give the focus to a textfield using personal profile and windows mobile 5? if you do, could you please post it?, if you don't, should i report this as a bug?. Can you figure out any walkarround?, thanks for all the support,
>

Hi M4nti5,

There was a thread several years ago in our forums asking about the same
thing:

http://forums.sun.com/thread.jspa?threadID=5303645

The person never said if he solved his particular problem, but there
might be a hint in the forum thread, that the default component of the
window always gets the focus for a requestFocus() call.

So, maybe making sure which default component is set by rearranging the
order that you create, add and setVisible(true) the certain UI
components in your Panel may impact which component will get the focus
for a subsequent requestFocus() call. One workaround to try is to make
sure that the TextField is the first component in your Panel that is
created (new), added, and setVisible(true).

Hinkmond

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

davyp
Offline
Joined: 2007-01-03
Points: 0

I did some digging in the patches I made, and I can now confirm that it is my
Windows Mobile personal profile port that is causing this problem.

Basically, there is a PPCComponentPeer.java class that has a dummy
implementation for one of its requestFocus methods:

public boolean requestFocus(Component child, Window parent, boolean temporary, boolean focusedWindowChangeAllowed, long time)

This dummy method always returned false and that was the reason why you could not
set the focus for a particular widget.

I will have a look at the Qt peerset to see what this method is supposed to do, or at
least provide a slightly better alternative for the current dummy method.

Davy

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> I did some digging in the patches I made, and I can now confirm that it is my
> Windows Mobile personal profile port that is causing this problem.
>
> Basically, there is a PPCComponentPeer.java class that has a dummy
> implementation for one of its requestFocus methods:
>
> public boolean requestFocus(Component child, Window parent, boolean temporary, boolean focusedWindowChangeAllowed, long time)
>
> This dummy method always returned false and that was the reason why you could not
> set the focus for a particular widget.
>
> I will have a look at the Qt peerset to see what this method is supposed to do, or at
> least provide a slightly better alternative for the current dummy method.
>

No worries. Thanks, Davy! At least with that background info, we know
we aren't all going crazy. (Or, at least fully crazy) ;-)

Hinkmond

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

davyp
Offline
Joined: 2007-01-03
Points: 0

I have made new builds with a quick fix for the focus issue. I am not sure the fix covers
all cases, but it at least requesting focus works better now for the test example of
above. Check out the latest development builds (b161 - rev 20437) in the download
section on my site.

Davy

m4nti5
Offline
Joined: 2010-04-12
Points: 0

@Hinkmond: sorry, i'm allready nuts XD

@Davy: Hey, THANKS!!!, i proved it and works fine!

@all: thanks for the patience :D

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> @Hinkmond: sorry, i'm allready nuts XD
>
> @Davy: Hey, THANKS!!!, i proved it and works fine!
>
> @all: thanks for the patience :D
> [Message sent by forum member 'm4nti5']
>

Hi M4nti5,

Glad it worked out. :-)

Davy and Juan, thanks for your suggestions and helping M4nti5 get the
functionality he needed working!

Hinkmond

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

davyp
Offline
Joined: 2007-01-03
Points: 0

M4nti5: You're welcome

Hinkmond: I think Juan was helping out in another thread with Mathias' problem on
executing external binaries :-) Not sure if he is reading this thread.

Davy

Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> Hinkmond: I think Juan was helping out in another thread with Mathias' problem on
> executing external binaries :-) Not sure if he is reading this thread.
>

Oops. You're right. Thanks to you and Juan Lucas for helping on the
"Re: Runtime.getRuntime().exec() doesn't work" phoneME Advanced forum
thread too. ;-)

Hinkmond

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

jldominguez
Offline
Joined: 2008-01-02
Points: 0

Hello. I did not help m4nti5 technically, but he had all my moral support ;)