Hello,
I'm working on an application with a drag and drop window system. One Application window with 10+ inner windows draggable over the screen.
Now, I'm trying to implement a "popup" mechanism for inside of the inner windows. For example when clicking a button in a window, that the window turns black and a configuration panel appears on top.
This works great but I have some problems with JButtons underneath the popup panel. I active the popup by clicking a button, the window turns dark and the popup is shown where the button was clicked.
When I move the mouse out of the button (which is supposed to be in normal panel with a higher zorder than the popup) it results that the button turns visible through the popup.
To be more specific I've prepared a little test which demonstrates my problem:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ButtonOverlapTest extends JPanel {
private JPanel glasspane;
public ButtonOverlapTest() {
setPreferredSize(new Dimension(400,400));
setLayout(null);
JButton btnOpen = new JButton("open");
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showPopup();
}
});
JPanel pnlCenter = new JPanel();
pnlCenter.setSize(getPreferredSize());
pnlCenter.setBackground(Color.red);
pnlCenter.add(btnOpen);
add(pnlCenter);
}
public void showPopup() {
JButton btnClose = new JButton("close");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removePopup();
}
});
JPanel popup = new JPanel();
popup.setLayout(new BorderLayout());
popup.setBackground(Color.white);
popup.setPreferredSize(new Dimension(150,150));
popup.add(btnClose, BorderLayout.SOUTH);
glasspane = new JPanel();
glasspane.setOpaque(true);
glasspane.setSize(getPreferredSize());
glasspane.setBackground(new Color(0,0,0,100));
glasspane.add(popup);
Component c = getComponent(0);
add(glasspane);
setComponentZOrder(c, 1);
setComponentZOrder(glasspane, 0);
validate();
repaint();
}
public void removePopup() {
remove(glasspane);
validate();
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ButtonOverlapTest());
frame.pack();
frame.setVisible(true);
}
}
A simple frame with a red JPanel in it with a Button. When I click the button, my "popup" appears occulting the open button.
Perfect until I move the mouse out of the open button which is invisible. Once out of it bounds, it get's visible again.
Where is my fault?
Thanks in advance.