1

I am trying to make a program in which, On Selecting JToggleButton shows a Sliding Drawer animated Panel and on deselecting it Hides it.

I want something like this on Clicking Toggle Button,

| Toggle button|---->|Panel|
Slides the Panel like a drawer

and on Deselecting Toggle Button

|Toggle Button|<-----|Hides panel|

I am able to create a new panel on Selecting a JToggle Button but i am confused with creating the animation.

myToggleButton.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            if(ev.getStateChange()==ItemEvent.SELECTED){

                myPanel.add(slidingPanel,BorderLayout.CENTER); 
                myPanel.revalidate();
                myPanel.repaint();

            } else if(ev.getStateChange()==ItemEvent.DESELECTED){

                myPanel.remove(slidingPanel);
                myPanel.revalidate();
                myPanel.repaint();
                }
            }
        }); 

How can i achieve sliding Drawer animation for a panel on a ToggleButton click and then hiding the panel .

  • Swing provides no support for layout animation. But [here](https://www.algosome.com/articles/java-swing-panel-animation.html) you can find an example (OK, it could be complicated for you, when you have no expertise in Swing). – Sergiy Medvynskyy Aug 07 '19 at 14:11
  • Sounds similar to the https://github.com/javagl/CommonUI/blob/master/src/main/java/de/javagl/common/ui/panel/collapsible/CollapsiblePanel.java that I once implemented. If the intention only is to *move* the panel (and not really *resize* it during the animation), things may be simpler, though... – Marco13 Aug 07 '19 at 14:22

1 Answers1

1

I was able to figure out how to achieve it.I am writing the code for help.

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

public class SlidePanelDemo extends JPanel {

    private JPanel pnlMain;
    private JFrame frame;
    private JPanel contentPane;
    private static int drawWidth = 200;
    private static int drawHeight = 100;
    private Timer timer;
    private int graphWidth = 5;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SlidePanelDemo().createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        JToggleButton button = new JToggleButton("Tools");
        button.setSize(30, 30);
        button.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ev) {
                if (ev.getStateChange() == ItemEvent.SELECTED) {
                    System.out.println("button is selected");
                    timer = new Timer(40, (ActionEvent e1) -> {
                        graphWidth = graphWidth + 5;
                        System.out.println("graphWidth is" + graphWidth);
                        pnlMain.setSize(graphWidth, drawHeight);
                        pnlMain.setVisible(true);
                        if (graphWidth >= 0 && graphWidth == drawWidth) {
                            timer.stop();
                        }
                    });
                    timer.setRepeats(true);
                    timer.start();
                } else if (ev.getStateChange() == ItemEvent.DESELECTED) {
                    System.out.println("button is not selected");
                    timer = new Timer(40, (ActionEvent e1) -> {
                        graphWidth = graphWidth - 5;
                        System.out.println("graphWidth is" + graphWidth);
                        if (graphWidth > 0 && graphWidth <= drawWidth) {
                            pnlMain.setSize(graphWidth, drawHeight);
                            pnlMain.setVisible(true);
                        }
                        if (graphWidth == 5) {
                            timer.stop();
                        }
                    });
                    timer.setRepeats(true);
                    timer.start();
                }
            }
        });
        pnlMain = createMainPanel();
        JButton label = new JButton("Click");
        JButton label2 = new JButton("Click me");
        pnlMain.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        pnlMain.add(label);
        pnlMain.add(label2);
        contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        contentPane.setOpaque(true);
        contentPane.add(button);
        contentPane.add(pnlMain);

        pnlMain.setVisible(true);

        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("Slide Out Panel Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(contentPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setSize(500, 300);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(graphWidth, drawHeight);
            }
        };
        panel.setBorder(BorderFactory.createTitledBorder("Main"));
        return panel;
    }
}