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
– Vinicius Leonardo
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.
– user28595
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.
– Vinicius Leonardo
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.
– user28595
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.
– user28595
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!
– Vinicius Leonardo