Hey

Seem to be having a problem updating a JLabel updateScore() and don't seem to know why.
this is effecting my keeping score. I know it something small but for life of me can't figure it out.

Other JLabels have been up dated (see attachment for full programe)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.event.* ;
import java.net.*;
import java.io.*;
import java.awt.Color;
import java.util.Random;


/**
 *
 * @author Stephen
 */
public class NumberGame extends JFrame
{
     JLabel sumQ,picture,score,life;
     JPanel picpan,mainpan,playerpan;
     protected int total, answeertotal,lives = 5,scores;
     JTextField answeer; 
     private Container c;
    
     MainPanel correction = new MainPanel();
     ButtonHandler handler = new ButtonHandler();
      
     
    NumberGame()
    {
        c = getContentPane();
	c.setBackground(Color.white);
	setSize(300,500);
	setLocation(250,100);
        c.setVisible(true);
        c.setBackground(Color.PINK);
        
        PicPanel pic = new PicPanel();
        JPanel picpan = new JPanel(); 
        picpan.add(pic);
 
        MainPanel pane = new MainPanel();
        JPanel mainpan = new JPanel();
        mainpan.add(pane);
        
        player status = new player();
        JPanel playerpan = new JPanel();
        playerpan.add(status);
     
        
        c.add(picpan,BorderLayout.NORTH);
        c.add(mainpan,BorderLayout.CENTER);  
        c.add(playerpan,BorderLayout.SOUTH);
    }
    
    public class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            
            player update = new player();
            answeertotal = Integer.parseInt(answeer.getText( ));
            
            if(answeertotal == total)
            {
                JOptionPane.showMessageDialog(null, "Correct","",JOptionPane.INFORMATION_MESSAGE);
                correction.reset();
                update.updateScore();
                
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Sorry your wrong" + answeertotal + "   "+ total ,"",JOptionPane.ERROR_MESSAGE);
                update.updatelife();
            }
        }
    }
 
    class player extends JPanel
    {
        String lif,sco;
        
        player()
        {
            int2str();
            life = new JLabel(lif);
            add(life);
            score = new JLabel(sco);
            add(score);
        }
        
        public void int2str()
        {
            lif = String.valueOf("Current lives : " + lives);
            sco = String.valueOf("Current score : " + scores);
        }
        
        public  void updateScore()
        {
            scores = scores + 10;
            int2str();
            score.setText(sco);
        }
        
        public void updatelife()
        {
            lives = lives - 1;
            int2str();
            life.setText(lif);
        }
    
    }
    
        

    public static void main(String args[])
	{
		JFrame frame = new NumberGame();
		frame.setVisible(true);
		frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

Thanks alot to anyone who takes time to have a look.

Attachments
package NumberGame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.event.* ;
import java.net.*;
import java.io.*;
import java.awt.Color;
import java.util.Random;



/**
 *
 * @author Stephen
 */
public class NumberGame extends JFrame
{
     JLabel sumQ,picture,score,life;
     JPanel picpan,mainpan,playerpan;
     protected int total, answeertotal,lives = 5,scores;
     JTextField answeer; 
     private Container c;
    
     MainPanel correction = new MainPanel();
     ButtonHandler handler = new ButtonHandler();
      
     
    NumberGame()
    {
        c = getContentPane();
	c.setBackground(Color.white);
	setSize(300,500);
	setLocation(250,100);
        c.setVisible(true);
        c.setBackground(Color.PINK);
        
        PicPanel pic = new PicPanel();
        JPanel picpan = new JPanel(); 
        picpan.add(pic);
 
        MainPanel pane = new MainPanel();
        JPanel mainpan = new JPanel();
        mainpan.add(pane);
        
        player status = new player();
        JPanel playerpan = new JPanel();
        playerpan.add(status);
     
        
        c.add(picpan,BorderLayout.NORTH);
        c.add(mainpan,BorderLayout.CENTER);  
        c.add(playerpan,BorderLayout.SOUTH);
    }
    
    public class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            
            player update = new player();
            answeertotal = Integer.parseInt(answeer.getText( ));
            
            if(answeertotal == total)
            {
                JOptionPane.showMessageDialog(null, "Correct","",JOptionPane.INFORMATION_MESSAGE);
                correction.reset();
                update.updateScore();
                
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Sorry your wrong" + answeertotal + "   "+ total ,"",JOptionPane.ERROR_MESSAGE);
                update.updatelife();
            }
        }
    }
 
    class player extends JPanel
    {
        String lif,sco;
        
        player()
        {
            int2str();
            life = new JLabel(lif);
            add(life);
            score = new JLabel(sco);
            add(score);
        }
        
        public void int2str()
        {
            lif = String.valueOf("Current lives : " + lives);
            sco = String.valueOf("Current score : " + scores);
        }
        
        public  void updateScore()
        {
            scores = scores + 10;
            int2str();
            score.setText(sco);
        }
        
        public void updatelife()
        {
            lives = lives - 1;
            int2str();
            life.setText(lif);
        }
    
    }
    class PicPanel extends JPanel
    {
       
        public PicPanel()
        {
            picture = new JLabel(createImageIcon("sun.jpg"));
            add(picture);
        }
        
        
        protected  ImageIcon createImageIcon(String path) 
        {
        java.net.URL imgURL = NumberGame.class.getResource(path);
    
            if (imgURL != null) 
            {
                return new ImageIcon(imgURL);
            } 
            else 
            {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    }
    
    class MainPanel extends JPanel
    {
        int num1,num2;
        String join1,join2,prompt;
        
        MainPanel()
        {
            
            random();
            createsum();
            sumQ = new JLabel(prompt);
            add(sumQ);
            answeer = new JTextField(10);
            answeer.setEditable(true);
            add(answeer);
            compute();
            answeer.addActionListener(handler);
        }
        
        public void random()
        {
            Random generator = new Random();
            num1 = generator.nextInt(99) + 1;
            num2 = generator.nextInt(99) + 1;
            join1 = String.valueOf(num1);
            join2 = String.valueOf(num2);
        }
        
        public void createsum()
        {
            prompt = "The sum is : ";
            prompt = prompt.concat(join1);
            prompt = prompt.concat(" + ");
            prompt = prompt.concat(join2);
       }
        
        public void compute()
        {
            total = num1 + num2;
        }
        
        public void reset()
        {
            random();
            createsum();
            sumQ.setText(prompt);
            compute();
        }
    } 

    public static void main(String args[])
	{
		JFrame frame = new NumberGame();
		frame.setVisible(true);
		frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
sun1.jpg 54.19 KB

Well, you don't say what labels you are having trouble updating, but some things really stand out as a problem. In your constructor

JPanel playerpan = new JPanel();

You're defining this as a local variable - not initializing your class level "playerpan" variable. Same with the other panels. This means that you essentially have no reference to those panels outside the constructor. Aside from that, why does player extend JPanel but refer to components in the enclosing class? The only variables in "player" are two strings. This is a rather odd tangle of component classes.

As for the label update, this is most likely due to this line in your button handler

player update = new player();

You are creating a new separate "player" instance and calling the update method on it - not the one that you actually added to your JFrame.

Comments
Great help thanks a lot!!

well what i'm trying to do is keep each JPanel separate from the main program and define them there and then do the actions etc from the main.

I know it probilly is not right as i only starting off and don't expect to get the programming
standards right. but it seem to be working for me up until now.

public class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            
            player update = new player();
            answeertotal = Integer.parseInt(answeer.getText( ));
            
            if(answeertotal == total)
            {
                JOptionPane.showMessageDialog(null,  "Correct","",JOptionPane.INFORMATION_MESSAGE);
                correction.reset();  // THIS WORKS FOR ME I CREATE AN INSTANCE OF MAINPANEL AND CALL THE RESET() METHOD WHICH AND NEW QUESTION ETC LOOK AT ATTACHMENTS FOR FULL PROGRAM
                update.updateScore();// THIS IS MENT TO UPDATE THE CURRENT SCORE WHICH IS THE score JLabel BY USING THE updateScore() METHOD IN THE player class
                
                
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Sorry your wrong" + answeertotal + "   "+ total ,"",JOptionPane.ERROR_MESSAGE);
                update.updatelife();
            }
        }
    }

Once the call has been made it goes to the updateScore() method and new score then goes to the int2str(); to get it displayable and finaly it suppose to append the new scor to the score JLabel score.setText(sco);

public void int2str()
        {
            lif = String.valueOf("Current lives : " + lives);
            sco = String.valueOf("Current score : " + scores);
        }
        
        public  void updateScore()
        {
            scores = scores + 10;
            int2str();
            score.setText(sco);
        }

not sure where i'm gone wrong. and ya extending the JPanel from each inner class is bad alright i'd say but when i change it i get quiet an amount of errors.

Well, this statement

update.updateScore();

is not updating the panel that is in your frame. It's being called against the local "update" variable that you created. You need to change the constructor to initialize a class level "player" instance variable instead of the local one that it is creating now (see my previous post) and call the updateScore() on that instance variable. Currently you don't have a reference to that instance to work from. (Whereas you do with the "correction" call that is currently working for you in that handler)

If you want to keep the panels as separate classes, I would recommend moving the JLabels that go in those panel into those classes and not have them in your top-level JFrame class. Let those panels handle their own components. There is no reason that updateScore should have to set text on a label that is defined at the JFrame level. Having them each extend JPanel is fine, but keep their functionality encapsulated as well, otherwise you are not gaining anything by separating them. Also note that you don't have to put those objects into other JPanels just to add them to the frame - they are JPanels themselves - just add them.

Thanks a lot for that. I know the code is probably bad practice but you got to learn some way.
Answer was right in front of me!!! as you said just make it global.

Got JMenu working with different levels in it working now with phased picture display where you got to get 10 questions right before you see the whole picture.

Is there any tips you could give me for cleaning up my code etc??

Thanks anyway.

I would just recommend working on getting the responsibilities for each of those classes that extend JPanel encapsulated. Think of them as separate entities with distinct duties to maintain certain pieces of your functionality. Your top-level class should just call methods on those to give them the info they need to update whatever state they are maintaining. Your classes should not have to update internal variables in one another. Ideally they shouldn't even know anything about the internal variables of the other. Create methods that provide the interaction mechanisms between them so the class itself is controlling it's own internal state. Don't let other classes muck about with the guts of a classes variables.

This question has already been answered. Start a new discussion instead.