1

I'm trying to create a Swing GUI in Eclipse by following the guide in Oracle's documentation. However, they use Netbeans in the tutorial and I believe that might be the cause of the problem.

I've tried to create a temperature converter but the label for the fahrenheit number is repeatedly complaining that it cannot be resolved. Here is the source code:

private JPanel contentPane;
private JTextField tempTextField; // Not sure if I need the other components here as well?
                                  // Could be needed in order to be used in the 
                                  // actionPerformed method.

...

JButton convertButton = new JButton("Convert");
    convertButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            //Parse degrees Celsius as a double and convert to Fahrenheit.
            int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
                    * 1.8 + 32);
            fahrenheitLabel.setText(tempFahr + " Fahrenheit"); // fahrenheitLabel is what's 
        }                                                     //highlighted in Eclipse.
    });
    convertButton.setBounds(6, 38, 134, 29);
    contentPane.add(convertButton);

    JLabel fahrenheitLabel = new JLabel("Fahrenheit");
    fahrenheitLabel.setBounds(152, 43, 78, 16);
    contentPane.add(fahrenheitLabel);

The link to the Oracle documentation is here: http://docs.oracle.com/javase/tutorial/uiswing/learn/creatinggui.html

I should point out the error persists even when fahrenheitLabel is declared prior to the method - so it definitely can't be the ordering of the code that matters (I think).

If it's anything to do with the components having incorrect names I would be surprised, but here is a screenshot:

enter image description here

Many thanks to anyone who can give suggestions on the matter!

EDIT: Thank you to those who have already given suggestions to fiddle with the ordering of the code. However, it then tells me I need to make the fahrenheitLabel final, which I would have thought conflicts with the ability to dynamically change upon the user entering different inputs.

EDIT 2: Apparently making the variable final is valid. Thank you to all for your suggestions.

Jon Heller
  • 34,999
  • 6
  • 74
  • 132
Daniel Soutar
  • 827
  • 1
  • 10
  • 24
  • 2
    Are you sure it's not working even if you declare the JLabel before the actionPerformed method? - It obviously won't work with this code, because you declare `fahrenheitLabel` after you're trying to set the text of it. – Lukas Rotter Sep 10 '15 at 12:38
  • I did, but it then complains "Cannot refer to the non-final local variable fahrenheitLabel defined in an enclosing scope". Suggests to make the variable final, but I'm not sure if that's the intention. – Daniel Soutar Sep 10 '15 at 12:44
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 10 '15 at 12:49
  • 1
    `fahrenheitLabel.setBounds(152, 43, 78, 16);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 10 '15 at 12:52
  • I appreciate the comments Andrew, but why would the fahrenheitLabel need to be declared as final? – Daniel Soutar Sep 10 '15 at 12:58
  • with the convert button u can change the text of JLabel. but use static type of the variable then it will work try this or shall i give u demo for the same? – Kishan Bheemajiyani Sep 10 '15 at 15:07

2 Answers2

1

Of course it doesnt know the label, because you declare it after the method. Try putting

JLabel fahrenheitLabel = new JLabel("Fahrenheit");

before

convertButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        //Parse degrees Celsius as a double and convert to Fahrenheit.
        int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
                * 1.8 + 32);
        fahrenheitLabel.setText(tempFahr + " Fahrenheit"); // fahrenheitLabel is what's 
    }                                                     //highlighted in Eclipse.
});
Maik Fruhner
  • 300
  • 4
  • 14
  • Appreciate the suggestion! However it then tells me I need to make fahrenheitLabel final. I'm not sure if that's what I'm supposed to do? – Daniel Soutar Sep 10 '15 at 12:46
  • Adding `final` just means you can never afterwards write `fahrenheitLabel = ...`, which is perfectly fine. – Manu Sep 10 '15 at 13:06
0
public class SimpleDemo extends JFrame {

    JLabel jLabel = new JLabel("Changing label");
    JButton button = new JButton("Click");
    static int i = 0;



    public SimpleDemo() {
            // TODO Auto-generated constructor stub
            this.setSize(500, 500);

            this.add(button);
            this.add(jLabel);
            this.setLayout(new FlowLayout(2, 2, 2));
            this.setVisible(true);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);

            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    // TODO Auto-generated method stub
                    jLabel.setText("Changed Text" + i++);

                }
            });
        }

        public static void main(String args[]) {
            System.out.println("Woring fine********");
            SimpleDemo demo = new SimpleDemo();

        }
    }

Check this one might be useful to u.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68