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 ?