user2264202
user2264202

Reputation: 63

JLabel not showing text in GUI

so my code ask the user for input. then converts the temp. should be simple but my code is not working. It does not output my lable 3 it just does nothing. that is the only problem i am having with my code, i just dont know how to fix it

  import javax.swing.*;


   public class FahrenheitPanel extends JPanel

  {

 private JLabel lable1;
private JLabel lable2;  
private JLabel lable3;
 private JTextField fahrenheit;
   public FahrenheitPanel()

  {

  lable1 = new JLabel ("Enter Fahrenheit temperature:");

  lable2 = new JLabel ("Temperature in Celsius: ");

  fahrenheit = new JTextField (5);

  fahrenheit.addActionListener (new TempListener());
  add (lable1);
    add (fahrenheit);
    add (lable2);      


  setPreferredSize (new Dimension(300, 75));

}

private class TempListener implements ActionListener

{
  public void actionPerformed (ActionEvent event)

  {

     int fahrenheitTemp, celsiusTemp;

     String text = fahrenheit.getText();
     fahrenheitTemp = Integer.parseInt (text);

     celsiusTemp = (fahrenheitTemp-32) * 5/9;
     lable3.setText(Integer.toString (celsiusTemp));  
        add ( lable3 );                           
  }
}


   public static void main (String[] args)
  {
    JFrame frame = new JFrame ("Fahrenheit to Celsius Converter");

     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     FahrenheitPanel panel = new FahrenheitPanel();
     frame.getContentPane().add(panel);
     frame.pack();
     frame.setVisible(true);
  }
     }

Upvotes: 0

Views: 2498

Answers (3)

akki0996
akki0996

Reputation: 713

First of all, i converted the Integer value to Double becuase it may have double numbers, Second your are not adding your label in your class, this is the problem... run your program, if there is any problem , feel free to ask me

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.*;


    public class FahrenheitPanel extends JPanel

        {

            private JLabel lable1;
            private JLabel lable2;  
            private JLabel lable3;
            private JTextField fahrenheit;

    public FahrenheitPanel()

        {

            lable1 = new JLabel ("Enter Fahrenheit temperature:");

        lable2 = new JLabel ("Temperature in Celsius: ");

        lable3 = new JLabel("");
        fahrenheit = new JTextField (5);

        fahrenheit.addActionListener ((ActionListener) new TempListener());
        add (lable1);
        add (fahrenheit);
        add (lable2);      
        add(lable3);

        setPreferredSize (new Dimension(250, 75));

        }

    private class TempListener implements ActionListener

        {
            public void actionPerformed (ActionEvent event)

                {

                    double fahrenheitTemp, celsiusTemp;

                    String text = fahrenheit.getText();
                    fahrenheitTemp = Double.parseDouble (text);

                    celsiusTemp = ((fahrenheitTemp-32) * 5/9);
                    lable3.setText(Double.toString (celsiusTemp));  

                }
        }


public static void main (String[] args)
{
      JFrame frame = new JFrame ("Fahrenheit to Celsius Converter");

      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      FahrenheitPanel panel = new FahrenheitPanel();
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
}
}

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691785

The third label should be added to the frame from the start, with some default text.

If you add the label dynamically, then the container must be validated (by calling validate() on the panel).

Also, you should not set the preferred size of a panel. The layout manager computes the preferred size based on the components it contains.

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Don't add the labl3 JLabel to your GUI in the actionPerformed(...) method as doing this means you will be trying to add the JLabel as many times as the listener method is called, and will need to call revalidate and repaint unnecessarily. Instead add this JLabel to your GUI from the very beginning in the class's constructor.

Upvotes: 2

Related Questions