开发者

getDataVector gives various data types from TJable

I have a JTable and when I use jTable1.getModel()).getDataVector() objects from different columns have different types (they should be all Strings for my case)

My table has five columns:

  1. number (it's String actually, but I have no problem with parsing it)

  2. String chosen from combobox attached to cell, but when I want to take value from it it appears to be Vector, not a String

  3. again number (like in 0 column - no problem here)

  4. again String with combobox, but gives me String value, so no problem

  5. number (hidden in String) but again this should be String but is Vector

how I create comboboxes in cells (column 1 and 3):

    TableColumn column = jTable1.getColumnModel().getColumn(3);
    JComboBox cb = new JComboBox(ServerIntf.DataModificationType.getStringList().toArray());
    column.setCellEditor(new DefaultCellEditor(cb));

how I create table model:

   public TableModel getTableModel() {
    Vector<Vector<String>> data = task.getDataSet(dataName);
    ServerIntf.SimpleDataType sdt = Manager.manager.getSimpleDataType(task, dataName);
    rowsMin = sdt.getRowMin();
    rowsMax = sdt.getRowMax();
    columnsMin = sdt.getColMin();
    columnsMax = sdt.getColMax();
    if (data != null) {
        //nothing important here, it doeasn't come into this part anyway
    }
    int cmax = 5;
    int rmax = 1;
    Vector columns = new Vector(cmax);
    columns.addAll(Arrays.asList(new String[]{"ID", "Przekształcenie", "Ile razy", "Co z danymi", "Co dalej"}));

    return new DefaultTableModel(columns, rmax);
}

And this is how I read data:

  public Algor开发者_C百科ythmTable(List get) {
    steps = new Vector<Step>();
    for (List<String> row : (List<List<String>>) get) {
        steps.add(new Step(row));
    }
    //boring here
}
  //...
    public Step(List list) {
        id = Integer.parseInt((String) list.get(0));
        matrix = ((String) ((Vector) list.get(1)).get(0)).isEmpty() ? null : ((String) ((Vector) list.get(1)).get(0));
        count = ((String) list.get(2))==null || ((String) list.get(2)).isEmpty() ? 0 : Integer.parseInt((String) list.get(2));
        after = ((String) list.get(3)).isEmpty() ? null : ServerIntf.DataModificationType.getByName((String) list.get(3));
        nextStep = ((String) list.get(4))==null || ((String) list.get(4)).isEmpty() ? -1 : Integer.parseInt(((String) list.get(1)));
    }

I get CastException at the last line (nextStep = ...)

  Exception occurred during event dispatching:
  java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.String

I was getting the same exception at the line (matrix = ...) but as you can see I casted it differently and now it seems to work fine. I thought at first that the problem was connected to the comboboxes in cells, but it also occurs in column where there is simple String inserted by the user.

Well I can simply do another casting but it bothers me, because it should work without it (didn't have that problem in different kinds of JTables), this makes code hard to read and if will ever want to expand this table with more columns (which might happen in near future) and I will struggle again with the same problem.

Does anyone have any idea, why does it work like that and if there's any way of forcing jtable (or model) to give me values of cells in unified format?


Instead of coercing your List<List<String>> into a DefaultTableModel, it may be more maintainable to extend AbstractTableModel, as suggested in Creating a Table Model. In particular, you override getColumnClass() to specify the precise type of each datum. This has the direct benefit of enabling several default Editors and Renderers. There's a related example here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜