Problem updating a JLabel??

Thread Solved
Reply

Join Date: Nov 2006
Posts: 29
Reputation: toomuchfreetime is an unknown quantity at this point 
Solved Threads: 1
toomuchfreetime's Avatar
toomuchfreetime toomuchfreetime is offline Offline
Light Poster

Problem updating a JLabel??

 
0
  #1
Mar 11th, 2008
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)


  1.  
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import javax.swing.JTextField;
  6. import javax.swing.event.* ;
  7. import java.net.*;
  8. import java.io.*;
  9. import java.awt.Color;
  10. import java.util.Random;
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author Stephen
  16.  */
  17. public class NumberGame extends JFrame
  18. {
  19. JLabel sumQ,picture,score,life;
  20. JPanel picpan,mainpan,playerpan;
  21. protected int total, answeertotal,lives = 5,scores;
  22. JTextField answeer;
  23. private Container c;
  24.  
  25. MainPanel correction = new MainPanel();
  26. ButtonHandler handler = new ButtonHandler();
  27.  
  28.  
  29. NumberGame()
  30. {
  31. c = getContentPane();
  32. c.setBackground(Color.white);
  33. setSize(300,500);
  34. setLocation(250,100);
  35. c.setVisible(true);
  36. c.setBackground(Color.PINK);
  37.  
  38. PicPanel pic = new PicPanel();
  39. JPanel picpan = new JPanel();
  40. picpan.add(pic);
  41.  
  42. MainPanel pane = new MainPanel();
  43. JPanel mainpan = new JPanel();
  44. mainpan.add(pane);
  45.  
  46. player status = new player();
  47. JPanel playerpan = new JPanel();
  48. playerpan.add(status);
  49.  
  50.  
  51. c.add(picpan,BorderLayout.NORTH);
  52. c.add(mainpan,BorderLayout.CENTER);
  53. c.add(playerpan,BorderLayout.SOUTH);
  54. }
  55.  
  56. public class ButtonHandler implements ActionListener
  57. {
  58. public void actionPerformed(ActionEvent e)
  59. {
  60.  
  61. player update = new player();
  62. answeertotal = Integer.parseInt(answeer.getText( ));
  63.  
  64. if(answeertotal == total)
  65. {
  66. JOptionPane.showMessageDialog(null, "Correct","",JOptionPane.INFORMATION_MESSAGE);
  67. correction.reset();
  68. update.updateScore();
  69.  
  70. }
  71. else
  72. {
  73. JOptionPane.showMessageDialog(null, "Sorry your wrong" + answeertotal + " "+ total ,"",JOptionPane.ERROR_MESSAGE);
  74. update.updatelife();
  75. }
  76. }
  77. }
  78.  
  79. class player extends JPanel
  80. {
  81. String lif,sco;
  82.  
  83. player()
  84. {
  85. int2str();
  86. life = new JLabel(lif);
  87. add(life);
  88. score = new JLabel(sco);
  89. add(score);
  90. }
  91.  
  92. public void int2str()
  93. {
  94. lif = String.valueOf("Current lives : " + lives);
  95. sco = String.valueOf("Current score : " + scores);
  96. }
  97.  
  98. public void updateScore()
  99. {
  100. scores = scores + 10;
  101. int2str();
  102. score.setText(sco);
  103. }
  104.  
  105. public void updatelife()
  106. {
  107. lives = lives - 1;
  108. int2str();
  109. life.setText(lif);
  110. }
  111.  
  112. }
  113.  
  114.  
  115.  
  116. public static void main(String args[])
  117. {
  118. JFrame frame = new NumberGame();
  119. frame.setVisible(true);
  120. frame.pack();
  121. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  122. }
  123. }


Thanks alot to anyone who takes time to have a look.
Last edited by toomuchfreetime; Mar 11th, 2008 at 2:43 pm.
Attached Images
File Type: jpg sun1.jpg (54.2 KB, 0 views)
Attached Files
File Type: java NumberGame.java (4.8 KB, 0 views)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Problem updating a JLabel??

 
1
  #2
Mar 11th, 2008
Well, you don't say what labels you are having trouble updating, but some things really stand out as a problem. In your constructor
  1. 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
  1. 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.
Last edited by Ezzaral; Mar 11th, 2008 at 2:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 29
Reputation: toomuchfreetime is an unknown quantity at this point 
Solved Threads: 1
toomuchfreetime's Avatar
toomuchfreetime toomuchfreetime is offline Offline
Light Poster

Re: Problem updating a JLabel??

 
0
  #3
Mar 11th, 2008
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.

  1. public class ButtonHandler implements ActionListener
  2. {
  3. public void actionPerformed(ActionEvent e)
  4. {
  5.  
  6. player update = new player();
  7. answeertotal = Integer.parseInt(answeer.getText( ));
  8.  
  9. if(answeertotal == total)
  10. {
  11. JOptionPane.showMessageDialog(null, "Correct","",JOptionPane.INFORMATION_MESSAGE);
  12. 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
  13. update.updateScore();// THIS IS MENT TO UPDATE THE CURRENT SCORE WHICH IS THE score JLabel BY USING THE updateScore() METHOD IN THE player class
  14.  
  15.  
  16. }
  17. else
  18. {
  19. JOptionPane.showMessageDialog(null, "Sorry your wrong" + answeertotal + " "+ total ,"",JOptionPane.ERROR_MESSAGE);
  20. update.updatelife();
  21. }
  22. }
  23. }


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);

  1.  
  2. public void int2str()
  3. {
  4. lif = String.valueOf("Current lives : " + lives);
  5. sco = String.valueOf("Current score : " + scores);
  6. }
  7.  
  8. public void updateScore()
  9. {
  10. scores = scores + 10;
  11. int2str();
  12. score.setText(sco);
  13. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Problem updating a JLabel??

 
0
  #4
Mar 11th, 2008
Well, this statement
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 29
Reputation: toomuchfreetime is an unknown quantity at this point 
Solved Threads: 1
toomuchfreetime's Avatar
toomuchfreetime toomuchfreetime is offline Offline
Light Poster

Re: Problem updating a JLabel??

 
0
  #5
Mar 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Problem updating a JLabel??

 
0
  #6
Mar 11th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC