-1

I would like to change this code to display only "OK" and delete the cancel button.

Object contestacion5 = JOptionPane.showInputDialog(null, "#5 Que describe mejor a la Norteña?", "Examen Tijuanas PR", //3
            JOptionPane.DEFAULT_OPTION, null,
            new Object[] {"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." }, null);

http://i.snag.gy/6nSlc.jpg Here it is the picture, I want it exactly as this but without the Cancel button, thanks!

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Duplicate: http://stackoverflow.com/questions/16511039/is-there-a-way-to-only-have-the-ok-button-in-a-joptionpane-showinputdialog-and –  Sep 22 '13 at 17:31
  • You are welcome... don't you want us to do anything else more for you? http://whathaveyoutried.com . Check `JOptionPane` javadoc. – SJuan76 Sep 22 '13 at 17:31
  • @SJuan76 Just in case you are blind, he showed us exactly what he tried. – mavroprovato Sep 22 '13 at 17:34
  • 2
    @mavroprovato no, he just pasted some code from elsewhere and told us "I want this". If he had written that code, he would have read the javadoc and would already have found the solution himself. – SJuan76 Sep 22 '13 at 17:38
  • Read the Swing tutorial on [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) for plenty of examples. – camickr Sep 22 '13 at 18:17
  • I dont understand the post linked by Oliver Benns, so if you can change it for me with my code it would be great!! – Emmanuel Urias Sep 22 '13 at 18:51
  • @SJuan76 and mavroprovato, this isnt a code of "elsewhere", this is written by me, and I still havent find out what to do, sorry for not being a newbie, Im kinda new to programming. – Emmanuel Urias Sep 22 '13 at 18:54
  • @mavroprovato ........ – Emmanuel Urias Sep 22 '13 at 18:55
  • `I dont understand the post linked by Oliver Benns` - that is why I gave you the link to the Swing tutorial. It contains examples with explanations. Read all the suggestions. `Im kinda new to programming.` - again that is why I gave you the tutorial link. It contains the basics for using Swing code along with all kinds of examples. Read the tutorial and ask specific questions when there is something you don't understand. – camickr Sep 22 '13 at 22:30

1 Answers1

0

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:

  1. return type String instead of int.
  2. return null instead of -1 when the user cancelled it
  3. return answers[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.",
});
Boann
  • 48,794
  • 16
  • 117
  • 146
  • thanks for not being such di*k and answering my questions not posting some lazy tutorial that Ive read before doing this damn post. Could you please edit my post to work like you say? I still dont manage to do it by myself. – Emmanuel Urias Sep 24 '13 at 01:13
  • @EmmanuelUrias I've added to my answer. Are you able to use it now? – Boann Sep 24 '13 at 01:33
  • I still dont figure it out, it keeps asking me to initialize those variables in the main, as I am creating a new method getShowOptions() – Emmanuel Urias Sep 24 '13 at 01:57
  • @EmmanuelUrias Nope, I haven't the faintest idea what you're talking about. – Boann Sep 24 '13 at 02:09
  • Is there a way here you can PM me? @Boann – Emmanuel Urias Sep 24 '13 at 02:11
  • Nope. Post your problematic code in a comment, or edit your original question, or post a new question, or post your **ACTUAL** error messages instead of paraphrasing, or something. – Boann Sep 24 '13 at 02:27
  • ive just copied your code and pasted in here and started editing it, but without success. – Emmanuel Urias Sep 24 '13 at 02:34
  • 'Object contestacion1 = JOptionPane.showInputDialog(null, "#1 Que describe mejor a la Caldo Ranchero?", "Examen Tijuanas PR", //2 JOptionPane.DEFAULT_OPTION, null, new Object[] {"Sopa lechuga, tomate, cebolla, aguacate, salsa chipotle y limon.", "Sopa de lechuga, tomate, cebolla, aguacate y limon.", "Sopa de cebolla, pimientos, arroz, lechuga, tomate y aguacate" }, null); String respuesta1 = "Sopa de lechuga, tomate, cebolla, aguacate y limon." ; if (contestacion1 == respuesta1) { buenas++; } else { malas++; }' – Emmanuel Urias Sep 24 '13 at 02:40
  • thats actually how I have it, I didnt managed to use yours or put it to work, check out if u can edit it. – Emmanuel Urias Sep 24 '13 at 02:41
  • I've already been as specific as possible. I'm sorry but if you don't have *any* programming experience at all I have to wonder why someone gave you this job to do. If it's your own job, you ought to hire an actual programmer from a website like freelancer.com. If you really expect people to babysit you through every tiny step for free, you need to follow their instructions. For the third time, **what is the actual error message?** – Boann Sep 24 '13 at 02:47