Leg*_*les 4 java swing arraylist jcombobox
我有一个奇怪的(在我看来)问题.我正在为自定义渲染器我需要的一些自定义功能制作一个自定义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);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误是在第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;
}
Run Code Online (Sandbox Code Playgroud)
我也奇怪地在我的isRowSelected(int i)函数中收到以下警告:"对java.util.Collection.contains的疑问调用:预期类型字符串,实际类型字符串".
再说一次,我不明白为什么我会收到警告.它似乎是完全有效的代码,我以前做过类似的事情.任何帮助这个奇怪的警告和错误,将不胜感激.
编辑:更改声明如下:
public class CustomComboBoxModel extends AbstractListModel<String>
Run Code Online (Sandbox Code Playgroud)
代替:
public class CustomComboBoxModel<String> extends AbstractListModel<String>
Run Code Online (Sandbox Code Playgroud)
似乎已经摆脱了警告和错误.我不明白为什么,因为我有一个自定义JList模式声明如下:
public class CustomListModel<String> extends javax.swing.DefaultListModel<String>
Run Code Online (Sandbox Code Playgroud)
在同一个包中没有错误.
Mar*_*ers 11
在这一行你已经声明了一个名为的参数String:
public class CustomComboBoxModel<String> extends AbstractListModel<String> {
^^^^^^
Run Code Online (Sandbox Code Playgroud)
当您稍后引用时String,编译器认为您的意思是类型参数,而不是 java.lang.String类.
这里的解决方案是删除类型参数,因为您不需要类型参数.
以下简化示例给出了类似的错误消息:
class Foo<String>
{
String s = ""; // error: incompatible types
}
Run Code Online (Sandbox Code Playgroud)
完整的错误消息是:
Main.java:3: incompatible types
found : java.lang.String
required: String
String s = "";
^
在线查看:ideone
| 归档时间: |
|
| 查看次数: |
2995 次 |
| 最近记录: |