1

I have just written a program in Netbeans that moves/copies/deletes files, and I wanted to give it a "diagnostic mode" where information about selected files, folders, variables, etc is displayed in a Text Area. Now, I could set this to only be visible when the "diagnostic mode" toggle is selected, but I think it would look awesome if the text area started behind the program, and "slid" out from behind the JFrame when the button is toggled. Is there any way to do this?

Thanks! -Sean

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Sean Nelan
  • 23
  • 3
  • A second JFrane with setDefautCloseAction HIDE_ON_CLOSE – Joop Eggen May 04 '14 at 20:21
  • @JoopEggen: ????. Are you really recommending this? To the original poster: This would require animation of some sort, often controlled by a Swing Timer. You could create an image of the sliding JPanel and then draw that image with varying degrees of translation, until it is in position, and then use a CardLayout to swap the image-showing JPanel for the real thing. – Hovercraft Full Of Eels May 04 '14 at 20:24
  • Also, please look at [my answer here](http://stackoverflow.com/a/11283582/522444) to a similar question, and to [this answer as well](http://stackoverflow.com/a/22275584/522444) that does a fade-in fade-out swap. – Hovercraft Full Of Eels May 04 '14 at 20:26
  • @HovercraftFullOfEels I thought a separate JFrame to be the simplest solution, though it lacks the appeal of your solution. – Joop Eggen May 05 '14 at 10:07

1 Answers1

1

Here is some starter code for you. This will right-slide a panel of just about any content type. Tweak as necessary. Add error checking and exception handling.

Tester:

static public void main(final String[] args) throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            final JPanel slider = new JPanel();
            slider.setLayout(new FlowLayout());
            slider.setBackground(Color.RED);
            slider.add(new JButton("test"));
            slider.add(new JButton("test"));
            slider.add(new JTree());
            slider.add(new JButton("test"));
            slider.add(new JButton("test"));
            final CpfJFrame42 cpfJFrame42 = new CpfJFrame42(slider, 250, 250);
            cpfJFrame42.slide(CpfJFrame42.CLOSE);
            cpfJFrame42.setSize(300, 300);
            cpfJFrame42.setLocationRelativeTo(null);
            cpfJFrame42.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            cpfJFrame42.setVisible(true);
        }
    });
}

Use GAP & MIN to adjust spacing from JFrame and closed size.

Closed

Opened

The impl is a little long...it uses a fixed slide speed but that's one the tweaks you can make ( fixed FPS is better ).

package com.java42.example.code;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;


public class CpfJFrame42 extends JFrame {

public static int       GAP               = 5;
public static int       MIN               = 1;

public static final int OPEN              = 0x01;
public static final int CLOSE             = 0x02;
private JDialog         jWindow           = null;
private final JPanel    basePanel;
private final int       w;
private final int       h;
private final Object    lock              = new Object();
private final boolean   useSlideButton    = true;
private boolean         isSlideInProgress = false;

private final JPanel    glassPane;
{
    glassPane = new JPanel();
    glassPane.setOpaque(false);
    glassPane.addMouseListener(new MouseAdapter() {
    });
    glassPane.addMouseMotionListener(new MouseMotionAdapter() {
    });
    glassPane.addKeyListener(new KeyAdapter() {
    });
}

public CpfJFrame42(final Component component, final int w, final int h) {

    this.w = w;
    this.h = h;
    component.setSize(w, h);

    addComponentListener(new ComponentListener() {
        @Override
        public void componentShown(final ComponentEvent e) {
        }

        @Override
        public void componentResized(final ComponentEvent e) {
            locateSlider(jWindow);
        }

        @Override
        public void componentMoved(final ComponentEvent e) {
            locateSlider(jWindow);
        }

        @Override
        public void componentHidden(final ComponentEvent e) {
        }
    });

    jWindow = new JDialog(this) {
        @Override
        public void doLayout() {
            if (isSlideInProgress) {
            }
            else {
                super.doLayout();
            }
        }
    };
    jWindow.setModal(false);
    jWindow.setUndecorated(true);
    jWindow.setSize(component.getWidth(), component.getHeight());
    jWindow.getContentPane().add(component);
    locateSlider(jWindow);
    jWindow.setVisible(true);

    if (useSlideButton) {
        basePanel = new JPanel();
        basePanel.setLayout(new BorderLayout());
        final JPanel statusPanel = new JPanel();
        basePanel.add(statusPanel, BorderLayout.SOUTH);
        statusPanel.add(new JButton("Open") {
            private static final long serialVersionUID = 9204819004142223529L;
            {
                setMargin(new Insets(0, 0, 0, 0));
            }
            {
                addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        slide(OPEN);
                    }
                });
            }
        });

        statusPanel.add(new JButton("Close") {
            {
                setMargin(new Insets(0, 0, 0, 0));
            }
            private static final long serialVersionUID = 9204819004142223529L;
            {
                addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        slide(CLOSE);
                    }
                });
            }
        });

        {
            //final BufferedImage bufferedImage = ImageFactory.getInstance().createTestImage(200, false);
            //final ImageIcon icon = new ImageIcon(bufferedImage);
            //basePanel.add(new JButton(icon), BorderLayout.CENTER);
        }

        getContentPane().add(basePanel);
    }
}

private void locateSlider(final JDialog jWindow) {
    if (jWindow != null) {
        final int x = getLocation().x + getWidth() + GAP;
        final int y = getLocation().y + 10;
        jWindow.setLocation(x, y);
    }
}

private void enableUserInput() {
    getGlassPane().setVisible(false);
}

private void disableUserInput() {
    setGlassPane(glassPane);
}

public void slide(final int slideType) {
    if (!isSlideInProgress) {
        isSlideInProgress = true;
        final Thread t0 = new Thread(new Runnable() {
            @Override
            public void run() {
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                disableUserInput();
                slide(true, slideType);
                enableUserInput();
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                isSlideInProgress = false;
            }
        });
        t0.setDaemon(true);
        t0.start();
    }
    else {
        Toolkit.getDefaultToolkit().beep();
    }
}

private void slide(final boolean useLoop, final int slideType) {
    synchronized (lock) {
        for (int x = 0; x < w; x += 25) {
            if (slideType == OPEN) {
                jWindow.setSize(x, h);
            }
            else {
                jWindow.setSize(getWidth() - x, h);
            }
            jWindow.repaint();
            try {
                Thread.sleep(42);
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
        if (slideType == OPEN) {
            jWindow.setSize(w, h);
        }
        else {
            jWindow.setSize(MIN, h);
        }
        jWindow.repaint();
    }
}
}
Java42
  • 7,628
  • 1
  • 32
  • 50