上下文:
我正在制作管理保真卡的小程序。该应用程序的过去版本已由其他开发人员完成。没有文件。我必须改进它。
问题:
要联系客户,该应用程序有一些组合框。那些组合框由垂直填充
我尝试对组合框中的项目进行排序,但每次都失败
我读过有关Collections.sort(x)的内容;其中x可以是List或Vector
但无论我把放置元素的指令放在哪里,eclipse标记排序都会出现这个错误:
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>>
这是组合框的代码:
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;
}
这是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]);
在Hovercraft Full Of Eels建议修改我的班级NomClient之后,我明白我的NomCli类是问题,而不是使用向量,所以这是一个新的步骤,但还没有解决方案,所以这里是我的修改NomClient分类:
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;
}
}
我还在sselect.listenomclientfidelite()中的return语句之前添加了Collection sort,
像这样: Collections.sort(requete);
return requete.toArray(new Object[0]);
现在我必须处理java.lang.NullPointerException。 &#34;其他&#34;为空
有没有人能够正确排序我的组合框?
答案 0 :(得分:3)
如果您无法更改NomClient类以使其实现Comparable<NomClient>
,这意味着您必须为其提供public int compareTo(NomClient o)
方法,那么请在排序中使用Comparator<NomClient>
方法调用。这是一个您创建的类,它有一个方法public int compare(NomClient o1, NomClient o2)
,并返回-1,0或1,具体取决于o1是否在功能上小于,等于或大于o2参数。您可以将Comparator实例作为Collections.sort(myCollection, myComparator)
方法调用中的第二个参数传递。
请注意,您的问题 nothing 与使用Vector有关,而且与NomClient类没有实现Comparable有关。
答案 1 :(得分:0)
感谢Hovercraft Full Of Eels他的解决方案很好。
我意识到我的载体的第一项是空的,这就是可比较的方法失败的原因。所以我一直在处理这个案子,这是最后的实施:
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]);
}
除了在组合框中插入返回的ArrayList之外别无其他。