Hello. I am trying to recreate manually a small celsius-to-fahrenheit converter that I created with Design view before that. I used its code for reference, but still doesn't work. When I run my code I don't get the components drawn on the screen, only an empty window. What am I missing, probably the type of layout?

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class CelsiusConverterCoded extends JFrame
{
    private JTextField temptext;
    private JLabel fLabel;
    private JLabel label;
    private JButton button;

    public CelsiusConverterCoded()
    {
        initComponents();
    }

    public void initComponents()
    {
        JFrame frame = new JFrame("Celsius Converter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        temptext = new JTextField("Enter degrees hmnya");
        frame.getContentPane().add(temptext);

        label = new JLabel("Celsius");
        frame.getContentPane().add(label);

        button = new JButton();
        button.setText("Convert");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                convertButtonActionPerformed(evt);
            }
        });

        fLabel = new JLabel();
        frame.getContentPane().add(fLabel);

        frame.pack();
    }

    private void convertButtonActionPerformed(ActionEvent evt)
    {
        int tempFahr = (int)((Double.parseDouble(temptext.getText()))*1.8+32);
        fLabel.setText(tempFahr+" Fahrenheit");
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new CelsiusConverterCoded().setVisible(true);
            }
        });
    }
}

Recommended Answers

All 4 Replies

The add methods of JFrame will add to the content pane, so there is no need to do things like frame.getContentPane().add(temptext) when frame.add(temptext) will do the same thing. The remove and setLayout methods also automatically work on the content pane. You only need getContentPane when you are doing something more sophisticated to that component.

All JFrames come with a java.awt.BorderLayout on the content pane. See the Javadoc for BorderLayout to learn how to use that layout correctly, because you are adding all of your components to the center of the frame, meaning that they replace each other. The last component you add is an empty label, which explains why you frame is empty. Once you learn how to use the layout, you might consider choosing a different layout with JFrame.setLayout.

Well, still nothing. I don't understand how am I supposed to use a layout. How is JPanel connected to JFrame?

package celsiusconvertercoded;

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CelsiusConverterCoded extends JFrame
{
    private JTextField temptext;
    private JLabel fLabel;
    private JLabel label;
    private JButton button;
    FlowLayout flow = new FlowLayout();

    public CelsiusConverterCoded()
    {
        initComponents();
    }

    public void initComponents()
    {
        JFrame frame = new JFrame("Celsius Converter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Celsius Converter");

        temptext = new JTextField();

        label = new JLabel();
        label.setText("Celsius");

        button = new JButton();
        button.setText("Convert");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                convertButtonActionPerformed(evt);
            }
        });

        fLabel = new JLabel();
        fLabel.setText("Fahrenheit");

        JPanel p = new JPanel();
        p.setLayout(flow);
        p.add(temptext);
        p.add(label);
        p.add(button);
        p.add(fLabel);

        frame.pack();
    }

    private void convertButtonActionPerformed(ActionEvent evt)
    {
        int tempFahr = (int)((Double.parseDouble(temptext.getText()))*1.8+32);
        fLabel.setText(tempFahr+" Fahrenheit");
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new CelsiusConverterCoded().setVisible(true);
            }
        });
    }  
}

Short version: A JPanel is a container that you can add to a JFrame to help organise and lay out fields and buttons.
In your code
1. You have two JFrames and that's confusing you. CelsiusConverterCoded extends JFrame, so your new CelsiusConverterCoded() is a JFrame, but then you create another (unneccesary) JFrame on line 28.
2. You add your items to your JPanel OK, but you don't add the JPanel to your JFrame, so that's why you don't see it.

Thanks for clarification. With some further side-help I got it to work.

package celsiusconvertercoded;

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CelsiusConverterCoded extends JFrame
{
    private JTextField temptext;
    private JLabel fLabel;
    private JLabel label;
    private JButton button;
    FlowLayout flow = new FlowLayout();

    public CelsiusConverterCoded()
    {
        initComponents();
    }

    public void initComponents()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Celsius Converter");

        temptext = new JTextField();

        label = new JLabel();
        label.setText("Celsius");

        button = new JButton();
        button.setText("Convert");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                convertButtonActionPerformed(evt);
            }
        });

        fLabel = new JLabel();
        fLabel.setText("Fahrenheit");

        JPanel p = new JPanel();
        getContentPane().add(p);
        p.setLayout(flow);
        p.add(temptext);
        p.add(label);
        p.add(button);
        p.add(fLabel);

        pack();
    }

    private void convertButtonActionPerformed(ActionEvent evt)
    {
        int tempFahr = (int)((Double.parseDouble(temptext.getText()))*1.8+32);
        fLabel.setText(tempFahr+" Fahrenheit");
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new CelsiusConverterCoded().setVisible(true);
            }
        });
    }  
}

Of course, the design is not very good but I was looking to understand the functionality.

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.