0

I've created a modal dialog inside a JFrame using the glass pane. My display method is quite simple: it creates a JPanel as glass pane with some alpha background and adds the JLabel and an ok and close button. Then the glass pane is set and displayed via frame.getGlassPane().setVisible(true);.

Everything works fine: if I call the method the pane is displayed and I can click ok or cancel and the glass pane hides. But the method returns directly after showing the glass pane. But I want it to behave like the JOptionPane methods: they block until the dialog is closed.

But everytime I'm trying to insert any kind of busy waiting at the end of my show method the GUI is frozen if I click the open button. I've also tried to get the mechanism from JDialog#show() but that's a bit to complicated for me.

So how to block the show method while the glass pane is visible?

Here is a simple example:

public class GlassPaneSSCE extends JPanel {

    private JFrame parentFrame;

    public GlassPaneSSCE(JFrame parent) {
        parentFrame = parent;
        addKeyListener(new KeyAdapter() {});
        addMouseListener(new MouseAdapter() {});
        setBackground(new Color(0, 0, 0, 100));
        initGui();
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }

    private void initGui() {
        setLayout(new FlowLayout(FlowLayout.CENTER));
        setOpaque(false);

        final JPanel content = new JPanel(new BorderLayout(4, 4));
        content.setOpaque(true);
        content.setBorder(new EmptyBorder(8, 8, 8, 8));

        JLabel top = new JLabel("Title of this little modal dialog");
        content.add(top, BorderLayout.NORTH);

        JPanel inner = new JPanel(new BorderLayout());
        content.add(inner, BorderLayout.CENTER);

        inner.add(new JScrollPane(new JList(new String[] {
            "Item 1                                        ", 
            "Item 2", "Item 3"
        })));

        Box ctrlButtons = Box.createHorizontalBox();
        ctrlButtons.setBorder(new EmptyBorder(0, 4, 4, 4));
        ctrlButtons.add(Box.createHorizontalGlue());
        ctrlButtons.add(new JButton(new AbstractAction("OK") {

            @Override
            public void actionPerformed(ActionEvent e) {
                parentFrame.getGlassPane().setVisible(false);
                parentFrame.setGlassPane(new JPanel());
            }
        }));
        content.add(ctrlButtons, BorderLayout.SOUTH);

        add(content);
    }

    public void display() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                parentFrame.setGlassPane(GlassPaneSSCE.this);
                parentFrame.getGlassPane().setVisible(true);

                // Set the focus on the glass pane
                requestFocus();
                setFocusCycleRoot(true);
            }
        });

        // The next line should be executed only if
        // the ok button is clicked and not before
        System.out.println("End of display()");
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame();
        f.setLayout(new FlowLayout(FlowLayout.CENTER));
        JTextArea tp = new JTextArea(10, 10);

        for (int i = 0; i < 20; i++) {
            JButton b = new JButton("Open");
            b.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    GlassPaneSSCE gp  = new GlassPaneSSCE(f);
                    gp.display();
                }
            });
            f.add(b);

            tp.append("Item " + (i+1) + "\n");
        }
        f.add(new JScrollPane(tp));
        f.setSize(600, 600);
        f.setVisible(true);
    }

}
mythbu
  • 657
  • 2
  • 7
  • 16
  • Basically, you can't (very easily). You're best option is to rely on the return result of the glasspane via a listener then waiting for it to return from the "show" method – MadProgrammer Mar 15 '13 at 08:36
  • That's not nice - all the listeners blow up the code so much ... – mythbu Mar 15 '13 at 08:38
  • Not nearly as much as trying to block the currently running Event Queue and having to basically create your and manage your own... – MadProgrammer Mar 15 '13 at 08:39

0 Answers0