Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify toString() method JComboBox Swing

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.

like image 278
Filowk Avatar asked Feb 11 '13 18:02

Filowk


People also ask

What is JComboBox in Java Swing?

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 .

Which method is used to add items to JComboBox?

Creates a JComboBox with a default data model. The default data model is an empty list of objects. Use addItem to add items.

How do I make JComboBox not editable?

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.

Which is the superclass for JComboBox?

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.


2 Answers

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.

like image 200
Robin Avatar answered Sep 27 '22 21:09

Robin


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() );
like image 43
Gnanasekaran Palanisamy Avatar answered Sep 27 '22 21:09

Gnanasekaran Palanisamy