Results 1 to 4 of 4
- 10-06-2012, 09:30 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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!
- 10-06-2012, 09:44 PM #2
Re: If one JTextField is clicked clear another?
FocusListener
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-06-2012, 09:44 PM #3
Re: If one JTextField is clicked clear another?
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-06-2012, 11:20 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Press enter on JTextField to make focus on next JTextField?
By userno69 in forum AWT / SwingReplies: 3Last Post: 11-14-2011, 05:13 AM -
Buttons being clicked in certain order in GUI
By gfunk03 in forum AWT / SwingReplies: 8Last Post: 06-15-2011, 05:22 AM -
form appear after ok button clicked
By sks in forum NetBeansReplies: 1Last Post: 06-01-2011, 08:50 AM -
witch buttom was clicked?
By p0rnstar in forum New To JavaReplies: 6Last Post: 01-28-2010, 04:59 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks