K4M1coder
K4M1coder

Reputation: 21

how to sort Java combobox when filled with Vector?

context :

i'm working on an applet witch manage fidelity cards. past version of the app had been done by an other developper. there is no documentation. i have to improve it.

problem :

to reach a customer, the app have some combobox. those combobox are filled by vertors

i try to sort item in the combobox but fail each times

i've read things about Collections.sort(x); where x could be a List or a Vector

but wherever i put the instruction for sorting elements, eclipse mark sort with this error :

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (Vector<NomClient>). The inferred type NomClient is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>

here is the code of the combobox :

    private JComboBox<Object> getComboBox() {
    if (this.comboBox == null) {
        this.comboBox = new JComboBox<Object>();
        this.comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {

                try {
                    SelectionNumeroCarteFidelite2.this.name = SelectionNumeroCarteFidelite2.this.comboBox
                            .getSelectedItem().toString();
                    SelectionNumeroCarteFidelite2.this.mod2 = new DefaultComboBoxModel<Object>(
                            Select.listePrenomclientfidelite(SelectionNumeroCarteFidelite2.this.name));
                    SelectionNumeroCarteFidelite2.this.comboBox_1
                            .setModel(SelectionNumeroCarteFidelite2.this.mod2);
                    SelectionNumeroCarteFidelite2.this.lblTaperOuSlectionner
                            .setVisible(false);
                } catch (final Exception e1) {


                    final String message = "Choix Impossible - Merci de vérifier votre sélection";
                    System.out.print("Nom " + message);
                    final AlerteSelection fenetre = new AlerteSelection(
                            SelectionNumeroCarteFidelite2.this.interfaceactuelle,
                            message);
                    fenetre.setVisible(true);
                    SelectionNumeroCarteFidelite2.this.interfaceactuelle
                            .setEnabled(false);
                    SelectionNumeroCarteFidelite2.this.lblValider
                            .setVisible(false);
                    SelectionNumeroCarteFidelite2.this.lblTaperOuSlectionner
                            .setVisible(true);
                }

            }
        });
        this.comboBox.setEnabled(false);
        this.comboBox.setForeground(Color.GRAY);
        this.comboBox.setFont(new Font("Tahoma", Font.BOLD, 11));
        this.comboBox.setEditable(true);
        this.comboBox.setBorder(null);
        this.comboBox.setBackground(Color.WHITE);
        this.comboBox.setBounds(528, 426, 278, 22);

        this.mod = new DefaultComboBoxModel<Object>(
                Select.listenomclientfidelite());
        this.comboBox.setModel(this.mod);
        AutoCompletion.enable(this.comboBox);

    }
    return this.comboBox;
}

here is the code of the Select.listenomclientfidelite()

public static Object[] listenomclientfidelite() {
    final Vector<NomClient> requete = new Vector<NomClient>();
    try {
        c = Connexion.getCon();
        final String sql = "SELECT DISTINCT NOMCLIENT FROM CARTE_DE_FIDELITE INNER JOIN CLIENT ON CLIENT.IDCLIENT=CARTE_DE_FIDELITE.IDCLIENT";
        preStm = c.prepareStatement(sql);
        rs = preStm.executeQuery();
    } catch (final Exception e) {
        System.out.print("erreur" + e.getMessage());
    }
    try {
        requete.add(null);
        NomClient liste;
        while (rs.next()) {
            liste = new NomClient();
            liste.setNom(rs.getString(1));
            requete.add(liste);
            System.out.println("listenomclientfidelite, liste is : "+liste);
        }
        rs.close();
        preStm.close();
    } catch (final Exception e) {
        System.out.print("errorlistenom" + e.getMessage());
    }

    return requete.toArray(new Object[0]);

After beeing advised by Hovercraft Full Of Eels to modify my class NomClient, i understood that my NomCli class was the problen and not the use of vector, so here is a new step but no solution yet , so here is my Modified NomClient Class :

public class NomClient implements Comparable<NomClient> {

String nom;

public String getNom() {
    return this.nom;
}

public void setNom(final String nom) {
    this.nom = nom;
}

@Override
public String toString() {
    return this.nom;
}

@Override
public int compareTo(NomClient other) {
    System.out.println("nom : "+this.nom);
    System.out.println("nom to string : "+this.nom.toString());
    System.out.println(other.nom);
    System.out.println("compare to : "+other.nom.toString());       
    int last = this.nom.toString().compareTo(other.nom.toString());
    return last == 0 ? this.nom.compareTo(other.nom) : last;
}

}

i also have added Collection sort just before the return statement in sselect.listenomclientfidelite(),

like this :

        Collections.sort(requete);

            return requete.toArray(new Object[0]);

Now i have to deal with a java.lang.NullPointerException. "other" is null

Does any one have a clue to properly sort my combobox ?

Upvotes: 0

Views: 293

Answers (2)

K4M1coder
K4M1coder

Reputation: 21

thank to Hovercraft Full Of Eels his solution was the good one.

i realised the first item of my vector was null it's why the comparable methode failed. so i worked around to handle this case, and here are the final implementation :

for NomClient.java :

public class NomClient implements Comparable<NomClient> {

String nom;

public String getNom() {
    return this.nom;
}

public void setNom(final String nom) {
    this.nom = nom;
}

@Override
public String toString() {
    return this.nom;
}

@Override
public int compareTo(NomClient other) {
    // compareTo should return < 0 if this is supposed to be
    // less than other, > 0 if this is supposed to be greater than
    // other and 0 if they are supposed to be equal
    int last = 10;
    if (other != null){
        last = -10;
        if (this.nom != null){
            last = this.nom.compareTo(other.nom);
            }
    }
    return last;

for Select.listenomclientfidelite()

public static Object[] listenomclientfidelite() {
    final Vector<NomClient> requete = new Vector<NomClient>();
    try {
        c = Connexion.getCon();
        final String sql = "SELECT DISTINCT NOMCLIENT FROM CARTE_DE_FIDELITE INNER JOIN CLIENT ON CLIENT.IDCLIENT=CARTE_DE_FIDELITE.IDCLIENT";
        preStm = c.prepareStatement(sql);
        rs = preStm.executeQuery();
    } catch (final Exception e) {
        System.out.print("erreur" + e.getMessage());
    }
    try {
        requete.add(null);
        NomClient liste;
        while (rs.next()) {
            liste = new NomClient();
            liste.setNom(rs.getString(1));
            requete.add(liste);
            System.out.println("listenomclientfidelite, liste is : "+liste);
        }
        rs.close();
        preStm.close();
    } catch (final Exception e) {
        System.out.print("errorlistenom" + e.getMessage());
    }

    Collections.sort(requete);
    return requete.toArray(new Object[0]);
}

nothing else to do than insert the returned ArrayList in a combobox.

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

If you can't change the NomClient class so that it implements Comparable<NomClient>, meaning that you'd have to give it a public int compareTo(NomClient o) method, then use a Comparator<NomClient> in your sort method call. This is a class that you create that has one method, public int compare(NomClient o1, NomClient o2), and that returns a -1, 0, or 1, depending on if o1 is functionally less than, equal to or greater than the o2 parameter. You would pass your Comparator instance as the 2nd paramter in your Collections.sort(myCollection, myComparator) method call.

Note that your problem has nothing to do with use of a Vector, and all to do with the NomClient class not implementing Comparable.

Upvotes: 4

Related Questions