Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refreshing TextArea

 
Julie Maenhout
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I wonder if anyone can help me out. I have to make a code for a certain type of bar, where the prices change everytime someone buys a drink (example: customer buys 3 beers, the prices of all drinks change, 30 seconds later 5 lemonades are sold, prices of all drinks change again). I would like to make a dynamic price list that changes every time after a purchase (or if that isn't possible: that refreshes every 5 seconds (for example)).

Everytime a purchase has been made, I change the prices in my database and then I execute a query to get the new prices. Normally I would use setText() to show the prices on the screen, but this doesn't work because the GUI where I change the prices is another then the GUI where I have to show them.

Is there anyone who has a clue what I have got to do?

Thanks in advance!
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you're talking about a web based GUI (not swing). You can use Ajax with frequent polling on the page which displays the prices. This is probably the easiest solution to implement, but it is not very efficient.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julie Maenhout wrote:Is there anyone who has a clue what I have got to do?


Make your GUI completely separate from your program logic. Make an intermediary class to go between your GUI and the class that handles the logic. Something like this:

[GUI: handle user activity and display helpful text (like the price list)]
[Intermediary: watch the GUI controls for user events, take the data from those events and hand them off to the Logic, then get the updated price list and return it to the GUI]
[Logic: when new data arrives, update the price list]
 
Julie Maenhout
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a Swing GUI.

Thanks for the quick reply!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to read about the publisher/subscriber design pattern. You would most likely have something that computes all the new drink prices. You can set it up so that your GUI 'registers' with it as a "i want to know when things change" subscriber.

Each time the drink prices change, the publisher would send a message to all subscribers, saying "hey, things have changed". That way, you don't have to waste bandwith by your GUI checking every X seconds...if nothing changes for 10 minutes, there is no reason for these two things to be talking. But as soon as things do change, everyone who needs to know is alerted.
 
Julie Maenhout
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly what I mean, but I don't know how and where to send this signal. Is it possible to explain really thoroughly because I'm a real newbie and I don't know what I'm looking for?
I'm sorry if this causes you much work...
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julie, please BeForthrightWhenCrossPostingToOtherSites <- link
http://www.java-forums.org/new-java/66375-refreshing-textarea.html
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are tons of sites and books that can explain things much better than I could. Google is your friend, as it your local bookstore.

but BRIEFLY...

Your class that computes the new prices implements a few methods (among others)...registerObserver, removeObserver, and notifyObserver. Your gui would implement a update() method.

The compute class would have (again, among other things) an ArrayList to hold the interested objects. An object could call the registerObserver() method, passing itself as a reference. The registerObserver() method then adds this reference to the list.

removeObserver does the similar obvious thing.

When the right conditions are met, the notifyObserver method is called. This basically iterates through the ArrayList, and calls the update() method on each object reference it finds there.

I got most of this from the book "Head First Design Patterns", which is a terrific reference. But if you simply google "design patterns Observer", you'll get tons of hits.
 
Julie Maenhout
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help! I'll try and see if I can get it to work!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic