I am having trouble with implementing a glasspane on a JDialog.
Using the swing glasspane tutorial as a base I am trying to apply this to a JDialog.
Everything appears to work OK until I setDefaultLookandFeel(true) on the JDialog.
Events are propogated onto components on the JDialog (under the glasspane) but the JDialog doesn't appear to receive events such as dragging; clicking the top right had corner close cross.
Open the dialog by clicking the button
Dialog can be moved and closed; buttons work
Click the Checkbox to apply the Glasspane
Dragging no longer works; Cannot close
Buttons function as indicated.
Uncheck Checkbox; dragging etc functions correctly.
Finally be removing the JDialog.setDefaultLookandFeel(true) the dialog can be dragged even when the glasspane is raised.
Can anyone explain please?
import javax.swing.event.MouseInputAdapter;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class NotepadTest {
TextPanel jp;
JFrame jframe;
JMenuBar menuBar;
GlassDialog gd;
NotepadTest() {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
jframe = new JFrame("NotePad Test");
jp = new TextPanel();
jp.init();
jframe.getContentPane().add(jp, BorderLayout.CENTER);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.pack();
jframe.setVisible(true);
}
public static void main(String args[]) {
NotepadTest np = new NotepadTest();
}
class CheckAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
clickedCheckBox(e);
}
}
void clickedCheckBox(ActionEvent e) {
gd.myGlassPane.setVisible(((JCheckBox) e.getSource()).isSelected());
}
class dialogAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
gd = new GlassDialog(jframe);
gd.show();
}
}
class TextPanel extends JPanel {
TextPanel() {
}
void init() {
JCheckBox jcb = new JCheckBox("Glass Pane");
add(jcb);
jcb.addActionListener(new CheckAction());
add(Box.createHorizontalGlue());
JButton jbDialog = new JButton("Dialog");
add(jbDialog);
jbDialog.addActionListener(new dialogAction());
}
}
class GlassDialog extends MouseAdapter{
private MyGlassPane myGlassPane;
JDialog jdialog;
JPanel jp;
Container contentPane;
GlassDialog(JFrame jframe) {
jdialog = new JDialog(jframe, false);
jp = new JPanel();
contentPane = jdialog.getContentPane();
contentPane.add(jp);
}
GlassDialog() {
this(null);
}
JButton jbtn,jbtnBlock;
void show() {
jdialog.setPreferredSize(new Dimension(200, 100));
jdialog.pack();
jbtn = new JButton("Works thru GlassPane");
jbtn.setActionCommand("Works thru GlassPane");
jbtn.addMouseListener(this);
jbtnBlock = new JButton("Blocked byGlassPane");
jbtnBlock.setActionCommand("Blocked byGlassPane");
jbtnBlock.addMouseListener(this);
myGlassPane = new MyGlassPane(jbtn, menuBar, jdialog
.getContentPane());
jdialog.setGlassPane(myGlassPane);
myGlassPane.setVisible(false);
jp.add(jbtn);
jp.add(jbtnBlock);
jdialog.setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
System.out.println(((JButton)e.getSource()).getActionCommand());
}
class MyGlassPane extends JComponent {
public MyGlassPane(AbstractButton aButton, JMenuBar menuBar,
Container contentPane) {
CBListener listener = new CBListener(aButton, menuBar, this,
contentPane);
addMouseListener(listener);
addMouseMotionListener(listener);
}
}
class CBListener extends MouseInputAdapter {
Toolkit toolkit;
Component liveButton;
JMenuBar menuBar;
MyGlassPane glassPane;
Container contentPane;
public CBListener(Component liveButton, JMenuBar menuBar,
MyGlassPane glassPane, Container contentPane) {
toolkit = Toolkit.getDefaultToolkit();
this.liveButton = liveButton;
this.menuBar = menuBar;
this.glassPane = glassPane;
this.contentPane = contentPane;
}
public void mouseClicked(MouseEvent e) {
redirectEvent(e);
}
public void mouseEntered(MouseEvent e) {
redirectEvent(e);
}
public void mouseExited(MouseEvent e) {
redirectEvent(e);
}
public void mousePressed(MouseEvent e) {
redirectEvent(e);
}
public void mouseReleased(MouseEvent e) {
redirectEvent(e);
}
public void mouseDragged(MouseEvent e) {
redirectEvent(e);
}
public void mouseMoved(MouseEvent e) {
redirectEvent(e);
}
private void redirectEvent(MouseEvent e) {
boolean repaint = false;
Point glassPanePoint = e.getPoint();
Container container = contentPane;
Point containerPoint = SwingUtilities.convertPoint(glassPane,
glassPanePoint, contentPane);
// The mouse event is probably over the content pane.
// Find out exactly which component it's over.
Component component = SwingUtilities.getDeepestComponentAt(
container, containerPoint.x, containerPoint.y);
if ((component != null) && (component.equals(liveButton))) {
Point componentPoint = SwingUtilities.convertPoint(
glassPane, glassPanePoint, component);
component.dispatchEvent(new MouseEvent(component,
e.getID(), e.getWhen(), e.getModifiers(),
componentPoint.x, componentPoint.y, e
.getClickCount(), e.isPopupTrigger()));
}
}
}
}
}