Results 1 to 7 of 7
- 05-24-2010, 01:39 PM #1
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
How to write focus listener for buttons
Hi,
Hope you all will be fine. I have two buttons in form and i created three styles for the buttons. Selected, Unselected and pressed. The problem is when the form is first open the focus is by default on button1 whose name is sms and every thing works fine with it. But the problem is when i press down key and select second button whose name is internet the selectstyle doesn't work for it although pressStyle works and the message shown is "the focus is on null". I am confused whether to write actionlistener for it or focus listener for it. Please guide me and also tell me how can i write focus listener. I tried with focusGained(ActionEvent e) but i got an error.Here is the code
Thank youJava Code:public class ChooseOptions2 extends MIDlet implements ActionListener{ Form form1; Form form2; //RadioButton sms, internet; Button sms, internet; ButtonGroup group; TextArea txtArea1; TextArea txtArea2; Command cmdSelect = new Command("Select"); Command cmdBack = new Command("Back"); Command cmdExit = new Command("Exit"); Command cmdSend = new Command("Send"); public ChooseOptions2(){ //Initialize the LWUIT Display.init(this); } protected void startApp() throws MIDletStateChangeException { //Load the Theme // Resources r; // try { // r = Resources.open("/LWUITtheme.res"); // UIManager.getInstance(). // setThemeProps(r.getTheme("LWUITDefault")); // } catch (IOException e) { // e.printStackTrace(); // } // group = new ButtonGroup(); // // sms = new RadioButton("SMS"); // group.add(sms); // // // internet = new RadioButton("Internet"); // group.add(internet); Font font = Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE); sms = new Button("SMS"); sms.addActionListener(this); internet = new Button("Internet"); internet.addActionListener(this); //Setup Form 1 form1 = new Form("Please Select Options"); form1.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); //set text color for title form1.getTitleStyle().setFgColor(0x669999); //set font style for title form1.getTitleStyle().setFont(font); //set background color for the title bar form1.getTitleStyle().setBgColor(0x66CC33); //create a common style for unselected state Style unselStyle = new Style(); unselStyle.setFont(font); unselStyle.setBgTransparency(64); unselStyle.setFgColor(0xffc605); unselStyle.setBorder(Border.createEtchedRaised()); //create selected style for button Style buttonSelStyle = new Style(); buttonSelStyle.setBgColor(0x336633); buttonSelStyle.setFont(font); buttonSelStyle.setBorder(Border.createBevelRaised()); //internet.setSelectedStyle(buttonSelStyle); //set style for selected state of button // button.setSelectedStyle(buttonSelStyle); //create pressed style for button Style buttonPrStyle = new Style(); buttonPrStyle.setBgColor(0x006633); buttonPrStyle.setFont(font); buttonPrStyle.setBgTransparency(127); buttonPrStyle.setBorder(Border.createRoundBorder (12, 5, 0x006600)); //set style for pressed state of button //internet.setPressedStyle(buttonPrStyle); //sms.setUnselectedStyle(unselStyle); //internet.setUnselectedStyle(unselStyle); sms.setSelectedStyle(buttonSelStyle); sms.setPressedStyle(buttonPrStyle); // if (internet.isSelected()){ //internet.setFocus(true); //} //sms.setNextFocusDown(internet); internet.setSelectedStyle(buttonSelStyle); internet.setPressedStyle(buttonPrStyle); // internet.setFocusable(true); //create a new style object Style menuStyle = new Style(); //set the background color -- the same as for title bar menuStyle.setBgColor(0x66CC33); //set the text color for soft button menuStyle.setFgColor(0x669999); //set font style for soft button menuStyle.setFont(font); //now install the style for soft button form1.setSoftButtonStyle(menuStyle); //set a background color for the form form1.getStyle().setBgColor(0x66FF99); form1.addComponent(sms); form1.addComponent(internet); //form1.addComponent(BorderLayout.NORTH, new Label("My First Form")); //form1.addComponent(BorderLayout.WEST, new Label("WEST")); //form1.addComponent(BorderLayout.CENTER, new Label("CENTER")); //form1.addComponent(BorderLayout.EAST, new Label("EAST")); //form1.addComponent(BorderLayout.SOUTH, new Label("Click Rotate")); form1.addCommand(cmdSelect); form1.addCommand(cmdExit); form1.addCommandListener(this); form1.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 1000)); //Setup Form 2 form2 = new Form("Form 2"); //set Form layout form2.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); //Create container that holds component and set its layout // Container textBoxContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS)); //create text Area txtArea1 = new TextArea("", 2, 10, TextArea.ANY); txtArea1.setEditable(true); txtArea1.setGrowByContent(true); txtArea1.getStyle().setBorder(Border.createLineBorder(2)); // txtArea1.requestFocus(); //txtArea1.addFocusListener(this); txtArea1.setFocusable(true); // txtArea1.setText("hi how r u asdsadsadasdasdasdasdasdasdasdasdsadsadsad"); //txtArea1.setFocusable(true); //adding txtArea1 to container //textBoxContainer.addComponent(txtArea1 ); //create text Area2 txtArea2 = new TextArea(2, 5); txtArea2.setEditable(false); //txtArea1.setFocus(true); // txtArea1.setEnabled(true); //txtArea1.setFocusable(true); //txtArea1.setFocusPainted(true); //adding txtArea1 to container //textBoxContainer.addComponent(txtArea2 ); //form2.addComponent(0,null,new Label("This is the second Form")); //form2.addComponent(textBoxContainer); form2.addCommand(cmdSend); form2.addCommand(cmdBack); form2.addCommandListener(this); form2.addComponent(txtArea1); form2.addComponent(txtArea2); // form2.setFocusable(true); //form2.setFocused(txtArea1); // form2.setFocused(textBoxContainer.getComponentAt(0)); System.out.println("the focus is on: " + form2.getFocused() ); form2.setTransitionInAnimator(Transition3D.createCube(1000, true)); form1.show(); } public void actionPerformed(ActionEvent evt) { //check which command cliked if (evt.getCommand()==cmdExit) { notifyDestroyed(); } else if (evt.getCommand()==cmdSelect) { form2.show(); } else if (evt.getCommand()==cmdBack) { form1.show(); } else if (evt.getCommand()==cmdSend) { String text = txtArea1.getText(); System.out.println("txtArea1 contain this text: " + text); //try{ //new SendDataToCGI().getData(); //} catch(IOException e){ // e.printStackTrace(); // } } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
- 05-24-2010, 02:38 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Why did you abandon your previous threads for this problem?
Why did you ignore the advice given in those threads?
- 05-24-2010, 03:14 PM #3
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
Hi,
what advice i ignored? I am stuck. First you guide me to make forms not to make different midlets, i follow it, actaully i did two things tell me i am doing right or wrong.first i add this,
the i add these two,Java Code:public class ChooseOptions2 extends MIDlet implements ActionListener, FocusListener{
then finally i came down to method focusGained but it give me arrorJava Code:sms = new Button("SMS"); sms.addActionListener(this); sms.addFocusListener(this); internet = new Button("Internet"); internet.addActionListener(this); internet.addFocusListener(this);
It says cannot find symbol class FocusEvent. When you write ActionListener(ActionEvent) it doesn't say Cannot find ActionEvent class. why it is saying this. Am i using the wrong method signature or what i am doing wrong so this message is coming.Java Code:public void focusGained(FocusEvent e){ } public void actionPerformed(ActionEvent evt) { //check which command cliked if (evt.getCommand()==cmdExit) { notifyDestroyed(); } else if (evt.getCommand()==cmdSelect)
Thanks
- 05-24-2010, 03:38 PM #4
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
Hi,
i found that the actual method signatures are
Tell me one thing in this caseJava Code:public void focusLost(Component cmpnt) { //System.out.println("Hi thjasdtsa: " + cmpnt.) //throw new UnsupportedOperationException("Not supported yet."); } public void focusGained(Component cm) { System.out.println("Hi thjasdtsa: " + cm.hasFocus()); if (cm.hasFocus()){ }
Why setPressedStyle is working, The question arises why one statement work for the internet button. Why setSelectedStyle is only not working. Any idea.Java Code:sms.setSelectedStyle(buttonSelStyle); sms.setPressedStyle(buttonPrStyle); //} //sms.setNextFocusDown(internet); // form1.set internet.setSelectedStyle(buttonSelStyle); internet.setPressedStyle(buttonPrStyle);
- 05-24-2010, 04:58 PM #5
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
Hey i solve it :). I don't know that i am saying right or wrong but please correct me if i am wrong. Actaully what i was doing is that i have two buttons in my form and i defined only one style for both. But when i defined style for both it worked.
Like this
But tell me one thing if i have a lot of buttons then what will be the appraoch.Java Code:Style smsUnselStyle, smsButtonSelStyle, smsButtonPrStyle; Style internetUnselStyle, internetButtonSelStyle, internetButtonPrStyle;
Thanks
- 05-25-2010, 06:25 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
I said
1.) You should not write code without the API specs for the classes you intend to be using. If you followed that advice you would have known the exact method signatures.
2.) I said you should not setFocus but instead use control.requestFocus.
If you have many buttons that share the same style then create a subclass of Button that you dress with that style. Then you create instances of your (styled) Button instead. Alternatively, you can create a Button factory method that gives back a button dressed with the style that you want.
- 05-25-2010, 07:01 AM #7
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
Hi,
Thanks for your help:).But i want to tell you i followed your advice.I immediately downlaod it from internet when u said to me to do this.Yes i agree for focus listener i didn't see it but it was because i forgot because i am newbie and also it seemed i need focus listener but it was not. It was something else but you learn from your mistakes so did I, but now i am referring it for everything like form,or whatever LWUIT contain.
Anyways thanks for your help:). Now i have another question but i think it needs separate thread.
Thank you again:);)
Similar Threads
-
Tab Listener
By teckno101 in forum AWT / SwingReplies: 2Last Post: 09-29-2009, 09:40 PM -
JTable Cell Focus Listener
By Manfizy in forum New To JavaReplies: 1Last Post: 07-21-2009, 08:08 AM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-20-2008, 11:07 PM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-10-2008, 02:00 AM -
How can i write listener on ArrowButtons in Basic ScrollBarUI. in 1.5 version
By praveenrn in forum AWT / SwingReplies: 1Last Post: 08-03-2007, 01:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks