aspose file tools*
The moose likes Java in General and the fly likes Listen to changes to an integer variable to update JLabel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Listen to changes to an integer variable to update JLabel" Watch "Listen to changes to an integer variable to update JLabel" New topic
Author

Listen to changes to an integer variable to update JLabel

M Aman
Greenhorn

Joined: Sep 10, 2010
Posts: 3
I am making a quiz program in Swing. On the GUI there is a JLabel which displays the current question number. I have stored this in an integer variable. Now, this number can change when the user presses <<previous>> or <<next>> button or clicks New File or Open File on the GUI . I change the display question as follows:

And similar code in the newFile and OpenFile methods.
This works, but it is not very elegant and there is lots of rewriting of code in every method/event that can change count.
Since currentQuestionLabel is associated with count, I want it to change every time <<count>> changes, without having to depend on action events such button fire. Also, the previousQuestion and nextQuestion should be disabled if count is 0 or <<maxQuestions>>.

How can I listen to the changes made to count and act accordingly, at one place?
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

First thing that comes to mind is to encapsulate the count integer in a custom class/object. If you then make it 'observable' and you let all objects that need to know about the changes to the count integer, register themselves to this object, you're pretty close to what you're looking for.

Read up on the Observer pattern (on the net or in a book like Head First Design Patterns). It'll give you some more detail on how to implement this.

Cheers,
Wim
M Aman
Greenhorn

Joined: Sep 10, 2010
Posts: 3
First thing that comes to mind is to encapsulate the count integer in a custom class/object. If you then make it 'observable' and you let all objects that need to know about the changes to the count integer, register themselves to this object, you're pretty close to what you're looking for.

Read up on the Observer pattern (on the net or in a book like Head First Design Patterns). It'll give you some more detail on how to implement this.

Cheers,
Wim


Yes, I have implemented Observer and Observable feature now. I created two inner classes Count and CountObserver:

Now when any event that makes change to count, I call . This way, I can make the right changes to nextQuestion and previousQuestion (as in when to be disabled/enabled). I did a run, and it works fine. Thank you so much.
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

Glad to see that worked for you. And thanks in return for providing some details and code snippets of the implementation. Great for reference.

Cheers,
Wim
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listen to changes to an integer variable to update JLabel