I would like to change this code to display only \"OK\" and delete the cancel button.
Object contestacion5 = JOptionPane.showInputDialog(null, \"#5 Que descr
I did some experimenting. It's easy enough to use showInputDialog
to show the answers in a dropdown list (combobox) but it does not seem to be possible to remove the Cancel button.
Instead you can use showConfirmDialog
, where the 'message' is not a simple String, but a visual panel containing: (1) a label for the question; (2) a combobox for the answers. I've wrapped this up into a method to make it easier to use:
static int showQuestion(String dialogTitle, String question, String[] answers) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel(question), BorderLayout.NORTH);
JComboBox<String> comboBox = new JComboBox<>(answers);
panel.add(comboBox);
if (JOptionPane.CLOSED_OPTION == JOptionPane.showConfirmDialog(null, panel, dialogTitle, JOptionPane.DEFAULT_OPTION)) {
return -1;
}
return comboBox.getSelectedIndex();
}
Example usage:
int choice = showQuestion("Examen Tijuanas PR", "#5 Que describe mejor a la Norteña?", new String[] {
"Ensalada de espinacas, tomates, zetas, cebolla, tocineta, aguacate, queso de hoja y tiras de maiz crujientes en vinagreta de la casa.",
"Lechuga romana servida con tomate, cebolla, maiz, aguacate, queso de hoja y tiritas de maiz crujientes acompañado de su seleccion de filetes de pollo de res.",
"Ensalada vegetariana de nopales, tomates, cebolla, lechuga romana, queso de hoja, aguacate, y aderezo especial de la casa.",
});
System.out.println("User chose #" + choice);
The showQuestion
method returns the 0-based index of the answer the user chose. The dialog has an 'OK' button but no 'Cancel' button; however, there's still a problem: the user can still close the dialog by clicking the 'X' of the dialog, or by right-clicking the titlebar and selecting 'Close' from the popup menu. That has the same effect as 'Cancel'. So, the code above checks for this, and returns -1
if the user did not make a choice because they closed the dialog somehow without clicking 'OK'.
I can't see an easy way to remove the close button of the dialog. It would be annoying anyway, because it would prevent them from closing the program or cancelling the test. It's best to let the user close/cancel the dialog if they really want to, and handle that situation as appropriate.
Also, it might be more user-friendly to show the choices as radio buttons (these things: (●) A
, ( ) B
, ( ) C
) instead of a dropdown list. That way, the user can read all the choices at once without an extra click. Here's an alternative showQuestion
method which does that, if you want. (It calls the dialog in a loop just in case the user did not select any option before clicking 'OK'.)
static int showQuestion(String dialogTitle, String question, String[] answers) {
Box box = new Box(BoxLayout.Y_AXIS);
box.add(new JLabel(question));
JRadioButton[] radioButtons = new JRadioButton[answers.length];
ButtonGroup buttonGroup = new ButtonGroup();
for (int i = 0; i < answers.length; i++) {
radioButtons[i] = new JRadioButton(answers[i]);
buttonGroup.add(radioButtons[i]);
box.add(radioButtons[i]);
}
for (;;) {
if (JOptionPane.CLOSED_OPTION == JOptionPane.showConfirmDialog(null, box, dialogTitle, JOptionPane.DEFAULT_OPTION)) {
return -1;
}
for (int i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].isSelected()) return i;
}
}
}
Edit: To return the answer directly instead of an index into the array, make a few small changes to the function above:
String
instead of int
.null
instead of -1
when the user cancelled itanswers[comboBox.getSelectedIndex()]
instead of just comboBox.getSelectedIndex()
So it becomes:
static String showQuestion(String dialogTitle, String question, String[] answers) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel(question), BorderLayout.NORTH);
JComboBox<String> comboBox = new JComboBox<>(answers);
panel.add(comboBox);
if (JOptionPane.CLOSED_OPTION == JOptionPane.showConfirmDialog(null, panel, dialogTitle, JOptionPane.DEFAULT_OPTION)) {
return null;
}
return answers[comboBox.getSelectedIndex()];
}
Then, the equivalent of the original snippet is:
Object contestacion5 = showQuestion("Examen Tijuanas PR", "#5 Que describe mejor a la Norteña?", new String[] {
"Ensalada de espinacas, tomates, zetas, cebolla, tocineta, aguacate, queso de hoja y tiras de maiz crujientes en vinagreta de la casa.",
"Lechuga romana servida con tomate, cebolla, maiz, aguacate, queso de hoja y tiritas de maiz crujientes acompañado de su seleccion de filetes de pollo de res.",
"Ensalada vegetariana de nopales, tomates, cebolla, lechuga romana, queso de hoja, aguacate, y aderezo especial de la casa.",
});