I wanted to hide my JDesktopPane
in a trigger of button. But this won't hide itself. I created a static void method that hides the frame and calling this in actionPerformed. Do I need to run another EDT to hide my frame? Any tips would appreciated.
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JDesktoppane extends JFrame
{
JDesktopPane desktop;
JMenu menu;
public JDesktoppane()
{
super("TITLE");
int inset = 80;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset,
screenSize.width - inset*2,
screenSize.height - inset*2);
//Set up the GUI
desktop = new JDesktopPane();
//Setting JDesktopPane as Container
setContentPane(desktop);
setJMenuBar(createMenuBar());
//Make dragging a little faster but perhaps uglier.
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}
public JMenuBar createMenuBar()
{
JMenuBar bar = new JMenuBar();
menu = new JMenu("CLICK ME");
JMenuItem item = new JMenuItem("CLICK ME AGAIN");
item.addActionListener(listener);
menu.add(item);
bar.add(menu);
return bar;
}
ActionListener listener = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
createFrame();
}
};
public void createFrame()
{
InternalFrame frame = new InternalFrame();
frame.setVisible(true);
desktop.add(frame);
}
public static void createAndShowGUI()
{
JDesktoppane p = new JDesktoppane();
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.setVisible(true);
}
public static void hideGUI()
{
JDesktoppane p = new JDesktoppane();
p.setVisible(false);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
public class InternalFrame extends JInternalFrame
{
public InternalFrame()
{
addComponentsToPane(getContentPane());
pack();
}
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
JDesktoppane.hideGUI();
}
};
private void addComponentsToPane(final Container pane)
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JButton btn = new JButton("HIDE ME");
btn.addActionListener(listener);
panel.add(btn);
pane.add(panel);
}
}
In hideGUI
you are creating a new window and then hiding that:
public static void hideGUI()
{
// creating a new window
JDesktoppane p = new JDesktoppane();
p.setVisible(false);
}
I think what you mean to do is somehow give the InternalFrame
a reference to the original window that you create in createAndShowGUI
.
You could do something like this:
public class InternalFrame extends JInternalFrame
{
JDesktoppane parent;
public InternalFrame(JDesktoppane parent)
{
this.parent = parent;
addComponentsToPane(getContentPane());
pack();
}
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// note
parent.setVisible(false);
}
};
...
}
And:
public void createFrame()
{
// vvvv
InternalFrame frame = new InternalFrame(this);
frame.setVisible(true);
desktop.add(frame);
}
It's a little strange though, for the internal pane to have a button which hides its parent. A more idiomatic way would be to have a JMenuBar or something like that with an option for it. (And do you have a plan to get the window back? Usually if you want to hide a window, you just minimize it.)