![]() |
| ||
| Problem updating a JLabel?? 2 Attachment(s) 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. |
| ||
| Re: Problem updating a JLabel?? 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. |
| ||
| Re: Problem updating a JLabel?? 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 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. |
| ||
| Re: Problem updating a JLabel?? 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. |
| ||
| Re: Problem updating a JLabel?? 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. |
| ||
| Re: Problem updating a JLabel?? 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. |
| All times are GMT -4. The time now is 12:32 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC