| | |
Problem updating a JLabel??
Thread Solved
![]() |
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)
Thanks alot to anyone who takes time to have a look.
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)
java Syntax (Toggle Plain Text)
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.
Last edited by toomuchfreetime; Mar 11th, 2008 at 2:43 pm.
Well, you don't say what labels you are having trouble updating, but some things really stand out as a problem. In your constructor 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 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.
Java Syntax (Toggle Plain Text)
JPanel playerpan = new JPanel();
As for the label update, this is most likely due to this line in your button handler
Java Syntax (Toggle Plain Text)
player update = new player();
Last edited by Ezzaral; Mar 11th, 2008 at 2:52 pm.
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.
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);
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.
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.
java Syntax (Toggle Plain Text)
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);
java Syntax (Toggle Plain Text)
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 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.
Java Syntax (Toggle Plain Text)
update.updateScore();
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.
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.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: jdbc sql statement
- Next Thread: struts tutorials
| Thread Tools | Search this Thread |
-xlint add android api applet application array automation bank bi binary blackberry block bluetooth class client code compile compiler component database developmenthelp dice eclipse equation error event fractal functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int j2me j2seprojects java javac javame javaprojects jetbrains jni jpanel jtable julia learningresources lego linux mac main map method mobile myregfun netbeans notdisplaying number online openjavafx pearl problem program project qt scanner screen scrollbar server set singleton sms sort sorting spamblocker sql sqlserver storm string superclass swing system textfields thread threads time title tree tutorial-sample update variablebinding windows xor






