Results 1 to 3 of 3
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default How to set foucs on first label after a user press cancel

    Hi,

    I am trying to set foucs on a label in the following code; i am able to set it when the user enters the values first time; i also want to set focus on the same label after the user presses cancel

    Java Code:
    /*<applet code = "sumApplet.class" width="320" height="250"></applet>*/
    public class sumApplet extends java.applet.Applet implements java.awt.event.ActionListener, java.awt.event.FocusListener 
    {
    	java.awt.Label firstNum, secondNum, result;
    	java.awt.TextField f, s, r;
    	java.awt.Button sum, cancel;
    
    	public sumApplet() {
    		setLayout(null);
    		firstNum = new java.awt.Label("First Number");
    		secondNum = new java.awt.Label("Second Number");
    		result = new java.awt.Label("result");
    		f = new java.awt.TextField();
    		s = new java.awt.TextField();
    		r = new java.awt.TextField();
    		r.setEditable(false);
    		sum = new java.awt.Button("Sum");
    		cancel = new java.awt.Button("Cancel");
    		add(firstNum);
    		firstNum.setBounds(10, 30, 60, 20);
    		add(f);
    		f.setBounds(90, 30, 20, 20);
    		add(secondNum);
    		secondNum.setBounds(10, 70, 80, 20);
    		add(s);
    		s.setBounds(100, 70, 20, 20);
    		add(result);
    		result.setBounds(10, 100, 60, 20);
    		add(r);
    		r.setBounds(90,100,40,20);
    		add(sum);
    		sum.setBounds(10, 140, 40, 30);
    		add(cancel);
    		cancel.setBounds(60, 140, 60, 30);
    		sum.addActionListener(this);
    		cancel.addActionListener(this);
    		f.addFocusListener(this);
    	}
    
    	public void actionPerformed(java.awt.event.ActionEvent e) 
    	{
    		java.awt.Button b = (java.awt.Button) e.getSource();
    		try
    		{
    		if(b.getLabel().equals("Sum"))
    		{
    			String s1 = f.getText();
    			String s2 = s.getText();
    			int sum = Integer.parseInt(s1) + Integer.parseInt(s2);
    			String toResult = " "+ sum;
    			r.setText(toResult);
    		}
    		else
    		{
    			f.setText(null);
    			s.setText(null);
    			r.setText(null);
    		}
    	}
    		catch(java.lang.NumberFormatException nfe)
    		{
    			System.out.println("NumberFormatException" + " " + nfe.getLocalizedMessage());
    		}
    	}
    	public void focusGained(java.awt.event.FocusEvent fg)
    	{
    		f.setFocusable(isActive());
    	}
    	public void focusLost(java.awt.event.FocusEvent fl)
    	{
    		//f.lostFocus(e, what)
    	}
    }
    Output:-
    How to set foucs on first label after a user press cancel-1.png

    Please help.

    Thanks in advance

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: How to set foucs on first label after a user press cancel

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: How to set foucs on first label after a user press cancel

    Read through the methods of JLabel, including the methods it inherits from superclasses, looking for any methods that might request the focus. Read the API carefully so you make the right choice -- don't use methods whose use is discouraged.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Cancel button for exiting app
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 07:20 PM
  2. Struts 2 cancel button
    By kirtichopra2003 in forum Advanced Java
    Replies: 0
    Last Post: 09-29-2009, 03:40 PM
  3. Cancel Particular scheduled task
    By hjava in forum New To Java
    Replies: 0
    Last Post: 08-11-2009, 11:10 PM
  4. How should I cancel this thread ?
    By playwin2 in forum Threads and Synchronization
    Replies: 7
    Last Post: 08-27-2008, 06:20 PM
  5. Press any key to continue/press enter
    By JT4NK3D in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 09:27 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
  •