Results 1 to 7 of 7
  1. #1
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Unhappy 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

    Java 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) {
        }
    }
    Thank you

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why did you abandon your previous threads for this problem?
    Why did you ignore the advice given in those threads?

  3. #3
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default

    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,

    Java Code:
    public class ChooseOptions2 extends MIDlet implements ActionListener, FocusListener{
    the i add these two,

    Java Code:
        sms = new Button("SMS");
            sms.addActionListener(this);
            sms.addFocusListener(this);
    
    
            internet = new Button("Internet");
            internet.addActionListener(this);
            internet.addFocusListener(this);
    then finally i came down to method focusGained but it give me arror

    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)
    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.

    Thanks

  4. #4
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default

    Hi,
    i found that the actual method signatures are

    Java 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()){
               
            }
    Tell me one thing in this case

    Java Code:
            sms.setSelectedStyle(buttonSelStyle);
            sms.setPressedStyle(buttonPrStyle);
    
            //}
            //sms.setNextFocusDown(internet);
          // form1.set
           internet.setSelectedStyle(buttonSelStyle);
           internet.setPressedStyle(buttonPrStyle);
    Why setPressedStyle is working, The question arises why one statement work for the internet button. Why setSelectedStyle is only not working. Any idea.

  5. #5
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Wink

    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

    Java Code:
        Style smsUnselStyle, smsButtonSelStyle, smsButtonPrStyle;
        Style internetUnselStyle, internetButtonSelStyle, internetButtonPrStyle;
    But tell me one thing if i have a lot of buttons then what will be the appraoch.

    Thanks

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  7. #7
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default

    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

  1. Tab Listener
    By teckno101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-29-2009, 10:40 PM
  2. JTable Cell Focus Listener
    By Manfizy in forum New To Java
    Replies: 1
    Last Post: 07-21-2009, 09:08 AM
  3. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-21-2008, 12:07 AM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 03:00 AM
  5. Replies: 1
    Last Post: 08-03-2007, 02:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •