Skip to main content

3 buttons A,B,C are added on Container. How to focus button C by default?

7 replies [Last post]
perfectlifepan
Offline
Joined: 2005-10-02
Points: 0

3 buttons A,B,C are added on Container.


Container con = new Container();
con.setLayout(new GridLayout(1,3));
Button A = new Button("A");
Button B = new Button("B");
Button C = new Button("C");
con.addComponent(A);
con.addComponent(B);
con.addComponent(C);

which method can I call to focus button C by default?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ghady_rayess
Offline
Joined: 2009-01-09
Points: 0

just call C.requestFocus();

perfectlifepan
Offline
Joined: 2005-10-02
Points: 0

Hi
c.requestFocus() seem not workable on Container.
when I call c.requestFocus(), following debug message cant be called.

[b]c.addFocusListener(
new FocusListener(){
public void focusGained(Component c){
System.out.println("focus sucessfully");
}
public void focusLost(Component c){

}
}
);[/b]

ghady_rayess
Offline
Joined: 2009-01-09
Points: 0

you have to set the container's focusable to true.

c.setFocusable(true);

perfectlifepan
Offline
Joined: 2005-10-02
Points: 0

container, button A,B,C are all focusable
i.e
[b]container.setFocusable(true);
a.setFocusable(true);
b.setFocusable(true);
c.setFocusable(true); [/b]

Button C still cant be focused After it call requestFocus();

Shai Almog

You don't need to make the container focusable.
Make sure to call requestFocus when the component has a parent form
otherwise the method will do nothing...
Form f = new Form();
f.setLayout(new BorderLayout());
Container con = new Container();
con.setLayout(new GridLayout(1,3));
Button A = new Button("A");
Button B = new Button("B");
Button C = new Button("C");
con.addComponent(A);
con.addComponent(B);
con.addComponent(C);
f.addComponent(BorderLayout.NORTH, con);
C.requestFocus();
f.show();

> container, button A,B,C are all focusable
> i.e
> [b]container.setFocusable(true);
> a.setFocusable(true);
> b.setFocusable(true);
> c.setFocusable(true); [/b]
>
>
> Button C still cant be focused After it call requestFocus();
> [Message sent by forum member 'perfectlifepan' (perfectlifepan)]
>
> http://forums.java.net/jive/thread.jspa?messageID=361205
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>

--
Shai Almog

LWUIT Blog http://lwuit.blogspot.com/
LWUIT FAQ https://lwuit.dev.java.net/faq.html
YouTube Channel http://www.youtube.com/user/vprise
Applications in LWUIT https://lwuit.dev.java.net/nonav/featuredapp.html

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

perfectlifepan
Offline
Joined: 2005-10-02
Points: 0

Thank for all..
I have fixed this problem yesterday

The reason why I cant focus the button because
c.resquestFocus() is called directly in keyListener of BBScreenImplementation

The solution is
1) c.requestFocus() put in a Thread first
2) new the thread and start it in keyListener of BBScreenImplementation

jules_
Offline
Joined: 2009-05-15
Points: 0

Hi,

check the api's you'll find ur answer in 30 secs.

Jules