Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By neilq5

Thread: If one JTextField is clicked clear another?

  1. #1
    neilq5 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default If one JTextField is clicked clear another?

    Hey Guys,

    I'm new to the Forum/Java.

    I'm taking an online course and plan to take the Java Associate Developer Exam in the next couple of months.

    There's a small project I'm working on and could use some help. I am making a Mile to Nautical Mile conversion calculator. I have got it working in the console and assumed it would be just as easy to do it in a JFrame, but that obviously wasn't the case!

    I have got it working near perfectly, just adding a few final touches.

    I have my JFrame with two JTextFields and a button. One JTextField is labelled Miles, the other Nautical. You simply enter the amount you want converted into one and press the button and the result will come up in the other.
    To do this, i add the following:

    Java Code:
    public void actionPerformed(ActionEvent e) {
    		String StrMile = entmiles.getText();
    		String StrNaut = entnaut.getText();
    	try{	
    		if(StrNaut.length() > 0){
    			nautcon = Double.parseDouble(StrNaut);
    			entmiles.setText(nautcon*1.15078 + "");
    		}else if(StrMile.length() > 0){
    			milecon = Double.parseDouble(StrMile);
    			entnaut.setText(milecon*0.868976 + "");
    		}
    	}catch(Exception e1){
    	JOptionPane.showMessageDialog(window, "Please Enter a valid number");	
    	}
    	
    		
    	}

    Now, my Problem.
    As you can see, it knows what to calculate based on if there is something in the JTextField. This is causing problems if i want to run more than one calculation as once i run one calculation both textfields will contain values.

    I don't want the user to have to use a reset button everytime they want to do a calculation, so is there a way that when i say click into the "entmiles" JTextField, the entnaut TextField will clear and vice versa?

    I appreciate any help I can get!

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

    Default Re: If one JTextField is clicked clear another?

    FocusListener

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

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

    Default Re: If one JTextField is clicked clear another?

    Moved from New to Java

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

  4. #4
    neilq5 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: If one JTextField is clicked clear another?

    Thanks for the help, i realised i'm better just clearing both fields when i click into one to start a new calculation.

    Thanks for the push in the right direction!
    DarrylBurke likes this.

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 05:13 AM
  2. Buttons being clicked in certain order in GUI
    By gfunk03 in forum AWT / Swing
    Replies: 8
    Last Post: 06-15-2011, 05:22 AM
  3. form appear after ok button clicked
    By sks in forum NetBeans
    Replies: 1
    Last Post: 06-01-2011, 08:50 AM
  4. witch buttom was clicked?
    By p0rnstar in forum New To Java
    Replies: 6
    Last Post: 01-28-2010, 04:59 PM
  5. Replies: 1
    Last Post: 01-30-2009, 06:44 PM

Tags for this Thread

Posting Permissions

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