Java - JComboBox con ArrayList o Map

 
Vista:
sin imagen de perfil

JComboBox con ArrayList o Map

Publicado por Gabriela (1 intervención) el 13/08/2016 20:00:16
Estoy realizando un proyecto, en Ingresar Pedido quiero utilizar un Map en los Pedidos y Pais, como hago para que la lista que muestra el JComboBox tome todos los datos de mi Map?


Sin-titulo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

JComboBox con ArrayList o Map

Publicado por Esmeralda (10 intervenciones) el 15/08/2016 20:01:26
Hola podrias utilizarlo de la siguiente forma
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
 
public class ComboBoxItem extends JFrame implements ActionListener
{
    public ComboBoxItem()
    {
        Vector model = new Vector();
        model.addElement( new Item(1, "car" ) );
        model.addElement( new Item(2, "plane" ) );
        model.addElement( new Item(3, "train" ) );
        model.addElement( new Item(4, "boat" ) );
 
        JComboBox comboBox;
 
        //  Easiest approach is to just override toString() method
        //  of the Item class
 
        comboBox = new JComboBox( model );
        comboBox.setDragEnabled(true);
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.NORTH );
 
        //  Most flexible approach is to create a custom render
        //  to diplay the Item data
 
        comboBox = new JComboBox( model );
        comboBox.setDragEnabled(true);
        comboBox.setRenderer( new ItemRenderer() );
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.SOUTH );
    }
 
    public void actionPerformed(ActionEvent e)
    {
        JComboBox comboBox = (JComboBox)e.getSource();
        Item item = (Item)comboBox.getSelectedItem();
        System.out.println( item.getId() + " : " + item.getDescription() );
    }
 
    class ItemRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index,
                isSelected, cellHasFocus);
 
            if (value != null)
            {
                Item item = (Item)value;
                setText( item.getDescription().toUpperCase() );
            }
 
            if (index == -1)
            {
                Item item = (Item)value;
                setText( "" + item.getId() );
            }
 
 
            return this;
        }
    }
 
    class Item
    {
        private int id;
        private String description;
 
        public Item(int id, String description)
        {
            this.id = id;
            this.description = description;
        }
 
        public int getId()
        {
            return id;
        }
 
        public String getDescription()
        {
            return description;
        }
 
        public String toString()
        {
            return description;
        }
    }
 
    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxItem();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
     }
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

JComboBox con ArrayList o Map

Publicado por Jacobo (1 intervención) el 15/08/2016 22:59:23
Hola.

Para llenar un JComboBox con arraylist o Map:
Lo puedes hacer de la siguiente manera:
Ejemplo ya tienes tu lista con los datos a llenar.

1
2
3
4
5
6
7
8
9
10
11
ArrayList<String> lista = new ArrayList<>();
lista.add("America Norte");
..................................
....................
 
//Nececitamos un for para llenarlo
for(String campos : lista) {
   miCombo.addItems(campos);
}
 
//Ya despues puedes manejar el evento del JComboBox y hacer lo que necesitas con el.

Ahora si quieres manejarlo con un Map.
Un Map tiene un llave y valor.
La llave no pude ser repetida ya que es unica y no admite que coloques varias iguales.
Ejemplo sencillo, puedes ocupar cualquier tipo ya sea String, Integer, Double, Boolean, Short:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Map<Integer, String> mapa = new HashMap<>();
mapa.put(1, "Dulce");
mapa.put(2, "Amargo);
..............................

// Y tambien nececitamos un for para incluir tus valores en el comboBox

for (Map.Entry<Integer, Integer> entry : mapa.entrySet()) {
    System.out.println("LLave = " + entry.getKey() + ", Valor = " + entry.getValue());
    //Aqui llenas tu JComboBox
    miCombo.addItem(entry.getValue());
}
 
//Si estas ocupando Jdk 8 puedes hacerlo de esta manera.
 
mapa.forEach((k,v)->{
	 System.out.println("LLave = " + k + ", Valor = " + v);
    //Aqui llenas tu JComboBox
    miCombo.addItem(v);
});

Saludos!!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar