0

I would like to know how to smooth the automatic transition of a JScrollPane. I'm using

JScrollBar scroll = scollpane.getVerticalScrollBar();
scroll.setValue(scroll.getMaximum());

Something like JScrollPane.setUnitIncrement(inc) with inc values of ~0.001 and a 10ms delay between increments would help, I guess, but setUnitIncrement only takes ints.

Also, I want the same smooth animation for adding an element like JList to a JPanel with BoxLayout. I'd like it to have a smooth top-to-bottom appearance, not instantly being displayed when using JPanel.add(JList).

I'm mostly looking towards an implementation of AurelienRibon's SlidingLayout or Tween, but I can't wrap my head around it.

dryairship
  • 6,022
  • 4
  • 28
  • 54
Bill Mack
  • 49
  • 5
  • Maybe something like [this](http://stackoverflow.com/questions/15604399/simple-way-of-creating-an-animated-jscrollpane-in-java/15605803#15605803)? – MadProgrammer Dec 29 '15 at 02:31
  • Adding a time delay alone doesn't help. I can't get the scrollbar to transition between lines. It only advances one line at a time. As for my other request, the main problem is gradually displaying the JList. Also, I'm preety new to java so there may be an easy way out. – Bill Mack Dec 29 '15 at 02:39
  • I think you need to explain better what it is you want to achieve. The linked example will cause the scroll pane to scroll automatically over time – MadProgrammer Dec 29 '15 at 02:42
  • You can think of it like scrolling through the notepad window. It will never land the view on a half line. So does my application, it scrolls one line at a time and I want it to scroll 0.1 or less of a line at a time. – Bill Mack Dec 29 '15 at 02:49
  • *"an easy way out*" - While Swing can do animation, it wasn't designed for it from the get go. There are some tricks you can do, for example, using a proxy layout manager to inject animation, for [example](http://stackoverflow.com/questions/32368190/too-many-jpanels-inside-a-jpanel-with-gridbaglayout/32372506#32372506), [example](http://stackoverflow.com/questions/14540080/animations-when-using-gridbag-layout/14541651#14541651) and [example](http://stackoverflow.com/questions/27463989/java-moving-jlabel-twice-using-timer/27466801#27466801) – MadProgrammer Dec 29 '15 at 04:20
  • Or even do something more complex like [this example](http://stackoverflow.com/questions/29645202/set-jbutton-to-the-location-of-another-jbutton/29651705#29651705) – MadProgrammer Dec 29 '15 at 04:22
  • 1
    `I want it to scroll 0.1 or less of a line at a time.` - you can scroll 1 pixel at a time. If it doesn't work, then your code is wrong. Since you didn't post a [SSCCE](http://sscce.org/) demonstrating the problem we can't help. So create a simple GUI with a scroll pane and a "Start Scrolling" button. The button will start a Timer and change the value of the scrollbar by 1 pixel every time the Timer fires. – camickr Dec 29 '15 at 05:26
  • Seems you're right. I have a dubious Thread.wait() in my project that is messing with Thread.sleep and forces a bad scrollbar update. I did a workaround with DeltaTime and it updates well now. Also, the animated [example](http://stackoverflow.com/questions/32368190/too-many-jpanels-inside-a-jpanel-with-gridbaglayout/32372506#32372506) was just what I was looking for. Thank you for your answers ! – Bill Mack Dec 29 '15 at 07:47

1 Answers1

1

Smooth animations (any type, in any program) are done with implementations of Delta-Time.

Most frameworks use a Delta-Time pattern (the ones that don't are doing it wrong, because other methods are prone to Time-Drifting bugs), and are hard to understand if you don't grasp the concept of Delta-Time first.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
CosmicGiant
  • 6,275
  • 5
  • 43
  • 58