Java的Vector和JTable的问题



我第一次在Java中修补JTable和向量,我遇到了一个有趣的障碍。我的代码正确编译正确,但是当我运行它时,我会得到以下例外:

线程" 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表,但是当我尝试使用&lt的向量时,我的错误类型相同。Taskpanel>这是扩展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);
    }
}

您需要在此处使用VectorsVector

Vector<Vector> rowData = new Vector<Vector>();
rowData.addElement(testing);
JTable table = new JTable(rowData, columnNames); 

有关多列Vector表模型,请参见此示例。

您的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);

铸件是&lt;字符串>。目前您无法拥有矢量字符串。看看这个。

最新更新