🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages Started by Sam Seaborn Fri, 19 Dec 2003 18:56
animate move of JSplitPane divider?
#1402
Author: Sam Seaborn
Date: Fri, 19 Dec 2003 18:56
33 lines
1910 bytes
Hi,

I have a routine that automatically moves the divider of a JSplitPane from,
say, 0 to 100 (to show/hide a panel in my frame).  I simply use
splitPane.setDividerLocation(i) and it works great.

The thing is, I want to divider to slide back and forth with a smooth
"animated" effect.

I have tried something like the following:

// when button clicked
for (int iLocation=0; iLocation<100; iLocation++) {
  final int i = iLocation;
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      splitPane.setDividerLocation(i);
      revalidate();
      try {
        Thread.sleep(100);
      } catch (Exception x) {}
    }
  });
}

But the result is, click the button, get a pause for a couple seconds, then
the divider goes the final location.  I don't see any animation.

What am I doing wrong?

Any help is greatly appreciated,

SLL
Re: animate move of JSplitPane divider?
#1407
Author: John McGrath [TeamB]
Date: Sat, 20 Dec 2003 00:28
53 lines
2867 bytes
On 12/19/2003 at 1:56:25 PM, Sam Seaborn wrote:

> I have a routine that automatically moves the divider of a JSplitPane
> from, say, 0 to 100 (to show/hide a panel in my frame).  I simply use
> splitPane.setDividerLocation(i) and it works great.
> 
> The thing is, I want to divider to slide back and forth with a smooth
> "animated" effect.
> 
> I have tried something like the following:
[snip]

> But the result is, click the button, get a pause for a couple
> seconds, then the divider goes the final location.  I don't see any
> animation.

That is what I would expect from the code you provided.  What it does
is create 100 Runnable objects and post them to the AWT event queue
using SwingUtilities.invokeLater(), with a 1/10 second pause after
posting each event.  After your button click action completes (which
takes about 10 seconds), the 100 Runnable objects will be executed
rapidly from the event queue, so the motion of the divider is not
apparent.

The javax.swing.Timer class is exactly what you need.  It will run a
piece of your code periodically in the AWT event thread.  The following
(untested) code should be more or less what you want:

   public static final int LIMIT = 100;
   public static final int INTERVAL = 100;

   final Timer timer;
   ActionListener timerAction = new ActionListener() {
      public void actionPerformed( ActionEvent event ) {
         int loc = splitPane.getDividerLocation();
         if ( loc < LIMIT ) {
            splitPane.setDividerLocation( loc + 1 );
         } else {
            timer.stop();
         }
      }
   }
   timer = new Timer( INTERVAL, timerAction );
   timer.start();

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads