dispalying the values through labels

Could you please help me sort the problem out ?

I try to input the numbers into text box and display them in the output labels.

The program compiles but it does calculate the values. It does not give the error message, either.

Here is the code below:

// Capture actions

public void actionPerformed(ActionEvent event)

{

// If 'Convert Tempurature' button pushed perform action

String action = event.getActionCommand();//detect mouse event

if ( action.equals( "Convert Temperature" ) ){

int selectedTempIndex = cboTemps.getSelectedIndex();

//String selectedTempType = (String)cboTemps.getSelectedItem();

try {

String xStr = xInput.getText();

double x = Double.parseDouble(xStr);

}

catch (NumberFormatException e) {

// The string xStr is not a legal number.

lblFahrenheit.setText(" Illegal data");

return;

}

// Calculate Base Tempurature

switch( selectedTempIndex ){

case 0:

fahr = (1.8 * x) + 32;

//lblFahrenheit.Text = "fahr";

lbl1.setText("Fahrenheit conversion = " + (fahr));

case 1:

cel = x - 276.16;

//lblCelsius.Text = cel;

lbl2.setText("Celsius conversion = " + (cel));

case 2:

kelv = x + 273.16;

//lblKelvin.Text = kelv;

lbl3.setText("Kelvin conversion = " + (kelv));

case 3:

rank = 1.8 * x;

//lblRankine.Text = rank;

lbl4.setText("Rankine conversion = " + (rank));

case 4:

reaum = 0.8 * x;

//lblReaumer.Text = reaum;

lbl5.setText("Reaumer conversion = " + (reaum));

}

}

}

[1670 byte] By [eastgatea] at [2007-10-3 8:53:25]
# 1
Have you tried the suggestions you were given?Also please remember to use code tags.
zadoka at 2007-7-15 4:03:15 > top of Java-index,Desktop,Core GUI APIs...
# 2
1) Each case block in a switch needs to end with a break statment or control will drop through to the next case.2) Have you actually associated the object containing the actionPerrformed() method with the JButton with addActionListener()?
malcolmmca at 2007-7-15 4:03:15 > top of Java-index,Desktop,Core GUI APIs...
# 3

I just added the break statement after each case block but it still doesnot work.

I have added the ActionListener to the button before.

//create the button

btn = new JButton("Convert Temperature");

btn.addActionListener(this);

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new BorderLayout());

// Change panel color

buttonPanel.setBackground(Color.green);

// Add Button button Panel

buttonPanel.add(btn, BorderLayout.CENTER);

.

eastgatea at 2007-7-15 4:03:15 > top of Java-index,Desktop,Core GUI APIs...
# 4
I just tried the suggestions -break statement and the action listener method to the button but it stilll does not work
eastgatea at 2007-7-15 4:03:15 > top of Java-index,Desktop,Core GUI APIs...
# 5
Is the label being set correctly and not being displayed? Have you put any printlns in the switch to make sure it doing what it should?
zadoka at 2007-7-15 4:03:15 > top of Java-index,Desktop,Core GUI APIs...
# 6

I have set up the tabels properly. The message is only displayed when the sring is entered. It seems that the button does not do any calculations.

Here is the code below.

/*

In this applet, the user can type one number. The

user can click on button labled Convert Temperature, and / to perform

basic arithmetic operations on the numbers. When the user

clicks on a button the answer is displayed. The applet

should be about 400 by 300 pixels.

*/

import java.applet.*;// Defines classes to create an applet

import java.awt.*; // Defines basic classes for GUI programming.

import java.awt.event.*;// Defines classes for working with events.

import javax.swing.*;// Defines the Swing GUI classes.

import javax.swing.event.*; // Defines classes for working with events

/**

* Description of Temperature applet

*/

public class TemperatureApplet extends JApplet implements ActionListener

{

private double x;

private double cel;

private double fahr;

private double kelv;

private double rank;

private double reaum;

private JTextField xInput;

public Container container;

private JButton btn;

private JComboBox cboTemps;

private JPanel topPanel;

private JPanel Fahrenheitpanel;

private JPanel Celsiuspanel;

private JPanel Kelvinpanel;

private JPanel Rankinepanel;

private JPanel Reaumerpanel;

private JPanel buttonPanel;

private JLabel lblFahrenheit;

private JLabel lblCelsius;

private JLabel lblKelvin;

private JLabel lblRankine;

private JLabel lblReaumer;

private JLabel lbl1;

private JLabel lbl2;

private JLabel lbl3;

private JLabel lbl4;

private JLabel lbl5;

// method Initializing the applet the applet

public void init()

{

// Add the components to container

// Get content pane

Container content = getContentPane();

/* Assign a background color to the applet and its

content panel. This color will show through between

components and around the edges of the applet. */

content.setBackground(Color.blue);

content.setForeground(Color.yellow);

// Setup Tempurature List

String[] TempsStrings = { "Fahrenheit", "Celsius", "Kelvin",

"Rankine", "Reaumer" };

// Create and Populate ComboBox

cboTemps = new JComboBox( TempsStrings );

// Create TextBox

xInput = new JTextField("0");

// Set TextBox Color

xInput.setBackground(Color.white);

// Create labels displaying the values after conversion

JLabel lbl1 = new JLabel("Fahrenheit:");

JLabel lbl2 = new JLabel("Celsius:");

JLabel lbl3 = new JLabel("Kelvin:");

JLabel lbl4 = new JLabel("Rankine:");

JLabel lbl5 = new JLabel("Reaumer:");

// Create tempurature result labels

lblFahrenheit = new JLabel();

lblCelsius = new JLabel();

lblKelvin = new JLabel();

lblRankine = new JLabel();

lblReaumer = new JLabel();

// Create panel to hold the input box and combo box.

JPanel topPanel = new JPanel();

// set up the layout of the panel

topPanel.setLayout(new BorderLayout());

//add th combo box to the panel

topPanel.add(cboTemps, BorderLayout.WEST);

//add the input box to the panel

topPanel.add(xInput, BorderLayout.CENTER);

// Change panel color

topPanel.setBackground(Color.green);

/* Create Fahrenheit panel to hold the labels; one dispalaying the static

label and the other one dispalying the value */

JPanel Fahrenheitpanel = new JPanel();

Fahrenheitpanel.setLayout(new BorderLayout());

Fahrenheitpanel.add(lbl1, BorderLayout.WEST);

Fahrenheitpanel.add(lblFahrenheit, BorderLayout.CENTER);

// Create Celsius panel

JPanel Celsiuspanel = new JPanel();

Celsiuspanel.setLayout(new BorderLayout());

Celsiuspanel.add(lbl2, BorderLayout.WEST);

Celsiuspanel.add(lblCelsius, BorderLayout.CENTER);

// Create Kelvin panel

JPanel Kelvinpanel = new JPanel();

Kelvinpanel.setLayout(new BorderLayout());

Kelvinpanel.add(lbl3, BorderLayout.WEST);

Kelvinpanel.add(lblKelvin, BorderLayout.CENTER);

// CreateRankine panel

JPanel Rankinepanel = new JPanel();

Rankinepanel.setLayout(new BorderLayout());

Rankinepanel.add(lbl4, BorderLayout.WEST);

Rankinepanel.add(lblRankine, BorderLayout.CENTER);

// CreateReaumer panel

JPanel Reaumerpanel = new JPanel();

Reaumerpanel.setLayout(new BorderLayout());

Reaumerpanel.add(lbl5, BorderLayout.WEST);

Reaumerpanel.add(lblReaumer, BorderLayout.CENTER);

//create the button

btn = new JButton("Convert Temperature");

btn.addActionListener(this);

// Createbutton panel

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new BorderLayout());

// Change panel color

buttonPanel.setBackground(Color.green);

// Add Button button Panel

buttonPanel.add(btn, BorderLayout.CENTER);

/* Set up the layout for the applet, using a GridLayout,

and add all seven panels that have been created. */

content.setLayout(new GridLayout(7,1,2,2));

content.add(topPanel);

content.add(Fahrenheitpanel);

content.add(Celsiuspanel);

content.add(Kelvinpanel);

content.add(Rankinepanel);

content.add(Reaumerpanel);

content.add(buttonPanel);

}

// Capture actions

public void actionPerformed(ActionEvent event)

{

try {

String xStr = xInput.getText();

double x = Double.parseDouble(xStr);

}

catch (NumberFormatException e) {

// The string xStr is not a legal number.

lblFahrenheit.setText(" Illegal data");

return;

}

// If 'Convert Tempurature' button pushed perform action

String action = event.getActionCommand();//detect mouse event

if ( action.equals( "Convert Temperature" ) ){

int selectedTempIndex = cboTemps.getSelectedIndex();

//String selectedTempType = (String)cboTemps.getSelectedItem();

// Calculate Base Tempurature

switch( selectedTempIndex ){

case 0:

fahr = (1.8 * x) + 32;

//lblFahrenheit.Text = "fahr";

lbl1.setText("Fahrenheit conversion = " + (fahr));

break;

case 1:

cel = x - 276.16;

//lblCelsius.Text = cel;

lbl2.setText("Celsius conversion = " + (cel));

break;

case 2:

kelv = x + 273.16;

//lblKelvin.Text = kelv;

lbl3.setText("Kelvin conversion = " + (kelv));

break;

case 3:

rank = 1.8 * x;

//lblRankine.Text = rank;

lbl4.setText("Rankine conversion = " + (rank));

break;

case 4:

reaum = 0.8 * x;

//lblReaumer.Text = reaum;

lbl5.setText("Reaumer conversion = " + (reaum));

break;

}

}

}

}

eastgatea at 2007-7-15 4:03:15 > top of Java-index,Desktop,Core GUI APIs...