I would like to have a JComponent
placed and sharing space with other components, differently depending on what configuration I select at runtime. For the first configuration the JComponent
is placed in BorderLayout
on EAST or WEST side and sharing space with other components in the frame. For the second configuration the JComponent
must be placed on top of all the other components, overlapping them and letting them resize.
What I did is create a JPanel
with a BorderLayout
and place in it all my JComponent
in NORTH, CENTER and EAST side. This is the initial configuration. I placed that JPanel
in a JLayeredPane
DefaultLayer layer and set this JLayeredPane
as the content of my JFrame
. I would like to use this JLayeredPane
to place the EAST side JComponent
on upper layer (if 2nd configuration is selected) and put it back to the DefaultLayer (if 1st configuration is selected).
Hope I'm clear and give enough details to have some help. Thanks in advance.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
/**
* @see http://stackoverflow.com/q/13776251/230513
*/
public class NewJavaGUI {
class Desktop {
private JPanel desktop;
private JLayeredPane layeredPane;
private HidingArea hidingArea;
private final JToggleButton toggleShowHide;
public Desktop() {
desktop = new JPanel(new BorderLayout(5, 5));
desktop.setBounds(0, 0, 600, 400);
layeredPane = new JLayeredPane(){
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 400);
}
};
layeredPane.add(desktop);
NorthArea northArea = new NorthArea();
northArea.setLayout(new BoxLayout(northArea, BoxLayout.LINE_AXIS));
toggleShowHide = new JToggleButton("Show");
toggleShowHide.addItemListener(new ShowHideItemListener());
JRadioButton conf1btn = new JRadioButton("In desktop");
conf1btn.setOpaque(false);
JRadioButton conf2btn = new JRadioButton("In layered pane");
conf2btn.setOpaque(false);
ButtonGroup group = new ButtonGroup();
group.add(conf1btn);
group.add(conf2btn);
northArea.add(conf1btn);
northArea.add(conf2btn);
northArea.add(Box.createHorizontalGlue());
northArea.add(toggleShowHide);
conf1btn.addActionListener(new SetComponentInDesktopListener());
conf2btn.addActionListener(new SetComponentInLayeredPaneListener());
desktop.add(northArea, BorderLayout.PAGE_START);
desktop.add(new CenterArea(), BorderLayout.CENTER);
hidingArea = new HidingArea();
desktop.add(hidingArea, BorderLayout.LINE_END);
conf1btn.setSelected(true);
}
/**
* The layered pane is added to the contentPane of a JFrame
*/
JLayeredPane getComponent() {
return layeredPane;
}
private class HidingArea extends JPanel {
public HidingArea() {
setBackground(Color.darkGray);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(200, 350);
}
}
private class NorthArea extends JPanel {
public NorthArea() {
setBackground(Color.gray);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 50);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(600, 50);
}
}
private class CenterArea extends JPanel {
public CenterArea() {
setBackground(Color.white);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(400, 400);
}
}
/**
* Hide or show the area contained in the JLayeredPane.
*/
private class ShowHideItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
JToggleButton toggle = (JToggleButton) e.getSource();
if (e.getStateChange() == ItemEvent.SELECTED) {
toggle.setText("Hide");
hidingArea.setBounds(
getBounds().width - hidingArea.getPreferredSize().width,
getBounds().height - hidingArea.getPreferredSize().height,
hidingArea.getPreferredSize().width,
hidingArea.getPreferredSize().height);
} else {
toggle.setText("Show");
hidingArea.setBounds(
getBounds().width,
getBounds().height - hidingArea.getPreferredSize().height,
hidingArea.getPreferredSize().width,
hidingArea.getPreferredSize().height);
}
}
}
/**
* @return the rectangular dimensions of the desktop.
*/
private Rectangle getBounds() {
return desktop.getBounds();
}
/**
* Add Hiding area to desktop.
*/
private class SetComponentInDesktopListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
toggleShowHide.setEnabled(false);
Component[] components = desktop.getComponents();
boolean toAdd = true;
for (Component component : components) {
if (component.equals(hidingArea)) {
toAdd = false;
}
}
if (toAdd) {
desktop.add(hidingArea, BorderLayout.LINE_END);
}
}
}
/**
* Remove Hiding area from desktop and add it to the JLayeredPane.
*/
private class SetComponentInLayeredPaneListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
toggleShowHide.setEnabled(true);
desktop.remove(hidingArea);
getComponent().add(hidingArea, (Integer) (JLayeredPane.DEFAULT_LAYER + 50));
}
}
}
private void display() {
JFrame f = new JFrame("NewJavaGUI");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Desktop d = new Desktop();
f.add(d.getComponent());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJavaGUI().display();
}
});
}
}