• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTabbedPane not refreshing

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I have created a java class with two JtabbedPane and i need to refresh these two panels every 10 seconds... I tried repaint(), revalidate(), validate functions.... But no fruitful has emerged so far... Could you some one please help me on this...

Thanks in advance... I am placing my sample code here for your reference... Your help would be really appreciated...Thanks

This is just sample...


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) please UseCodeTags next time. I've added them for you this time, and look how much better your post looks
2) please EaseUp. I've removed the "urgent" part from your subject line

Read concurrency in Swing and you'll understand the problem. The solution: use a javax.swing.Timer.
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.Actually first time i have been using this forum. Hence i was not aware of that. Still i dont get the actual issue with the program. Could you please brief me bit?

Thanks much
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Swing and AWT, there is the Event Dispatcher Thread (EDT). This is one single thread that does many things, including handling user input (typing, mouse clicks, etc) and repainting the user interface. This means you shouldn't do anything lengthy in the EDT, and that includes code inside listeners. Otherwise, when you click a button, the entire user interface hangs until that handling method ends.
Another important issue is that changes to the user interface should be done from the EDT as well, or you may get errors. I've seen lots of ArrayIndexOutOfBoundsExceptions because changes were done from another thread.

You said you want to repaint every 10 seconds. That means you need to use some kind of timer. That Thread.sleep(1000 * 10) is wrong for two reasons:
1) it is one single sleep of 10.000 milliseconds (10 seconds).
2) if you do this in the EDT your user interface will do nothing else for the full 10 seconds.

Now, as timers are concerned, there are two available in the API:
- java.util.Timer
- javax.swing.Timer

Both could do what you want but the second has one advantage: it runs on the EDT. That means you don't have to perform "tricks" like using EventQueue.invokeLater. You can simply put your code in the ActionListener's actionPerformed method and be done.

So, for example:
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your quick response. After i placed my code action performed method it was repeating for every 10 seconds. But the JTabbedane is not refreshed and i seeing the my old data only. Here is my latest code for your reference.

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks... Still it doesn't work for me
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But the JTabbedane is not refreshed and i seeing the my old data only.


If you want to refresh the data, why are you manipulating the components? You should be manipulating the data.


scrollPane1.setPreferredSize(new Dimension(1200,780));


Why are you explicitly setting the scrollPane's size? What do you hope to achieve by this?

In pseudo code
1) Construct Frame
2) Add tables to tabbed pane
3) Add tabbed pane to frame
4) Show frame
5) Update table model

If you update the model, then you will not need to invoke revalidate or repaint
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please give a small sample code? I am very new to swing... Thanks in advance
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have already posted code. You wrote it right?
Just pick from your code which matches the steps and sequence and you are ready to go.
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you much for your very quick response. code works very fine. One second thanks
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Now data is refreshing every 10 seconds. Also i would like set foreground color based on the gender... could you please help me out? Thanks in advance

reply
    Bookmark Topic Watch Topic
  • New Topic