United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4813436 JOptionPane.showOptionDialog does not return if "options" is JButton[]
JDK-4813436 : JOptionPane.showOptionDialog does not return if "options" is JButton[]

Details
Type:
Bug
Submit Date:
2003-02-05
Status:
Open
Updated Date:
2013-05-17
Project Name:
JDK
Resolved Date:
Component:
client-libs
OS:
linux
Sub-Component:
javax.swing
CPU:
x86
Priority:
P4
Resolution:
Unresolved
Affected Versions:
1.4.1
Targeted Versions:

Related Reports

Sub Tasks

Description

Name: jk109818			Date: 02/05/2003


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OPERATING SYSTEM VERSION : glibc-2.2-7, Linux 2.2.18,
SuSE Linux 7.1 (i386)


A DESCRIPTION OF THE PROBLEM :
JOptionPane.showOptionDialog() has an argument "options" of
type Object[]. In the API it says:

"options - an array of objects indicating the possible
choices the user can make; if the objects are components,
they are rendered properly; non-String  objects are rendered
using their toString methods; if this parameter is null, the
options are determined by the Look and Feel"

The test case provides three alternatives for options:
1. null,
2. String[]
3. JButton[]

In case of 1 and 2, JOptionPane behaves as expected. In case
of 3 the buttons are rendered properly and the button
specified for initialValue is focused. But obviously it has
no effect to click on any of the buttons. The API of
JOptionPane does not say that and how the custom buttons
should listen to events. Thus, the user expects that
JOptionPane will handle the events and return with the
appropriate value - just as it does in case of 2.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. The test case has three lines that compile only
exclusively. Each of the lines provides the different
"options" and "initialValue" arguments for the JOptionPane.
Uncomment the line with the arguments you want to test and
comment the two other lines.

2. Run the test case with the different "options" arguments.
  To open the option pane, click on the button "JOptionPane".

3. In case of JButton[] as "options" argument the option
pane won't close if any of the buttons is clicked.

EXPECTED VERSUS ACTUAL BEHAVIOR :
If it is possible to specify any type of Object array as
"options" than the API should be clear on the effect this
will have on the behaviour of JOptionPane. So either this is
a bug in the implementation, or in the API documentation. In
case of the latter, some information should be provided on
how to achieve that the option pane returns appropriately.
If this is to complex, there should be a note that the user
should rather construct a custom option pane.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
The error is that the JOptionPane does not return.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/*
 * JOptionPaneTest.java
 */

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

public class JOptionPaneTest
{
	private JFrame f = null;

	public static void main(String[] args)
	{
		new JOptionPaneTest();
	}

	public JOptionPaneTest()
	{
		initComponents();
	}

	private void initComponents()
	{
		f = new JFrame("JOptionPaneTest - dummy frame");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final Container content = f.getContentPane();
		content.setLayout(new GridLayout(0,1));

		content.add(new JLabel("main frame (parent)", SwingConstants.CENTER));

		final JButton invokeButton = new JButton("JOptionPane");
		invokeButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				showDialog();
			}
		});
		content.add(invokeButton);

		f.setSize(200,200);

		// works only with > 1.4
		f.setLocationRelativeTo(null);
		// end > 1.4

		f.setVisible(true);
	}

	private void showDialog()
	{
		final JButton okButton = new JButton("ok (button)");
		final JButton cancelButton = new JButton("cancel (button)");

		final JPanel inputPanel = new JPanel();
		inputPanel.add(new JLabel("input:"));
		inputPanel.add(new JTextField(20));

		int result = JOptionPane.showOptionDialog(f, inputPanel, "test dialog",
		JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
		//new JButton[]{okButton,cancelButton}, okButton);
		new String[]{"ok (string)", "cancel (string)"}, "ok (string)");
		//null, null);

		if (result == JOptionPane.OK_OPTION)
		{
			f.setBackground(Color.BLUE);
		}
		else
		{
			f.setBackground(Color.ORANGE);
		}
	}
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Specify the options as String[].
(Review ID: 178655) 
======================================================================

                                    

Comments
EVALUATION

Components that are passed in through the "options" parameter are not intended to be hooked up automatically by JOptionPane. We should make this clear in the JavaDoc and specify the actual intent of allowing components to be passed in through this array. Also, showing how one might hook up the buttons might be nice (possibly in the Java Tutorial).
###@###.### 2003-02-06

From an e-mail received from ###@###.###:
-----
The rationale for allowing any set of widgets, including buttons, is
for developers to customize the options to do whatever they
want. Perhaps you want a help button, or don't like our ordering, the
options argument give you this flexibility. As it can be any component
we can't possibly figure out how to wire it, and therefor don't do
anything special with them.

I would suggest that we spec out that the dialog is closed when the
'value' property changes. So that if you are adding custom options
that represent an option you'll undoubtedly want to wire this up to
invoke setValue when the user interacts with the widget, which will
trigger the dialog to close I would also suggest we show an example of
this with a help button.
-----
###@###.### 2003-02-06

As an additional note: When the documentation of JOptionPane is updated, I suggest we also update the JavaDoc of JOptionPane.createOptionDialog to specify that the options array parameter can also accept "Icon" objects. Appropriate text is available in the intro JavaDoc to JOptionPane where all parameters are discussed.
###@###.### 2003-02-06
                                     
2003-02-06



Hardware and Software, Engineered to Work Together