2

私はJavaで初めてJTablesとVectorsをいじくり回していて、興味深い問題にぶつかりました。私のコードは正しくコンパイルされますが、実行しようとすると、次の例外が発生します。

スレッド"main"の例外java.lang.ClassCastException:java.lang.Stringをjava.util.Vectorにキャストできません

キャストしているところがどこにも見当たらないので少し戸惑います。

Vector<String> columnNames = new Vector<String>();
columnNames.add("Tasks");

Vector<String> testing = new Vector<String>();
testing.add("one");
testing.add("two");
testing.add("three");

table = new JTable(testing, columnNames); // Line where the error occurrs.
scrollingArea = new JScrollPane(table);

私の目標はJPanelsのテーブルを作成することですが、<taskPanel>のVectorを使用しようとすると、同じタイプのエラーが発生します。JPanelを拡張するクラスは次のとおりです。

class taskPanel extends JPanel
{
    JLabel repeat, command, timeout, useGD;

    public taskPanel()
    {
        repeat = new JLabel("Repeat:");
        command = new JLabel("Command:");
        timeout = new JLabel("Timeout:");
        useGD = new JLabel("Update Google Docs:");

        add(repeat);
        add(command);
        add(timeout);
        add(useGD);
    }
}
4

3 に答える 3

3

各行にはすべての列のデータが含まれているtestingはずなので、ベクトルは次のようになります。vector of vectors

    Vector<Vector> testing = new Vector<Vector>();
    Vector<String> rowOne = new Vector<String>();
    rowOne.add("one");
    Vector<String> rowTwo = new Vector<String>();
    rowTwo.add("two");
    Vector<String> rowThree = new Vector<String>();
    rowThree.add("three");

    testing.add(rowOne);
    testing.add(rowTwo);
    testing.add(rowThree);

    table = new JTable(testing, columnNames); // should work now
    scrollingArea = new JScrollPane(table);
于 2012-10-26T20:10:06.407 に答える
2

ここでのを使用する必要がVectorありVectorsます:

Vector<Vector> rowData = new Vector<Vector>();
rowData.addElement(testing);

JTable table = new JTable(rowData, columnNames); 

複数列のVectorテーブルモデルについては、このを参照してください。

于 2012-10-26T20:10:20.533 に答える
0

キャストは<文字列>です。現在、ベクター文字列を使用することはできません。これを見てください。

于 2012-10-26T20:06:50.953 に答える