Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listen to changes to an integer variable to update JLabel

 
M Aman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic