Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Refreshing TextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Refreshing TextArea " Watch "Refreshing TextArea " New topic
Author

Refreshing TextArea

Julie Maenhout
Greenhorn

Joined: Dec 12, 2012
Posts: 7
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

Joined: Mar 24, 2005
Posts: 186
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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Dec 12, 2012
Posts: 7
It is a Swing GUI.

Thanks for the quick reply!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Julie Maenhout
Greenhorn

Joined: Dec 12, 2012
Posts: 7
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

Joined: May 03, 2008
Posts: 4531
    
    5

Julie, please BeForthrightWhenCrossPostingToOtherSites <- link
http://www.java-forums.org/new-java/66375-refreshing-textarea.html


luck, db
There are no new questions, but there may be new answers.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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

Joined: Dec 12, 2012
Posts: 7
Thanks for your help! I'll try and see if I can get it to work!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refreshing TextArea