4 Replies Latest reply: Apr 30, 2013 6:53 AM by jduprez RSS

    Label setText problem

    1003471
      Hi

      I'm preparing a project for my school subject programming in Java. I have to change value of the label and I dont know why it dont work. Here is my code. It looks like "label" object it is not seen inside actionListener.
      public class mostView extends JFrame {
      
           private JPanel myPanel;
      
           public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                     public void run() {
                          try {
                               mostView frame = new mostView();
                               frame.setVisible(true);
                          
                          } catch (Exception e) {
                               e.printStackTrace();
                          }
                     }
                });
           }
      
           /**
            * Create the frame.
            */
           public mostView() {
      
                this.setTitle("Waiting cars");
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setBounds(100, 100, 400, 200);
                this.setResizable(false);
                Container myPanel = new Container();
                myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
                setContentPane(myPanel);
                
                JButton btnAdd = new JButton("Add car");
                btnAdd.setAlignmentX(Component.CENTER_ALIGNMENT);
      
                JLabel label = new JLabel("0");
                
                myPanel.add(label);
                myPanel.add(btnAdd);
                
                          
                btnAdd.addActionListener(new ActionListener() {
                     public void actionPerformed(ActionEvent arg0) {
                          label.setText("5");              // this dont work
                          System.out.println("Click");  //  this works fine
                          
                     }
                });
           
      
           }
      I hope you know where the problem is. Thanks for any help.

      Edited by: 1000468 on 2013-04-17 06:12
        • 1. Re: Label setText problem
          PhHein
          What doesn't work, does it make your toaster explode? Post the exact errormessages/stack traces you get.
          • 2. Re: Label setText problem
            gimbal2
            you have to be more precise: it doesn't compile. When you so "it don't work" (and it is "it DOESN'T work") then people will assume you are talking about running the application. You will also want to post the actual error you get when asking for help, otherwise you are making people guess.

            The error you get is this:
            local variable label is accessed from within inner class; needs to be declared final
            You're a student, your whole world right now is learning new stuff. What is stopping you from reading into inner classes and this 'final' thing the error is referring to?

            Anyway, I would try actually making the label a member of your class, like that myPanel.
            • 3. Re: Label setText problem
              1003471
              Thank you. I've changed
               JLabel label = new JLabel("0"); 
              to
               final JLabel label = new JLabel("0"); 
              and it's working now. Yes the problem was that source code couldn't be compiled.

              I dont know why the final word is doing the thing here. And I dont know how to make the label a member of my class. Yes I'll read about the inner classes, thanks.
              • 4. Re: Label setText problem
                jduprez
                I dont know why the final word is doing the thing here.
                It's marking that the value of variable label cannot change. This value is a reference to an object, and if it could change then when the code in the inner class is executed the reference would be ambiguous.
                And I dont know how to make the label a member of my class. Yes I'll read about the inner classes, thanks.
                Hint: myPanel is a member of your class :)