• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Get data from database and show each by each data in a JLabel for 5 seconds

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get data from the database and show each row data in a JLabel that lasts for 5 seconds. After 5 seconds I need to show the second row data in the database.

My Database,



Code is,



When I execute this program it only set the last row detail to the JLabels. And also When the ArrayList Empty It needs to start from the beginning automatically.

So How can I fix this? Please anyone can help me? Thanks in advance.
 
Bartender
Posts: 5903
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your loop on line 34 has no timer in it so it just goes through your list in rapid fire. You probably don't want a loop there at all but something along the lines of
Where 'x' gets incremented once each time the timer event fires.
 
Hasitha Jayawardana
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Your loop on line 34 has no timer in it so it just goes through your list in rapid fire. You probably don't want a loop there at all but something along the lines of
Where 'x' gets incremented once each time the timer event fires.



I really don't know how to fix this. Can you help me?
 
Carey Brown
Bartender
Posts: 5903
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you set up a timer and a listener. You increment x. Then you call get(). So far so good.
This get method is called by your timer. Your call to EventManager.getAllEvents() should probably not be here. This get() is called from a timer loop and you don't want to get all the events every 5 seconds, so move this outside of the timer loop. Your for() loop here is a) redundant, you already have a loop in your timer, and b) it loops through all Events when all you want to do is get the next one. You can get a particular event from the list using an index, which I think your 'x' variable can do. You can call eventList.get(x).
 
Hasitha Jayawardana
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Here you set up a timer and a listener. You increment x. Then you call get(). So far so good.
This get method is called by your timer. Your call to EventManager.getAllEvents() should probably not be here. This get() is called from a timer loop and you don't want to get all the events every 5 seconds, so move this outside of the timer loop. Your for() loop here is a) redundant, you already have a loop in your timer, and b) it loops through all Events when all you want to do is get the next one. You can get a particular event from the list using an index, which I think your 'x' variable can do. You can call eventList.get(x).



I tried it outside the loop. But it gives me errors and has a problem with getting Event without a for loop. So I am glad that if you could show me how to do this. I really appreciate that.
 
Carey Brown
Bartender
Posts: 5903
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Hasitha Jayawardana
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Can you post what you've tried?



 
Carey Brown
Bartender
Posts: 5903
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Hasitha Jayawardana
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Line 13: see my last post.
Yes, you'll have to make eventList final.



      final ArrayList<Event> eventList = null;
       try
       {
           eventList = EventManager.getAllEvents(); // when I put it as final it gives error in here
       }catch(ClassNotFoundException | SQLException e)
       {
           System.out.println(e);
       }
 
Carey Brown
Bartender
Posts: 5903
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Hasitha Jayawardana
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:



Thank you soo much. It worked. Thanks for being there for me when I really needed you. I appreciate you so much.
 
Carey Brown
Bartender
Posts: 5903
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This bit of logic should be outside of the creation of the timer. No need to created the timer if the eventList is empty.
 
Rancher
Posts: 3051
22
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cross posted: https://stackoverflow.com/questions/53212247/get-data-from-database-and-show-each-by-each-data-in-a-jlabel-for-5-seconds/53214385#53214385

The OP was given an answer to this question yesterday.

The Op made no effort to solve the problem on their own and simply cut and pasted the exact question again in a different forum.
 
Hasitha Jayawardana
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:This bit of logic should be outside of the creation of the timer. No need to created the timer if the eventList is empty.



I got it. Thank you very much.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!