1

I have a question. I want to make a swing form that, when clicking in a button he slides a panel (with his content) to the left so the panel on the right replaces it with a smooth effect.

I Have tried to do a while how checks the size of the panel and then minimize it and shows the next one like this :

    while (jpanelprincipal1.getWidth() < 439 || jpanelprincipal1.getHeight() > 250)
    {
        int panel1width = jpanelprincipal1.getWidth();
        int panel2height = jpanelprincipal1.getHeight();
        jpanelprincipal1.setSize(panel1width -- , panel2height --);
        jpanelprincipal2.setSize(440,250);
    }

I used this trick in C# but with the Application.DoEvent(); (how obviously it's not available on java).

Is there anyway i can make a slide effect of 2 or more panels?

BTW : Sorry for my very bad english !

Thanks In Advance, Luis Da Costa

aliasbody
  • 816
  • 3
  • 11
  • 25

3 Answers3

6

As suggested by @HFOE, javax.swing.Timer is a good choice for animation. The setDividerLocation() method of JSplitPane can be called from the ActionListener. See How to Use Split Panes for additional options.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5069152 */
public class SplitPaneTest {

    double ratio = 0.5;
    double delta = ratio / 10;
    private void create() {
        JFrame f = new JFrame("JSplitPane");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MyPanel p1 = new MyPanel(Color.red);
        MyPanel p2 = new MyPanel(Color.blue);
        final JSplitPane jsp = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT, true, p1, p2);
        Timer timer = new Timer(200, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ratio += delta;
                if (ratio >= 1.0) {
                    ratio = 1.0;
                    delta = -delta;
                } else if (ratio <= 0) {
                    delta = -delta;
                    ratio = 0;
                }
                jsp.setDividerLocation(ratio);
            }
        });

        f.add(jsp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        timer.start();
    }


    private static class MyPanel extends JPanel {

        Color color;

        public MyPanel(Color color) {
            this.color = color;
            this.setPreferredSize(new Dimension(300, 300));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.drawLine(0, 0, getWidth(), getHeight());
            g.drawLine(getWidth(), 0, 0, getHeight());
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SplitPaneTest().create();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
6

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
camickr
  • 321,443
  • 19
  • 166
  • 288
4

I would probably do this with a Swing Timer. Change a class field representing the x, y position of the sliding JPanel in the timer's ActionListener and then call repaint on the container holding the JPanels. A JLayeredPane could work well as the container for the sliding JPanels.

Edit 1: regarding your request for code, I think the best thing is for you to try to create a very small compilable runnable program that attempts to do this, and then post your code with an explanation of your program's behavior as an edit to your original post. Also send us a comment to notify us of your changes. Then we can inspect your code, test it, modify it, and help you mold it into a working program. This is called creating a "Short, Self Contained, Correct (Compilable), Example" or SSCCE (please check the link).

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks for your help, but what is the easiest way to do that ? Using the Swing Timer or the JLayeredPane ? – aliasbody Feb 21 '11 at 17:39
  • These two concepts are not mutually exclusive. I would use the JLayeredPane to hold the sliding JPanels and the Swing Timer to do the animation -- so I'd use them both. Check out the Swing tutorials on these guys and you'll see how they work. – Hovercraft Full Of Eels Feb 21 '11 at 17:54
  • Argh :S Even after reading a lot of tutorials a still have problems with the slide :s Can you help me please with some code ? Thanks in Advance – aliasbody Feb 21 '11 at 18:36
  • see Edit 1. Or try the fine suggestions from the other folks here. – Hovercraft Full Of Eels Feb 21 '11 at 22:07