aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTabbedPane not refreshing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTabbedPane not refreshing" Watch "JTabbedPane not refreshing" New topic
Author

JTabbedPane not refreshing

Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
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...


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Nov 30, 2010
Posts: 51
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.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
Thanks... Still it doesn't work for me
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10372
    
    8

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

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
Could you please give a small sample code? I am very new to swing... Thanks in advance
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10372
    
    8

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

Joined: Nov 30, 2010
Posts: 51
Thank you much for your very quick response. code works very fine. One second thanks
Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTabbedPane not refreshing