Java - Como sobrescribir una subcadena en un JcomboBox

 
Vista:
Imágen de perfil de Kevin

Como sobrescribir una subcadena en un JcomboBox

Publicado por Kevin (2 intervenciones) el 08/04/2017 04:49:44
supongamos que en nuestro combo mostramos los datos concatenados de la siguiente forma : "código-Descripción" como hago que al seleccionar uno de los datos cargados solo me imprima el código en el combo, capturar solo el código no es problema el problema reside en sobrescribir esa cadena en el combo.
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
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Como sobrescribir una subcadena en un JcomboBox

Publicado por Andrés (340 intervenciones) el 08/04/2017 07:49:52
En How to Use Combo Boxes ubicado en: https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#configuring se encuentra el ejemplo: CustomComboBoxDemo.java sólo hay que revisarlo, ver como funciona y usarlo para el propósito deseado.

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.awt.*;
import javax.swing.*;
 
public class CustomComboBoxDemo extends JPanel {
 
    static class VO {
 
        private int id;
        private String description;
 
        public VO(int id, String description) {
            this.id = id;
            this.description = description;
        }
 
        public int getId() {
            return id;
        }
 
        public void setId(int id) {
            this.id = id;
        }
 
        public String getDescription() {
            return description;
        }
 
        public void setDescription(String description) {
            this.description = description;
        }
 
    }
 
    private static final VO[] numeros = {new VO(1, "Impar"), new VO(2, "Par"), new VO(3, "Impar"), new VO(4, "Par"), new VO(5, "Impar"), new VO(6, "Par"), new VO(7, "Impar"), new VO(8, "Par")};
 
    public CustomComboBoxDemo() {
        super(new BorderLayout());
 
        Integer[] intArray = new Integer[numeros.length];
        for (int i = 0; i < numeros.length; i++) {
            intArray[i] = new Integer(i);
 
        }
 
        //Create the combo box.
        JComboBox petList = new JComboBox(intArray);
        ComboBoxRenderer renderer = new ComboBoxRenderer();
        //renderer.setPreferredSize(new Dimension(200, 130));
        petList.setRenderer(renderer);
        petList.setMaximumRowCount(5);
 
        //Lay out the demo.
        add(petList, BorderLayout.PAGE_START);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    }
 
    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
    //Create and set up the window.
        JFrame frame = new JFrame("CustomComboBoxVODemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        JComponent newContentPane = new CustomComboBoxDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
 
    class ComboBoxRenderer extends JLabel
            implements ListCellRenderer {
 
        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(RIGHT);
            setVerticalAlignment(CENTER);
        }
 
        /*
            * This method finds the image and text corresponding
            * to the selected value and returns the label, set up
            * to display the text and image.
         */
        public Component getListCellRendererComponent(
                JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean cellHasFocus) {
            //Get the selected index. (The index param isn't
            //always valid, so just use the value.)
            int selectedIndex = ((Integer) value).intValue();
 
            VO vo = numeros[selectedIndex];
 
            if (isSelected) {
                setText(vo.getId() + "");
                setFont(list.getFont());
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setText(vo.getId() + "-" + vo.getDescription());
                setFont(list.getFont());
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
 
            return this;
 
        }
 
    }
}
Screenshot-from-2017-04-08-00-53-18
Screenshot-from-2017-04-08-00-54-11

Best,
Andrés
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Kevin

Como sobrescribir una subcadena en un JcomboBox

Publicado por Kevin (2 intervenciones) el 08/04/2017 16:15:11
gracias lo revisare
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