1,105,380 Community Members

How to Display in JTextfield with Conversion Calculator

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi All,

I was having trouble with getting my program to display all the necessary conversions. Here is a snippet of the code to help explain what I want to do:

private class CalcListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			String centInput, inchInput, meterInput, yardInput;
			double cent, inch, meter, yard;
			
			yardInput = yardsText.getText();
			inch = Double.parseDouble(yardInput) * 36.00;
			cent = Double.parseDouble(yardInput)* 91.44;
			meter = Double.parseDouble(yardInput) * 0.9144;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));

This will allow me to put an input into the "Yards" Textfield so that when I click calculate, the inch, cent, and meter Textfields will display their proper conversions. So my question is, How would I be able to make an input in the "Inch" Textfield, so that when I click the calculate button it gives all the conversions to the other units of measurements?(I need to be able to put an input into any textfield and get the proper conversions in the other ones.) But If I put similar code to the one I posted for the different input, I just get errors. I hope that I explained my question thoroughly enough. Thanks for any help in advance!

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

I just get errors

If you copy and paste the error messages here someone can help you.

How would I be able to make an input in the "Inch" Textfield,

Are you talking about the program changing the text field or about the user doing it?
The text field class has a method to set the text field's value. I don't know how you can make a user type into the text field.

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the reply! I am talking about the user changing input and being able to put input into any textbox that they please and being able to get the proper conversions to display into the other textfields. I have changed my code around to try to figure it out so here is my new code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ConversionCalculator extends JFrame{

	final int WINDOW_WIDTH = 900;
	final int WINDOW_HEIGHT = 200;
	
	private JPanel panel1, panel2, panel3;
	private JLabel centLabel, inchLabel, meterLabel, yardsLabel;
	private JTextField centText, inchText, meterText, yardsText;
	private JButton clear, calc, exit;
	private Container pane;
	
	public ConversionCalculator(){
		setTitle("Conversion Calculator");
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pane = getContentPane();
		pane.setLayout(new GridLayout(1,3));
		buildPanel();
		pane.add(panel1);
		pane.add(panel2);
		pane.add(panel3);
		setVisible(true);
	}
	
	private void buildPanel(){
		
		centLabel = new JLabel("Centimeters ", SwingConstants.LEFT);
		inchLabel = new JLabel("Inches ", SwingConstants.CENTER);
		meterLabel = new JLabel("Meters", SwingConstants.LEFT);
		yardsLabel = new JLabel("Yards", SwingConstants.CENTER);
		
		centText= new JTextField(8);
		inchText= new JTextField(8);
		meterText = new JTextField(8);
		yardsText = new JTextField(8);
		
		clear = new JButton("Clear");
		calc = new JButton("Calculate");
		exit = new JButton("Exit");
		
		clear.addActionListener(new ClearListener());
		calc.addActionListener(new CalcListener());
		exit.addActionListener(new ExitListener());
		
		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(2,2));
		panel1.add(centLabel); panel1.add(centText); 
		panel1.add(meterLabel); panel1.add(meterText); 
		
		panel2 = new JPanel();
		panel2.setLayout(new GridLayout(2,2));
		panel2.add(inchLabel); panel2.add(inchText);
		panel2.add(yardsLabel); panel2.add(yardsText);
		
		panel3 = new JPanel();
		panel3.setLayout(new GridLayout(3,1));
		panel3.add(clear);
		panel3.add(calc);
		panel3.add(exit);
	}
	
	private class ClearListener implements ActionListener{
		public void actionPerformed(ActionEvent d){
			centText.setText("");
			inchText.setText("");
			meterText.setText("");
			yardsText.setText("");
		}
	}
	private class CalcListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			String centInput = null, inchInput = null, meterInput = null, yardInput = null;
			double cent=0, inch=0, meter=0, yard=0;
			
			if(yardInput == yardsText.getText()){
			inch = Double.parseDouble(yardInput) * 36.00;
			cent = Double.parseDouble(yardInput)* 91.44;
			meter = Double.parseDouble(yardInput) * 0.9144;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));
			}
				
			if(inchInput == inchText.getText()){
			cent = Double.parseDouble(inchInput)* 2.54;
			meter = Double.parseDouble(inchInput)* 0.0254;
			yard = Double.parseDouble(inchInput) * 0.0278;
			
			yardsText.setText(String.valueOf(yard));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));
			}
			
			if(centInput == centText.getText()){
			inch = Double.parseDouble(centInput) * 0.393701;
			meter = Double.parseDouble(centInput)* 0.01;
			yard = Double.parseDouble(centInput) * 0.010936;
			
			inchText.setText(String.valueOf(inch));
			yardsText.setText(String.valueOf(yard));
			meterText.setText(String.valueOf(meter));
			}
			
			if(meterInput == meterText.getText()){
			inch = Double.parseDouble(meterInput) * 39.370079;
			cent = Double.parseDouble(meterInput)* 100.00;
			yard = Double.parseDouble(meterInput) * 1.093613;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			yardsText.setText(String.valueOf(yard));
			}
		}
	}
	
	private class ExitListener implements ActionListener{
		
		public void actionPerformed(ActionEvent f){
			System.exit(0);
		}
	}
}

Here is the driver class:

public class ConversionCalculatorDriver {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ConversionCalculator convCalc = new ConversionCalculator();

	}

}

The problem I am now having is that the user inputs a number into any textfield and pushes the "calculate" button, but no conversions are displaying into the other textfields. Also, no more error codes/messages now. Thanks for help again in advance!

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

The problem I am now having is that the user inputs a number into any textfield and pushes the "calculate" button, but no conversions are displaying into the other textfields

Can you detect which text field the user entered his data in?
Given that, you should then know which of the other fields need to be updated.

Please explain what you want this statement to do:

if(yardInput == yardsText.getText()){
Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

I am guessing that my code does not detect which text field the user entered their data in, because I do not know how to do that. I was trying to accomplish that with the if statements, but I do not think I did it properly, as it lets me type input into any of the textfields but does not display anything into the other textfields after the "calculate" button is pushed. How can I properly detect which textfield has input in it? Thanks again!

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Look at the code in the actionListener method for the calc button.
What does that code do?
What do you want it to do?

Please explain what you want this statement to do:

if(yardInput == yardsText.getText()){
Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

In the actionListener method: I want to it check which box has input in it and when the calculate button is pushed I want it convert the input into the other units of measurement and display them in the appropriate text field. But my code is not doing much of anything because nothing displays when I click calculate.

That if statement you took from my code: I would like it to check and see if there is input in that text field and if there is I would like it to use the following code to do the conversions and then display them in the other textfields.

Thanks again.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

I would like it to check and see if there is input in that text field

The statement is comparing a variable with a null value to reference to a String variable that is returned by the getText() method.
First problem here is that the == operator tests if two variables refer to the same object, not if two different objects contain the same String.
You should use the equals method to compare Strings.

If the getText method returns a null and the if statement is true, then what?
Where do you get the data from the text field for the computations that are done inside of the if statement?

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

So I would need to do a scanner to see what input is entered in the text field to read what data I am doing the conversions with?

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

The getText() method returns what input is entered in the text field.

The Scanner class is for reading from the console or from a disk file.

Also you could read in the tutorial about text fields.
Go to this site and Find Text:
http://download.oracle.com/javase/tutorial/reallybigindex.html
There are several topics listed include one for Text Fields.

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok, so if I implement the equals method:

private class CalcListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			String centInput = null, inchInput = null, meterInput = null, yardInput = null;
			double cent=0, inch=0, meter=0, yard=0;
			
			if(yardInput.equals(yardsText.getText())){
			inch = Double.parseDouble(yardInput) * 36.00;
			cent = Double.parseDouble(yardInput)* 91.44;
			meter = Double.parseDouble(yardInput) * 0.9144;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));
			}

then it would compare the input with what is in the textfield and if there is something entered in the textfield it will compute the conversions and display. This is what I am hoping it to do. But I get null errors as follows:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:81)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6373)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
	at java.awt.Component.processEvent(Component.java:6138)
	at java.awt.Container.processEvent(Container.java:2085)
	at java.awt.Component.dispatchEventImpl(Component.java:4735)
	at java.awt.Container.dispatchEventImpl(Container.java:2143)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
	at java.awt.Container.dispatchEventImpl(Container.java:2129)
	at java.awt.Window.dispatchEventImpl(Window.java:2478)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
	at java.awt.EventQueue.access$000(EventQueue.java:85)
	at java.awt.EventQueue$1.run(EventQueue.java:638)
	at java.awt.EventQueue$1.run(EventQueue.java:636)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
	at java.awt.EventQueue$2.run(EventQueue.java:652)
	at java.awt.EventQueue$2.run(EventQueue.java:650)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:81)

What variable on line 81 is null? When you see which one, back track in your code to see why it is null.

If you can not see which one, Add a println statement just before line 81 that prints out the values of all the variables on line 81.


Look at line 4 in the code you just posted.
Where do any of the variables defined in that line get values?

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

For debugging add some printlns at the very beginning of the listener method to show what are in the text fields. Something like this for all the text fields:

System.out.println("yardsText.getText()=" + yardsText.getText() + "<");
Member Avatar
sirlink99
Master Poster
766 posts since Oct 2010
Reputation Points: 44 [?]
Q&As Helped to Solve: 25 [?]
Skill Endorsements: 3 [?]
 
0
 

if you are using if statements then I would suggest you store the last value in each textfield then you compare them to see if they are not equal (has been changed) then you can recalculate the rest of the values using the one you used in the if statement. Then all you would need to do would be to set the new value into the text fields.

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

@ NormR1:

I added some printlns to see the values in the textfield:

public void actionPerformed(ActionEvent e){
			String centInput = null, inchInput = null, meterInput = null, yardInput = null;
			double cent=0, inch=0, meter=0, yard=0;
			
			//Test for output
			System.out.println("yardsText.getText()=" + yardsText.getText() + "<");
			System.out.println("inchText.getText()=" + inchText.getText() + "<");
			System.out.println("meterText.getText()=" + meterText.getText() + "<");
			System.out.println("centText.getText()=" + centText.getText() + "<");
			
			if(yardInput.equals(yardsText.getText())){
			inch = Double.parseDouble(yardInput) * 36.00;
			cent = Double.parseDouble(yardInput)* 91.44;
			meter = Double.parseDouble(yardInput) * 0.9144;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));
			}

and this is the result:

yardsText.getText()=10<Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:87)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6373)

inchText.getText()=<
meterText.getText()=<
centText.getText()=<
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
	at java.awt.Component.processEvent(Component.java:6138)
	at java.awt.Container.processEvent(Container.java:2085)
	at java.awt.Component.dispatchEventImpl(Component.java:4735)
	at java.awt.Container.dispatchEventImpl(Container.java:2143)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
	at java.awt.Container.dispatchEventImpl(Container.java:2129)
	at java.awt.Window.dispatchEventImpl(Window.java:2478)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
	at java.awt.EventQueue.access$000(EventQueue.java:85)
	at java.awt.EventQueue$1.run(EventQueue.java:638)
	at java.awt.EventQueue$1.run(EventQueue.java:636)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
	at java.awt.EventQueue$2.run(EventQueue.java:652)
	at java.awt.EventQueue$2.run(EventQueue.java:650)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

All it prints is what the user puts as input into whichever textfield they enter input to, and the other 3 values there is no data for. How can I use the results from the printllns to help me figure out what I need to do.
Thanks for the help once again!

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:87)

What variable is null at line 87?

Again: What are you testing here:

if(yardInput.equals(yardsText.getText())){

What is the value of yardInput?
If the equals returns true, you use yardInput on the next line:

inch = Double.parseDouble(yardInput) * 36.00;

How did it get a value that you want to parse?

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

yardInput is null @ line 87.

if(yardInput.equals(yardsText.getText())){

:
Here I am testing if yardInput equals what the user put into the textfield.

inch = Double.parseDouble(yardInput) * 36.00;

:
I cannot get a value that I want to parse because all of the conversions are based off of the user input, so there are no values being given to it, because inch is initialized as a 0.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

You can NOT call a method using a null variable. Here equals() is a method of the String class, but yardInput is null and this will NOT work.
yardInput.equals(

On this line you would attempt to convert the contents of yardInput to a double value.
inch = Double.parseDouble(yardInput) * 36.00;
But you NEVER put anything into the yardInput variable.

Neither of these statements will ever work unless you put a value into yardInput.

yardInput will not magically get a value. You must write an assignment statement that puts a value into it.

Member Avatar
javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok, I get what you are saying about me trying to use these without values. I corrected it and it works fine if I input a value into the "yards" textfield. I get an empty string error though if user puts a value into any textfield other than the "yards" text field, as seen here:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
	at java.lang.Double.parseDouble(Double.java:510)
	at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:85)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6373)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
	at java.awt.Component.processEvent(Component.java:6138)
	at java.awt.Container.processEvent(Container.java:2085)
	at java.awt.Component.dispatchEventImpl(Component.java:4735)
	at java.awt.Container.dispatchEventImpl(Container.java:2143)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
	at java.awt.Container.dispatchEventImpl(Container.java:2129)
	at java.awt.Window.dispatchEventImpl(Window.java:2478)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
	at java.awt.EventQueue.access$000(EventQueue.java:85)
	at java.awt.EventQueue$1.run(EventQueue.java:638)
	at java.awt.EventQueue$1.run(EventQueue.java:636)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
	at java.awt.EventQueue$2.run(EventQueue.java:652)
	at java.awt.EventQueue$2.run(EventQueue.java:650)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

This is how I have a portion of my code written now:

public void actionPerformed(ActionEvent e){
			String centInput = centText.getText() , inchInput = inchText.getText(), meterInput = meterText.getText(), 
								yardInput = yardsText.getText();
			
			double cent=0, inch=0, meter=0, yard=0;
			
			if(yardInput.equals(yardsText.getText()))
			{
				yardInput.equals(Double.parseDouble(yardsText.getText()));
				inch = Double.parseDouble(yardInput) * 36.00;
				cent = Double.parseDouble(yardInput) * 91.44;
				meter = Double.parseDouble(yardInput) * 0.9144;
			
				inchText.setText(String.valueOf(inch));
				centText.setText(String.valueOf(cent));
				meterText.setText(String.valueOf(meter));
				
				//Test for output
				System.out.println("yardsText.getText()=" + yardsText.getText() + "<");
				System.out.println("inchText.getText()= " + inch + "<");
				System.out.println("meterText.getText()= " + meter + "<");
				System.out.println("centText.getText()= " + cent + "<");
			}
			
			else if(inchInput.equals(inchText.getText()))
			{
				inchInput.equals(Double.parseDouble(inchText.getText()));
				cent = Double.parseDouble(inchInput)* 2.54;
				meter = Double.parseDouble(inchInput)* 0.0254;
				yard = Double.parseDouble(inchInput) * 0.0278;
			
				yardsText.setText(String.valueOf(yard));
				centText.setText(String.valueOf(cent));
				meterText.setText(String.valueOf(meter));
				
				//Test for output
				System.out.println("inchText.getText()= " + inchText.getText() + "<");
				System.out.println("yardsText.getText()=" + yard + "<");
				System.out.println("meterText.getText()= " + meter + "<");
				System.out.println("centText.getText()= " + cent + "<");
			}

			else if(centInput.equals(centText.getText()))
			{
				centInput.equals(Double.parseDouble(centText.getText()));
				inch = Double.parseDouble(centInput) * 0.393701;
				meter = Double.parseDouble(centInput)* 0.01;
				yard = Double.parseDouble(centInput) * 0.010936;
			
				inchText.setText(String.valueOf(inch));
				yardsText.setText(String.valueOf(yard));
				meterText.setText(String.valueOf(meter));
				
				//Test for output
				System.out.println("centText.getText()= " + centText.getText()  + "<");
				System.out.println("yardsText.getText()=" + yard + "<");
				System.out.println("inchText.getText()= " + inch+ "<");
				System.out.println("meterText.getText()= " + meter + "<");
			}

			else //if(meterInput.equals(meterText.getText()))
			{
				meterInput.equals(Double.parseDouble(meterText.getText()));
				inch = Double.parseDouble(meterInput) * 39.370079;
				cent = Double.parseDouble(meterInput)* 100.00;
				yard = Double.parseDouble(meterInput) * 1.093613;
			
				inchText.setText(String.valueOf(inch));
				centText.setText(String.valueOf(cent));
				yardsText.setText(String.valueOf(yard));
				
				//Test for output
				System.out.println("meterText.getText()= " + meterText.getText()  + "<");
				System.out.println("yardsText.getText()=" + yard + "<");
				System.out.println("inchText.getText()= " + inch+ "<");
				System.out.println("centText.getText()= " + cent + "<");
			}
			
		}
	}

I am trying to get it so that a user can insert an input into any of the four text fields and it converts the number into its perspective length and displays it in the appropriate textfield, but as of now it only works if the user puts a value into the "yards" textfield and not the others.
As always thanks for the help you have been giving me.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

You need to work through your logic and consider what the if tests are doing.
The following code is similar to what your code is doing:
x = 1;
if (x == 1) {

With these two statements the if test will ALWAYS be true.
That is exactly what your code is doing with the text field contents.

You need to look at what you want to test for BEFORE you attempt to convert a text field's contents to a number.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: