JDK-4223624 : JOptionPane too modal!!
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-03-24
  • Updated: 2000-02-18
  • Resolved: 2000-02-18
Related Reports
Duplicate :  
Description

Name: krT82822			Date: 03/24/99


If you use JOptionPane with two JFrame or JApplet with one
of the static methods like showMessageDialog you have the
modal effect on all frames.

This example create two JFrame with a JTextField and
a JTextArea inside. I've attached also a listener to monitor
the keyboard action. The effect is this:

All frames appear. The first frame create an option message with
its istance. But all seems modal. But if you press the icon "Frame2" on the 
window bar you can go inside the Frame2 and write all you want.

So the wrong effect is: if you have a lot of opened frames all
are in a modal form also if you create the option message with
the istance of one only. But you can write inside the other
frames using the window bar.

This is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.*;

public class prova
{
	public prova ()
	{
		JFrame f = new JFrame("Frame");
		JFrame f1 = new JFrame("Frame2");
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(3,0));
		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(3,0));
		
		JTextField tx = new JTextField();
		listener l = new listener();

		JTextArea txa = new JTextArea();
//		txa.addFocusListener(l);
		txa.addKeyListener(l);
		tx.addKeyListener(l);

		JTextField tx1 = new JTextField();
		listener l1 = new listener();

		JTextArea txa1 = new JTextArea();
//		txa.addFocusListener(l);
		txa1.addKeyListener(l);
		tx1.addKeyListener(l);

		p.add(tx);
		p.add(txa);
		
		p1.add(tx1);
		p1.add(txa1);
		
		f.getContentPane().add(p);
		f.pack();
		f.setVisible(true);
		
		f1.getContentPane().add(p1);
		f1.pack();
		f1.setVisible(true);
			
		
		JOptionPane.showMessageDialog(f,"Try","Can you press the other frame?",JOptionPane.ERROR_MESSAGE);
	
	}
	
	public static void main(String args[])
	{
		prova prv = new prova();		
	}
	
	class listener extends KeyAdapter implements FocusListener
	{
		public void keyPressed(KeyEvent e)
		{
		//Invoked when a key has been pressed.
		System.out.println("keyPressed");
			if (e.getKeyCode() == KeyEvent.VK_TAB)
			{
				System.out.println("TAB");
				e.setModifiers(InputEvent.CTRL_MASK);
				//e.consume();
			}
		}
		
		public void keyReleased(KeyEvent e) 
		{
		//Invoked when a key has been released.
		System.out.println("keyReleased");
		}
		
		public void keyTyped(KeyEvent e) 
		{
		//Invoked when a key has been typed.
		System.out.println("keyTyped");
		}

		public void focusGained(FocusEvent fe)
		{
			System.out.println("GAINED :"+fe);
		}
	
		public void focusLost(FocusEvent fe)
		{
			System.out.println("LOST :"+fe);
		}
	}
}
(Review ID: 56023)
======================================================================

Comments
EVALUATION Swing's OptionPane modality is dependent on AWT's Dialog modality, which is defined to be an app-modal style modality: * A dialog can be either modeless (the default) or modal. A modal * dialog is one which blocks input to all other toplevel windows * in the app context, except for any windows created with the dialog * as their owner. So this is not a bug, but as-designed. That stated, it would be desirable to have frame-style modality as an option, which is an existing AWT rfe: 4080029. amy.fowler@Eng 2000-02-17
17-02-2000