I have the following GUI code but my JLabel for results doesn't work when either of the buttons are pushed

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

public class TempConverterPanel extends JPanel
{

   private JLabel input, results;
   private JButton output_f, output_c;
   private JTextField temp, fahrenheit, celsius;

   public TempConverterPanel()
   {
		setLayout (new GridLayout (3, 1));
		setLayout (new FlowLayout ());
		setPreferredSize (new Dimension(400, 175));
      setBackground (Color.red);


	//asks for the temp input in fahrenheit

      input = new JLabel ("Enter the temperature you want to convert:");
       	   output_f = new JButton ("Convert to Celsius: ");
	output_c = new JButton ("Convert to Fahrenheit: ");
  results = new JLabel ("Input converted to is: ");
                
                temp = new JTextField (6);
		output_f.addActionListener (new FahrenheitListener());
		output_c.addActionListener (new CelsiusListener());

              
      add (input);
      add (temp);
      add (output_f);
		add (output_c);
      add (results);

 
   }


  //sets the listener for the fahrenheit temp

   private class FahrenheitListener implements ActionListener
   {

    public void actionPerformed (ActionEvent event)
      {
         double fahrenheitTemp, celsiusTemp;

         String text = temp.getText();

         fahrenheitTemp = Double.parseDouble(text);
         celsiusTemp = (fahrenheitTemp - 32) * 5/9;

         results.setText (Double.toString (celsiusTemp));

      }
   }


//sets the listener for the celsius temp

   private class CelsiusListener implements ActionListener
   {

    public void actionPerformed (ActionEvent event)
      {
         double celsiusTemp, fahrenheitTemp;

         String text = temp.getText();

         celsiusTemp = Double.parseDouble (text);
         fahrenheitTemp = celsiusTemp *  9/5 + 32;

         results.setText (Double.toString (fahrenheitTemp));

      }
   }
}

is there anyway i can fix this problem

Recommended Answers

All 8 Replies

I tried your code by running it (with a few changes like extending JFrame instead of JPanel, adding a main to make it run as a stand alone), and it worked.
Its converting the temperature correctly when I click on the buttons also.

So whats the problem ????

the jlabel displays for results but when i push the buttons the results label disappears.....

It works just fine for me as well. The only "disappearing" I could see is that the label will move depending on the length of the value displayed in the "result". That is because you used FlowLayout.

this is what my main looks like

import javax.swing.JFrame;

public class Temp2
{
  
  public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Temp2");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      TempConverterPanel panel = new TempConverterPanel();

      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
   }
}

And?

i get the same thing when i run my main program the results label is there but as soon as i hit the converter buttons the label disppears and the only thing left is the converted number......

Well of course it does. You set the text to that number. If you want that label to remain the same then use a separate label for the converted value.

commented: Oh dear never could have imagined that was his problem +3

thank oyu i fixed it it works the way its suppose to, i appreciate everyones help.

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.