我有一个奇怪(在我看来)的问题。我目前正在制作一个自定义的JComboBoxModel,以满足我从一些自定义渲染器中需要的自定义功能,但我还不确定如何处理它,稍后我会发布相关内容。
无论如何,正如标题所示,我遇到了一些类型错误。
这是该类(当前)的代码:
package miscclasses;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
public class CustomComboBoxModel<String> extends AbstractListModel<String> {
/**
* Contains a list of row indexes that shouldn't be displayed.
*/
private ArrayList<String> alreadySelectedIndexes;
/**
* The comboBoxes selected index.
*/
private int selectedIndex=-1;
/**
* Contains a list of values in this model.
*/
private ArrayList<String> vals;
/**
* Creates an empty CustomComboBoxModel.
*/
public CustomComboBoxModel() {
this.alreadySelectedIndexes=new ArrayList<>();
this.vals = new ArrayList<>();
}
/**Creates a CustomComboBoxModel with the values passed in.
*
* @param values the row values.
*/
public CustomComboBoxModel(ArrayList<String> values) {
this();
this.vals.addAll(values);
}
public CustomComboBoxModel(ArrayList<String> values, ArrayList<String> alreadySelected) {
this(values);
this.alreadySelectedIndexes.addAll(alreadySelected);
}
public CustomComboBoxModel(ArrayList<String> values, ArrayList<String> alreadySelected, int selectedIndex) {
this(values, alreadySelected);
this.selectedIndex=selectedIndex;
}
public CustomComboBoxModel(ArrayList<String> values, ArrayList<String> alreadySelected, String selectedValue) {
this(values, alreadySelected);
if(!this.vals.isEmpty()) {
//this.selectedIndex = Misc.containsI(this.vals, selectedValue);
}
}
@Override
public int getSize() {
return this.vals.size();
}
@Override
public String getElementAt(int i) {
return this.vals.get(i);
}
public boolean isRowSelected(int i) {
return ((!this.alreadySelectedIndexes.contains(Integer.toString(i)))?false:true);
}
}
我收到的错误在第55行,那里我试图获取所选值的索引。这是在接受两个ArrayList列表和一个String值的构造函数中被注释的行。错误显示“没有适合的方法用于containsI(java.util.ArrayList,String)方法miscclasses.Misc.containsI(java.util.ArrayList,java.lang.String)不适用”。据我所知,String和java.lang.String是完全相同的。因此,我无法弄清楚可能会导致这个问题,想请教那些对Code-fu更有了解的人。供参考,Misc.containsI(ArrayList,String)代码在此处:
/**
* Finds the index of value Target. Returns -1 if this ArrayList doesn't contain Target.
* @param a ArrayList of Strings a.
* @param target Value to find.
* @return Index of target if found; Else returns -1.
*/
public static int containsI(ArrayList<String> a, String target) {
for (int i = 0; i < a.size(); i++)
{
if(a.get(i).equalsIgnoreCase(target))
return i;
}
return -1;
}
我在isRowSelected(int i)函数中奇怪地收到以下警告:
"Suspcious call to java.util.Collection.contains: Expected type String, actual type String"。
再次强调,我不明白为什么会出现这样的警告。这似乎是完全有效的代码,而且我以前也做过类似的事情。如果能帮忙解决这个奇怪的警告和错误,我将不胜感激。
编辑:更改声明以便读取:
public class CustomComboBoxModel extends AbstractListModel<String>
替代:
public class CustomComboBoxModel<String> extends AbstractListModel<String>
看起来已经消除了警告和错误。然而,我不理解为什么会这样,因为我有一个声明为Custom JList mode的自定义JList模式:
public class CustomListModel<String> extends javax.swing.DefaultListModel<String>
在同一个包中没有错误。
String
创建为类的类型参数。你至少应该得到一半的功劳来解决这个问题! - Mark ByersString
,在某些情况下它甚至可能编译并且运行没有问题。问题在于如果你在类内部有类似String s = "";
这样的代码,那么你将会得到一个类型错误,因为字面字符串具有实际的java.lang.String
类型,而不能分配给你的String
类型。 - Mark Byers