I had to convert one of my programs into an applet.
And most of it I copied and pasted, but the program is suppose to let the user input a number in the texfield and when they click one of the radio button options the calculate resuls are suppose to take the place of where the blue text is. i have my itemlistener code and my program compiles fine, but nothing happens when i click on the radio buttons.. Can anyone please help me figure out what I may be doing wrong?
Java file
/*
*/
/*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.DecimalFormat; // Needed for formatting decimal
public class TemperatureApplet extends JApplet implements ItemListener
{
DecimalFormat df = new DecimalFormat("#,##0.00");
private double fahrenheit, celsius, kelvin;
JRadioButton optFtoC, optFtoK, optCtoF, optCtoK, optKtoF, optKtoC, optHidden;
JTextField txtTemp;
JLabel lblTitle;
JLabel lblResults;
JLabel lblTemp;
public void init()
{
// Create JLabels
JLabel lblTitle = new JLabel("Temperature Conversion Calculator");
JLabel lblTemp = new JLabel("Temperature");
JLabel lblResults = new JLabel("Results will display here");
// Create TextFields
JTextField txtTemp = new JTextField(5);
// Create Button Group/ Radio buttons
ButtonGroup grpOperator = new ButtonGroup();
JRadioButton optFtoC = new JRadioButton("Fahrenheit to Celsius");
JRadioButton optFtoK = new JRadioButton("Fahrenheit to Kelvin");
JRadioButton optCtoF = new JRadioButton("Celsius to Fahrenheit");
JRadioButton optCtoK = new JRadioButton("Celsius to Kelvin");
JRadioButton optKtoF = new JRadioButton("Kelvin to fahrenheit");
JRadioButton optKtoC = new JRadioButton("Kelvin to Celsius");
JRadioButton optHidden = new JRadioButton("");
// Create JPanels
JPanel pnlTitle = new JPanel();
JPanel pnlTemp = new JPanel();
JPanel pnlResults= new JPanel();
pnlTemp.setLayout(new GridLayout(1,2));
pnlResults.setLayout(new GridLayout(3,2));
// Add the radiobuttons to a button group
grpOperator.add(optFtoC);
grpOperator.add(optFtoK);
grpOperator.add(optCtoF);
grpOperator.add(optCtoK);
grpOperator.add(optKtoF);
grpOperator.add(optKtoC);
grpOperator.add(optHidden);
// Add objects to the panels
pnlTitle.add (lblTitle);
pnlTemp.add (lblTemp);
pnlTemp.add (txtTemp);
pnlResults.add (optFtoC);
optFtoC.addItemListener(this);
pnlResults.add (optFtoK);
optFtoK.addItemListener(this);
pnlResults.add (optCtoF);
optCtoF.addItemListener(this);
pnlResults.add (optCtoK);
optCtoK.addItemListener(this);
pnlResults.add (optKtoF);
optKtoF.addItemListener(this);
pnlResults.add (optKtoC);
optKtoC.addItemListener(this);
pnlResults.add (lblResults);
lblResults.setForeground(Color.blue);
Container content = getContentPane();
content.setLayout(new BorderLayout());
content.add(pnlTitle,BorderLayout.NORTH);
content.add(pnlTemp, BorderLayout.CENTER);
content.add(pnlResults, BorderLayout. SOUTH);
}// End init
public void itemStateChanged(ItemEvent choice)
{
if (choice.getStateChange() == ItemEvent.SELECTED)
{
if (optFtoC.isSelected()==true)
{fToC();}
else if (optFtoK.isSelected()==true)
{fToK();}
else if (optCtoK.isSelected()==true)
{cToK();}
else if (optCtoF.isSelected()==true)
{cToF();}
else if (optKtoF.isSelected()==true)
{kToF();}
else if (optKtoC.isSelected()==true)
{kToC();}
}
}//End ItemStateChanged
//Celsius to Fahrenheit converter method
private void cToF()
{
try
{
celsius = Double.parseDouble(txtTemp.getText());
fahrenheit = (celsius * (9.0/5.0)) + 32;
lblResults.setText("Results: " + df.format(fahrenheit));
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a valid numeric number","Error",JOptionPane.ERROR_MESSAGE);
clearIt();
}
}// end cToF
//Fahrenheit to Celsius Converter method
private void fToC()
{
try
{
fahrenheit = Double.parseDouble(txtTemp.getText());
celsius = (5.0/9.0) * (fahrenheit - 32);
lblResults.setText("Results: " + df.format(celsius));
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a valid numeric number","Error",JOptionPane.ERROR_MESSAGE);
clearIt();
}
}// end fToC
// Celsius to Kelvin converter method
private void cToK()
{
try
{
celsius = Double.parseDouble(txtTemp.getText());
kelvin = celsius + 273.15;
lblResults.setText("Results: " + df.format(kelvin));
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a valid numeric number","Error",JOptionPane.ERROR_MESSAGE);
clearIt();
}
}// end cToK
// Fahrenheit to Kelvin converter method
private void fToK()
{
try
{
fahrenheit = Double.parseDouble(txtTemp.getText());
kelvin = ((5.0/9.0) * (fahrenheit - 32)) + 273.15;
lblResults.setText("Results: " + df.format(kelvin));
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a valid numeric number","Error",JOptionPane.ERROR_MESSAGE);
clearIt();
}
}// end fToK
// Kelvin to Fahrenheit converter method
private void kToF()
{
try
{
kelvin = Double.parseDouble(txtTemp.getText());
fahrenheit = ((9.0/5.0) * (kelvin - 273.15)) + 32;
lblResults.setText("Results: " + df.format(fahrenheit));
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a valid numeric number","Error",JOptionPane.ERROR_MESSAGE);
clearIt();
}
}// end kToF
// Kelvin to celsius converter method
private void kToC()
{
try
{
kelvin = Double.parseDouble(txtTemp.getText());
celsius = kelvin - 273.15;
lblResults.setText("Results: " + df.format(celsius));
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a valid numeric number","Error",JOptionPane.ERROR_MESSAGE);
clearIt();
}
}// end kToC
//Clear textfield, radiobutton selection, and results
private void clearIt()
{
txtTemp.setText(" ");
optHidden.setSelected(true);
lblResults.setText("Results will display here");
}// end clearIt
}// End class
html file
<html>
<body>
<applet code = "TemperatureApplet.class" width = "500" height = "130">
</applet>
</body>
</html>