• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Listen to changes to an integer variable to update JLabel

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 96
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic