Hi everyone,

I'm trying to write a code that converts the number that a user inputs to either Celsius or Fahrenheit simply by pressing a button. So far I was able to compile and run the program displaying the panel, buttons, and the text field but I'm not sure how to write the code that gets the input and write the code to do the calculation and display it n the text field. Help me please.

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

public class FtoCConversionTest{
	public static void main(String[] args){
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				Frame frame = new Frame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

class Frame extends JFrame
{
	public Frame()
	{
		setTitle("Fehrenheit to Celsius converter");
		Panel panel = new Panel();
	    add(panel);
		pack();
	}
}
	
class Panel extends JPanel
{
	public Panel()
	{
		
		setLayout(new BorderLayout());

		//add text field and locate it
		textField = new JTextField();
                add(textField, BorderLayout.CENTER);
		
		//add JPanel
		panel = new JPanel();
		panel.setLayout(new GridLayout(1,2));

		//add buttons and locate them
		addButton("Convert to Celsius", command);
		addButton("Convert to Fehrenheit", command );
		add(panel, BorderLayout.SOUTH);
	}
	
	private void addButton(String lable, ActionListener listener){
		
		JButton button = new JButton(lable);
		button.addActionListener(listener);
		panel.add(button);
	}
    private JTextField textField;   
    private JPanel panel;
    private ActionListener command;
}

Recommended Answers

All 7 Replies

Frame and Panel are Java classes. You may want to rename your classes to make it less confusing, or at least be careful and make sure you don't get them mixed up.

What do you want to have done when the button is pressed? In other words, how are you going to display the output? Pop-up dialog box? Or you could have some JLabel for the answer. Anway, set up two static functions. One that converts Celsius to Fahrenheit and one that does the opposite. When the button is pressed, grab the input from the JTextField, covert it to a double, then call the appropriate conversion function, which will return the right answer, convert it to text, and display it, however you decide to display it.

Frame and Panel are Java classes. You may want to rename your classes to make it less confusing, or at least be careful and make sure you don't get them mixed up.

What do you want to have done when the button is pressed? In other words, how are you going to display the output? Pop-up dialog box? Or you could have some JLabel for the answer. Anway, set up two static functions. One that converts Celsius to Fahrenheit and one that does the opposite. When the button is pressed, grab the input from the JTextField, covert it to a double, then call the appropriate conversion function, which will return the right answer, convert it to text, and display it, however you decide to display it.

What I'm trying to do is when I type a number in the text field and press one of the buttons, it should display the result in the text box; no pop-up display box.

What I'm trying to do is when I type a number in the text field and press one of the buttons, it should display the result in the text box; no pop-up display box.

Then you need another JTextField, unless you are using the same JTextField for both the answer and the question (which is fine). Right now you only have one JTextField. So decide where the input comes from and where the output goes. If it's the same JTextField for both, do that. Otherwise, create a second JTextField. When the button is pressed, grab the text from the JTextField that has the input, convert it to a double, convert that double to the other temperature, then convert that converted temperature from a double to a string and stick it in whatever JTextField is supposed to contain the answer.

Then you need another JTextField, unless you are using the same JTextField for both the answer and the question (which is fine). Right now you only have one JTextField. So decide where the input comes from and where the output goes. If it's the same JTextField for both, do that. Otherwise, create a second JTextField. When the button is pressed, grab the text from the JTextField that has the input, convert it to a double, convert that double to the other temperature, then convert that converted temperature from a double to a string and stick it in whatever JTextField is supposed to contain the answer.

I apologize if I wasn't clear.
Yes, I'm using the same textField for both the input and output. But how will the ActionListener know which conversion I need when I press on one of the buttons?

I apologize if I wasn't clear.
Yes, I'm using the same textField for both the input and output. But how will the ActionListener know which conversion I need when I press on one of the buttons?

Right now you don't have your Listener(s) set up to do anything. You need to either have:

  1. One listener for both buttons. Within that listener, use getSource() to figure out which button triggered the event. Act accordingly.
  2. Two separate listeners, one for each button. No need for getSource(). The only time that ActionListener will be called is when that particular button is pressed. Call the appropriate function from within that ActionListener.

Java has some good tutorials. I sometimes think they make them a little overly-complex for beginners, so it takes a while to figure out what to focus on in the code. The example below is ONE way to handle two buttons, each of which do different things.

http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html


http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html

Right now you don't have your Listener(s) set up to do anything. You need to either have:

  1. One listener for both buttons. Within that listener, use getSource() to figure out which button triggered the event. Act accordingly.
  2. Two separate listeners, one for each button. No need for getSource(). The only time that ActionListener will be called is when that particular button is pressed. Call the appropriate function from within that ActionListener.

Java has some good tutorials. I sometimes think they make them a little overly-complex for beginners, so it takes a while to figure out what to focus on in the code. The example below is ONE way to handle two buttons, each of which do different things.

http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html


http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html

Thank you for you time:)

For those who need to use my code, this is my working program:

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

public class FtoCConversionTest{
	public static void main(String[] args){
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				ConvertorFrame frame = new ConvertorFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

class ConvertorFrame extends JFrame{
	
	public ConvertorFrame(){
		setTitle("Fahrenheit to Celsius Calculator");
		ConvertorPanel panel = new ConvertorPanel();
	    add(panel);
		pack();
	}
}
	
class ConvertorPanel extends JPanel{
	
	public ConvertorPanel(){
		
		setLayout(new BorderLayout());
		//add the panel
		panel = new JPanel();
		
		//add JTextField and position it
		textField = new JTextField();
	    JPanel northPanel = new JPanel();
	    northPanel.setLayout(new GridLayout(2,1));
	    //add the text field label and position it
	    northPanel.add(new JLabel("Type the number to convert below: ", SwingConstants.LEFT));
        northPanel.add(textField);
	    add(northPanel, BorderLayout.NORTH);
		
		//set the panel and position it
		panel.setLayout(new GridLayout(1,2));
		//add buttons and position them
		cButton = new JButton("Convert to Celsius");
		panel.add(cButton);
		fButton = new JButton("Convert to Fahrenheit");
		panel.add(fButton);
		add(panel, BorderLayout.SOUTH);
		
		Converter  converter = new Converter();
		cButton.addActionListener(converter);
		fButton.addActionListener(converter);
		
	}
	private class Converter implements ActionListener{
		
		public void actionPerformed (ActionEvent event){
			
			if (event.getSource() == cButton){
				int c = (int)(((Double.parseDouble(textField.getText()))- 32) * 0.56);
	            textField.setText(c + " °C");
	            textField.requestFocus();
	            textField.selectAll();
			}
			else if(event.getSource() == fButton){
    			int f = (int)(((Double.parseDouble(textField.getText()))* 1.8) + 32);
            	textField.setText(f + " ºF");
            	textField.requestFocus();
            	textField.selectAll();
			}
		}
	}
	private JButton cButton, fButton;
	private JTextField textField;
	private JPanel panel;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.