So my coding is a temperature converter. I have gotten everything down the way that it is wanted by the assignment except for one part. When I run the program, put in a value and select the radio button for conversion, no output ever shows up. Was hoping that someone could take a look and see what is going on with it. Here is the coding, it is two separate classes within java.

import javax.swing.*;

/**
 * This program allows the user to convert temperatures
 * 
 */

public class ConvertTemp {
	public static void main(String[] args) {
		JFrame frame = new ConvertTempFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Temperature Converter");
		frame.setVisible(true);
	}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

@SuppressWarnings("serial")
public class ConvertTempFrame extends JFrame 
{
	/**
	 * Constructs the frame
	 */
	public ConvertTempFrame() 
	{
		//adding input panel
		JPanel inputPanel = new JPanel();
		inputPanel.add(inlabel);
		inputPanel.add(infield);
		//adding output panel
		JPanel outputPanel = new JPanel();
		outputPanel.add(outlabel);
		outputPanel.add(outfield);
		outfield.setEditable(false);//removes ability to enter a value in the output field
		add(inputPanel, BorderLayout.NORTH); //adding input panel to frame
		add(outputPanel, BorderLayout.SOUTH); //adding output panel to frame
		add(createRadioButtons(), BorderLayout.CENTER);
		//This is the listener shared among all components
		class ChoiceListener implements ActionListener 
		{
			public void actionPerformed(ActionEvent event) 
			{
				outputConvertedTemp();
			}
		}
		listener = new ChoiceListener();
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
	}
	
	/**
	 * Creates the panel for selecting converting option
	 */
	public JPanel createRadioButtons()
	{
		/**
		 * Creates radio buttons to select the conversion type
		 */
		CtoK = new JRadioButton("Celsius to Kelvin");
		CtoK.addActionListener(listener);
		FtoK = new JRadioButton("Fahrenheit to Kelvin");
		FtoK.addActionListener(listener);
		KtoF = new JRadioButton("Kelvin to Fahrenheit");
		KtoF.addActionListener(listener);
		CtoF = new JRadioButton("Celsius to Fahrenheit");
		CtoF.addActionListener(listener);
		FtoC = new JRadioButton("Fahrenheit to Celsius");
		FtoC.addActionListener(listener);
		KtoC = new JRadioButton("Kelvin to Celsius");
		KtoC.addActionListener(listener);
		
		ButtonGroup group = new ButtonGroup();
		group.add(CtoK);
		group.add(FtoK);
		group.add(KtoF);
		group.add(CtoF);
		group.add(FtoC);
		group.add(KtoC);
		
		JPanel radioButtons = new JPanel();
		radioButtons.setLayout(new GridLayout(3, 2));
		radioButtons.add(CtoK);
		radioButtons.add(FtoK);
		radioButtons.add(KtoF);
		radioButtons.add(CtoF);
		radioButtons.add(FtoC);
		radioButtons.add(KtoC);
		return radioButtons;		
	}
	
	public void outputConvertedTemp()
	{
		int temp;
		double newtemp = 0;
		String inputString;
		inputString = infield.getText();//pulls the number that was entered in the input field
		temp = Integer.parseInt(inputString);// makes a new variable to handle the input
		//tells the output how to calculate the input based on the button that was pressed
		if (CtoK.isSelected())
			newtemp = (temp + 273);//Celsius to Kelvin equation
		else if (FtoK.isSelected())
			newtemp = ((.55555) * (temp - 32) + 273);//Fahrenheit to Kelvin equation
		else if (KtoF.isSelected())
			newtemp = (((temp - 273) * (1.8)) + 32);//Kelvin to Fahrenheit equation
		else if (CtoF.isSelected())
			newtemp = (((1.8) * temp) + 32);//Celsius to Fahrenheit equation
		else if (FtoC.isSelected())
			newtemp = ((.55555) * (temp - 32));//Fahrenheit to Celsius equation
		else if (KtoC.isSelected())
			newtemp = (temp - 273);//Kelvin to Celsius equation
		//sets newtemp value to output field
		outfield.setText(" "+ three.format(newtemp));
	}
	DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output
	private ActionListener listener;
	private JTextField infield = new JTextField(10);
	private JTextField outfield = new JTextField(10);
	private JLabel inlabel = new JLabel("Temperature Value:");
	private JLabel outlabel = new JLabel("Converted Temperature Value:");
	private static final int FRAME_WIDTH = 400;
	private static final int FRAME_HEIGHT = 200;
	private JRadioButton CtoK;
	private JRadioButton FtoK;
	private JRadioButton KtoF;
	private JRadioButton CtoF;
	private JRadioButton FtoC;
	private JRadioButton KtoC;
}

I think it might have to do with the ActionListener but I am not sure. Thanks in advance.

So after going back over this, I realized that I had made a mistake in the instructions. There needs to be and input radio button selected and an output radio button selected. After correcting this error, everything worked fine. Here is the coding for the first class:

import javax.swing.*;
public class ConvertTemp 
{
	public static void main(String[] args) 
	{
		//creates main box panel for everything to go in
		JFrame frame = new ConvertTempFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Temperature Converter");
		frame.setVisible(true);
	}
}

And here is the coding for the second class:

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

public class ConvertTempFrame extends JFrame 
{
	private ActionListener earpiece;
	private JTextField inputbox = new JTextField(10);
	private JTextField outputbox = new JTextField(10);
	private JRadioButton CelsiusIn;
	private JRadioButton FahrenheitIn;
	private JRadioButton KelvinIn;
	private JRadioButton CelsiusOut;
	private JRadioButton FahrenheitOut;
	private JRadioButton KelvinOut;
	private JLabel inputTitle = new JLabel("Temp Value:");
	private JLabel outputTitle = new JLabel("Converted Temp Value:");
	DecimalFormat three = new DecimalFormat("0.000");
	
	public ConvertTempFrame()
		{
		//creates 2 panels for border with the input and the output fields
		JPanel inputPanel = new JPanel();
		inputPanel.add(inputTitle);
		inputPanel.add(inputbox);
		JPanel outputPanel = new JPanel();
		outputPanel.add(outputTitle);
		outputPanel.add(outputbox);
		outputbox.setEditable(false);
		add(inputPanel, BorderLayout.NORTH); 
		add(outputPanel, BorderLayout.SOUTH);
		class ChoiceListener implements ActionListener 
		{
			public void actionPerformed(ActionEvent event) 
			{
				outputConvertedTemp();//performed when a button is selected
			}
		}
		earpiece = new ChoiceListener();
		createConversionTable();
		setSize(400, 200);
	}
	public void createConversionTable()
	{
		//creates center panel to hold radio button panels
		JPanel leftPanel = RadioButtonInput(); 
		JPanel rightPanel = RadioButtonOutput();
		JPanel conversionTable = new JPanel();
		conversionTable.setLayout(new GridLayout(1, 2));
		conversionTable.add(leftPanel);
		conversionTable.add(rightPanel);
		add(conversionTable, BorderLayout.CENTER);
	}
	public JPanel RadioButtonInput()
	{
		//creates the input radio buttons, groups them, aligns them, puts a border on, returns the panel
		CelsiusIn = new JRadioButton("Celsius");
		CelsiusIn.addActionListener(earpiece);
		FahrenheitIn = new JRadioButton("Fahrenheit");
		FahrenheitIn.addActionListener(earpiece);
		KelvinIn = new JRadioButton("Kelvin");
		KelvinIn.addActionListener(earpiece);	
		ButtonGroup group = new ButtonGroup();
		group.add(CelsiusIn);
		group.add(FahrenheitIn);
		group.add(KelvinIn);
		JPanel radioButtonsInput = new JPanel();
		radioButtonsInput.setLayout(new GridLayout(3, 1));
		radioButtonsInput.add(CelsiusIn);
		radioButtonsInput.add(FahrenheitIn);
		radioButtonsInput.add(KelvinIn);
		radioButtonsInput.setBorder(
				new TitledBorder(new EtchedBorder(), "Degree Input"));
		return radioButtonsInput;		
	}
	public JPanel RadioButtonOutput()
	{
		//creates the output radio buttons, groups them, aligns them, puts a border on, returns the panel
		CelsiusOut = new JRadioButton("Celsius");
		CelsiusOut.addActionListener(earpiece);
		FahrenheitOut = new JRadioButton("Fahrenheit");
		FahrenheitOut.addActionListener(earpiece);
		KelvinOut = new JRadioButton("Kelvin");
		KelvinOut.addActionListener(earpiece);
		ButtonGroup group2 = new ButtonGroup();
		group2.add(CelsiusOut);
		group2.add(FahrenheitOut);
		group2.add(KelvinOut);
		JPanel radioButtonsOutput = new JPanel();
		radioButtonsOutput.setLayout(new GridLayout(3, 1));
		radioButtonsOutput.add(CelsiusOut);
		radioButtonsOutput.add(FahrenheitOut);
		radioButtonsOutput.add(KelvinOut);
		radioButtonsOutput.setBorder(
				new TitledBorder(new EtchedBorder(), "Degree Output"));
		return radioButtonsOutput;		
	}
	
	
	public void outputConvertedTemp()
	{
		//reads buttons selected to determine output
		int temp;
		double newtemp = 0;
		temp = Integer.parseInt(inputbox.getText());
		if (CelsiusIn.isSelected() & KelvinOut.isSelected())
			newtemp = (temp + 273);//c to k
		else if (FahrenheitIn.isSelected() & KelvinOut.isSelected())
			newtemp = ((.55555) * (temp - 32) + 273);//f to k
		else if (KelvinIn.isSelected() & FahrenheitOut.isSelected())
			newtemp = (((temp - 273) * (1.8)) + 32);//k to f
		else if (CelsiusIn.isSelected() & FahrenheitOut.isSelected())
			newtemp = (((1.8) * temp) + 32);//c to f
		else if (FahrenheitIn.isSelected() & CelsiusOut.isSelected())
			newtemp = ((.55555) * (temp - 32));//f to c
		else if (KelvinIn.isSelected() & CelsiusOut.isSelected())
			newtemp = (temp - 273);//k to c
		//the following are to ensure that the output stays what it should be
		else if (CelsiusIn.isSelected() & CelsiusOut.isSelected())
			newtemp = temp;
		else if (FahrenheitIn.isSelected() & FahrenheitOut.isSelected())
			newtemp = temp;
		else if (KelvinIn.isSelected() & KelvinOut.isSelected())
			newtemp = temp;
		outputbox.setText(" "+ three.format(newtemp));
	}	
}
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.