Jcombobox inside a Joptionpane

Asked

Viewed 821 times

0

Good morning, I have a list of some reasons

 private List<Motivo> motivos;

And I need to fill a jComboBox with this list and get the selected option.

Currently I did with Messagedialog where I can fill normally but I can not get the selected item, follow example:

 JComboBox jcb = new JComboBox();
    for (Motivo motivos_bloqueia : motivos) {
        jcb.addItem(motivos_bloqueia.getMotivo());

    }

JOptionPane.showMessageDialog(null, jcb, "Selecione o motivo", JOptionPane.QUESTION_MESSAGE);

But this way I can’t get the selected item.

I read in some forums where I was directed to use an Inputdialog instead of Messagedialog, as follows:

Object[] opcoes = {"Um","Dois","Tres","Quatro"};
Object res = JOptionPane.showInputDialog(null, "Escolha um item" , "Selecao
de itens" ,
JOptionPane.PLAIN_MESSAGE , null ,opcoes,"");

That way I would have the item selected in the variable res. The problem is that the parameter it passes to the combobox to add the items in the options is an Object[] and in the case what I have is a List.

How I can get the item that the user selected in the combobox by clicking OK?

  • Take a look now @diegofm

  • I made an example just like yours and there was no problem. Be more specific where the problem is occurring, the code reported is not wrong.

  • I tried to edit it, I don’t know if it helped. Being more direct to the point, I need to GET the user-selected item in jComboBox by clicking OK on my Joptionpane. I only managed to do this using showInputDialog by passing the items to the combo box as Object[], but I have a List<Reasons> and can’t use Inputdialog with it.

  • 1

    This approach is not very good, Joptionpane was made for simpler tasks, that you are trying to do, it would be the case to use a screen with Jframe and add the correct components in it. The return of Joptionpane is for less complex and simple things, where building a screen would cause more work than using this component.

  • 1

    Imagine if every thing you need to get from the user to do a Joptionpane? Your application will get extremely tiring to handle. Sometimes it is better to make a small screen and put everything there for the user to do everything q have to do and then you just treat his entries at once.

  • 1

    It really is a rather 'strange' approach. I wanted to avoid creating a new screen and do with Joptionpane because I would only have a jCombobox inside, let’s say that is not the correct method to be done hehe, I will replace. Thank you!

Show 1 more comment

2 answers

2


A JOptionPane does not return the contents of a component inserted in it, as a combobox, since each component already has all the methods necessary for its content to be manipulated. In the case of JComboBox, I believe there is a mistake, you will not get the item selected by JOptionPane, you have to call getSelectedItem() of the component itself to get this. See an example below, using your code:

JComboBox jcb = new JComboBox();

for (int i = 0; i < 6; i++) {
    jcb.addItem("motivo 0" + (i + 1));
}

JOptionPane.showMessageDialog(null, jcb, "Selecione o motivo", JOptionPane.QUESTION_MESSAGE);

JOptionPane.showMessageDialog(null, jcb.getSelectedItem(), "Opção selecionada", JOptionPane.INFORMATION_MESSAGE);
  • I got it, I figured it out with your explanation.

1

The question is not very clear. It seems that you want something to happen by clicking the button. In this case, use OptionDialog, that returns a int and, from the value returned, do something.

    JComboBox jcb = new JComboBox();
        for (Motivo motivos_bloqueia : motivos) {
            jcb.addItem(motivos_bloqueia.getMotivo());

        }

    int selecionado = JOptionPane.showOptionDialog(null, jcb, "Selecione o motivo", JOptionPane.QUESTION_MESSAGE);

    if (selecionado == 1) {
        //faça algo
    }

Note that you can use ENUM if your list of reasons is previously known. Simply assign each ENUM value an equivalent int.

  • He wants to pick up the selected item in the combo, not the return of jOptionpane.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.