i posted a problem here a while back on doing a program, and i got the help i needed... but i'm faced with another problem. this code seems to be working fine but i want it store the values i entered into another text field... i cant seem to figure out how to do it. Right now there are 2 different text fields for entering the values for celsius and fahrenheit, i need another text field that stores the values i entered... can someone please help me?

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.text.*;

// A Java applet that coverts Fahrenheit temperatures to Celsius temperatures.
public class TempConversion extends Applet
{
     private Label lblFahrenheit, lblCelsius;
     private TextField txtFahrenheit, txtCelsius;
     private Button Calculate;
     private Panel panel;

     // Method that gets the components and puts them on the applet.
     public void init ()
     {
          setBackground (Color.cyan);
          panel = new Panel ();
          panel.setLayout (new GridLayout (5, 1, 12, 12));

          lblFahrenheit = new Label ("Fahrenheit");
          txtFahrenheit = new TextField (10);
          lblCelsius = new Label ("Celsius");
          txtCelsius = new TextField (10);
          Calculate = new Button ("Calculate");
          Calculate.addActionListener (new CalculateListener ());

          panel.add (lblFahrenheit); panel.add (txtFahrenheit);
          panel.add (lblCelsius); panel.add (txtCelsius);
          panel.add (Calculate);
          add (panel);
     } // method init

     // Inner class to listen for the Calculate button.
     class CalculateListener implements ActionListener
     {
          public void actionPerformed (ActionEvent event)
          {
               double fahrenheit, celsius;
               fahrenheit = AppletIO.getDouble (txtFahrenheit);
               celsius = (fahrenheit - 32) * 5 / 9;
               txtCelsius.setText ("" + AppletIO.decimals (celsius));
          } // method actionPerformed
     } // class CalculateListener
} // class TempConversion

// A class that handles IO for an applet.
class AppletIO
{
     // Gets a double from the TextField and returns it to the calling method.
     public static double getDouble (TextField box)
     {
          double number;
          try
          {
               number = Double.valueOf (box.getText ()).doubleValue ();
          } catch (NumberFormatException e) { number = 0;}
          return number;
     } // method getDouble

     // Formats a double for string output with two decimal places.
     public static String decimals (double number)
     {
          DecimalFormat decFor = new DecimalFormat ();
          decFor.setMaximumFractionDigits (2);
          decFor.setMinimumFractionDigits (2);
          return decFor.format (number);
     } // method decimals

     // Gets an integer from the TextField and returns it to the calling method.
     public static int getInteger (TextField box)
     {
          int number;
          try
          {
               number = Integer.parseInt (box.getText());
          } catch (NumberFormatException e) { number = 0;}
          return number;
     } // method getInteger
} // class AppletIO

Your question is not clear on what you want to do with the extra text field, but you are already manipulating two fields just fine. Why can you not just add the third and do whatever you want with it? What is giving you difficulties?

oh with the extra text field, i just want to store the values i put in for either the celsius or fahrenheit into the text field. suppose i put in 23 degrees in to the celsius box, i want 23 to appear on the third text field

Then post what you tried, because there is no attempt at that in the code you posted above and we can't just look over your shoulder.

ok this is what i did.. and i'm getting a giant convert box but no text box or anything. this is what i tried

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.plaf.BorderUIResource.TitledBorderUIResource;


public class CFConverter extends Applet {

	private JButton btnConvert;
	private JTextField cfield, ffield ;

	/**
	 * Initialize.
	 */
	public void init()
	{


		JPanel cpanel = new JPanel();
		cpanel.setBorder(new TitledBorderUIResource("Celsius"));
		cfield = new JTextField(10);
		JPanel fpanel = new JPanel();
		fpanel.setBorder(new TitledBorderUIResource("Fahrenheit"));
		ffield = new JTextField(10);
		JPanel kpanel = new JPanel();

		cpanel.add(cfield);
		fpanel.add(ffield);

		btnConvert = new JButton("Convert");
		ButtonListener listener = new ButtonListener();
		btnConvert.addActionListener(listener);
		cfield.addKeyListener(new FieldsListener());
		ffield.addKeyListener(new FieldsListener());

		//add(cpanel);
		//add(fpanel);

		//add(btnConvert);
//JLabel label;




				       // label=new JLabel();
				        setLayout(new BorderLayout());

				        JScrollBar hbar = new JScrollBar(
				                JScrollBar.HORIZONTAL, 1, 1, 0, 3);
				        JScrollBar vbar = new JScrollBar(
				                JScrollBar.VERTICAL, 1, 1, 0, 3);

				        hbar.setUnitIncrement(2);
				        hbar.setBlockIncrement(1);

				        hbar.addKeyListener(new FieldsListener());
				        vbar.addKeyListener(new FieldsListener());

				        add(hbar, BorderLayout.SOUTH);
				        add(vbar, BorderLayout.EAST);

				       //add(label, BorderLayout.CENTER);
						add(cpanel,BorderLayout.CENTER);
						add(fpanel,BorderLayout.CENTER);
						add(btnConvert,BorderLayout.CENTER);
}


	/**
	 * Listens for button press.
	 */
	class ButtonListener implements ActionListener {
	  public void actionPerformed(ActionEvent xxx) {
		if (xxx.getSource() == btnConvert) {
			try {
				if (!cfield.getText().equals("")) {
					double c = Double.parseDouble(cfield.getText());
					ffield.setText(new Double((c*1.8)+32).toString());

				}
				else if (!ffield.getText().equals("")) {
					double f = Double.parseDouble(ffield.getText());
					cfield.setText(new Double((f-32)/1.8).toString());

				}

			}
			catch (NumberFormatException nfe) {}
		}

	  }

	} // class ButtonListener


	class FieldsListener implements KeyListener {
	    public void keyTyped(KeyEvent e) {
			if (e.getSource() == cfield) {
				ffield.setText("");

			}
			if (e.getSource() == ffield) {
				cfield.setText("");

			}

		}
	    public void keyPressed(KeyEvent e) {}
	    public void keyReleased(KeyEvent e) {}

    } // class FieldsListener


}

Ok, here is what is causing that

add(cpanel,BorderLayout.CENTER);
add(fpanel,BorderLayout.CENTER);
add(btnConvert,BorderLayout.CENTER);

You are adding all of those components to the center. BorderLayout does not stack those in sequentially like FlowLayout does. You are placing them on top of one another. If you change it to this you will see that is the case

add(cpanel,BorderLayout.NORTH);
add(fpanel,BorderLayout.CENTER);
add(btnConvert,BorderLayout.SOUTH);

The rest of that code is fine. You probably want to apply a DecimalFormat to limit the number of decimal places displayed and you could tweak the layout a bit more, but the code itself works.

This article has been dead for over six months. Start a new discussion instead.