I have a Proveedores class with ID, Name, Lastname and I want add this object into the combobox.
ListIterator listaNombre = listaProveedores.listIterator();
listado = new Proveedores[listaProveedores.size()];
int cont = 0;
while (listaNombre.hasNext()) {
prov = (Proveedores) listaNombre.next();
listado[cont] = prov;
cont++;
}
this.vista.cArticuloFamilia.setModel(new javax.swing.DefaultComboBoxModel(listado));
With this code I add the differents objects into the combobox. It works. But now I want to override the toString method for show only Name attribute. Now combobox shows me the name class (Proveedores) and the ID.
entidades.Proveedores[idProveedores=1]
How can I override it to show the Proveedores Name?
Thanks.
JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .
Creates a JComboBox with a default data model. The default data model is an empty list of objects. Use addItem to add items.
u can make ur jcombobox uneditable by calling its setEnabled(false). A JComboBox is unEditable by default. You can make it editable with the setEditable(boolean) command. If you are talking about enabled or disabled you can set that by the setEnabled(boolean) method.
Other methods you are most likely to invoke on a JComboBox object are those it inherits from its superclasses, such as setPreferredSize . See The JComponent API for tables of commonly used inherited methods.
Use a custom ListCellRenderer to accomplish this.
You shouldn't tailor toString() to produce GUI data for complex objects. It is meant for an internal data representation for the developers eyes, not the users.
Java uses toString() to get the String representation of the Object by default it will return fully qualified classname @ followed by hashCode of the object.
Use ListCellRenderer to display Proveedores Name in the ComboBox.
Sample Code:
public static class ProveedoresRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) {
Object item = value;
// if the item to be rendered is Proveedores then display it's Name
if( item instanceof Proveedores ) {
item = ( ( Proveedores ) item ).getName();
}
return super.getListCellRendererComponent( list, item, index, isSelected, cellHasFocus);
}
}
then set the ProveedoresRenderer to the JComboBox.
ListIterator listaNombre = listaProveedores.listIterator();
listado = new Proveedores[listaProveedores.size()];
int cont = 0;
while (listaNombre.hasNext()) {
prov = (Proveedores) listaNombre.next();
listado[cont] = prov;
cont++;
}
this.vista.cArticuloFamilia.setModel(new javax.swing.DefaultComboBoxModel(listado));
// Set custom renderer to the combobox
this.vista.cArticuloFamilia.setRenderer( new ProveedoresRenderer() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With