Slide JPanel Content in a JForm on Java

he slides a panel (with his content) to the left so the panel on the right replaces it with a smooth effect

You question mentions you want the panel to “slide”, but the code looks like you are trying to get the panel to “shrink”, so it is replaced by another panel.

Assuming you have two panels each with the same size, then you can “slide” one out of view while the other slides into view.

To do this you an use a panel with a GridLayout. This way each component will be the same size. Then you add the panel to a scrollpane without any scrollbars. The size of the scrollpane will need to be set to the size of the first compnoent. Then you can “slide” the two panels by changing the position of the viewport. So in your Timer you would have code something like:

JViewport viewport = scrollPane.getViewport();
Point position = viewport.getViewPosition();
position.x += 5;
viewport.setViewPosition( position );

You would then stop the Timer when the position is greater than the size of the component.

Leave a Comment