4

私は奇妙な(私の心の中で)問題を抱えています。現在、カスタムレンダラーから必要ないくつかのカスタム機能用にカスタム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行目にあり、選択した値のインデックスを取得しようとしています。これは、2つのArrayListリストと1つの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)関数で次の警告が表示されます。「java.util.Collection.containsへの疑わしい呼び出し:予期された型String、実際の型String」。

繰り返しますが、なぜ警告が表示されるのかわかりません。それは完全に有効なコードのようです、そして私は以前に同様のことをしました。この奇妙な警告とエラーについての助けをいただければ幸いです。

編集:次のように宣言を変更します:

public class CustomComboBoxModel extends AbstractListModel<String>

それ以外の:

public class CustomComboBoxModel<String> extends AbstractListModel<String>

警告とエラーを取り除いたようです。ただし、カスタムJListモードが次のように宣言されているため、理由がわかりません。

public class CustomListModel<String> extends javax.swing.DefaultListModel<String>

エラーのない同じパッケージ内。

4

1 に答える 1

11

この行では、 という型パラメーターを宣言していますString

public class CustomComboBoxModel<String> extends AbstractListModel<String> {
                                 ^^^^^^

後で参照するとString、コンパイラは、 java.lang.Stringクラスではなく型パラメーターを意味すると考えます。

ここでの解決策は、型パラメーターが必要ないため、型パラメーターを削除することです。


次の簡単な例では、同様のエラー メッセージが表示されます。

class Foo<String>
{
    String s = ""; // error: incompatible types
}

完全なエラー メッセージは次のとおりです。

Main.java:3: 互換性のない型
見つかった: java.lang.String
必須: 文字列
    文字列 s = "";
               ^

オンラインで見る: ideone

于 2012-08-27T20:51:09.017 に答える