无法将DefaultTableModel设置为JDialog中包含的JTable

时间:2015-10-15 18:01:02

标签: java jtable jdialog defaulttablemodel abstracttablemodel

我创建了一个包含JTable的JDialog,当我尝试为它分配一个DefaultTableModel时,它给了我一个例外,甚至没有出现JDialog。 java.lang.ArrayIndexOutOfBoundsException: 11

分配表模型的代码:

jTable1.setModel(new BomModel(GetBomForSetupMaterial.getPartPositionList()));

我的AbstractTableModel课程:

public class BomModel extends AbstractTableModel {

    private static List<JPartPosition> partPositionList = new ArrayList<JPartPosition>();

    private String[] columnNames = {"Part Header ID", "Mounting Place", "Part Number",
        "Component Type", "Description", "PCB Layer ID", " Processing Type ID", "Component Quantity", "Quantity Unit ID", "Mounting Place Related Machine Group ID", "Componen Setup"};

    public BomModel() {
    }

    public BomModel(List<JPartPosition> positionList){
        this.partPositionList = positionList;

    }

    @Override
    public int getRowCount() {
        return partPositionList.size();
    }

    @Override
    public int getColumnCount() {
        return 12;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        Object value = "??";
        JPartPosition jpart = partPositionList.get(rowIndex);
        switch (columnIndex) {
            case 0:
                value = jpart.getPartHeaderId();
                break;
            case 1:
                value = jpart.getMountingPlace();
                break;
            case 2:
                value = jpart.getPartNumber();
                break;
            case 3:
                value = jpart.getComponentType();
                break;
            case 4:
                value = jpart.getDescription();
                break;
            case 5:
                value = jpart.getPcbLayerId();
                break;
            case 6:
                value = jpart.getProcessingTypeId();
                break;
            case 7:
                value = jpart.getComponentQuantity();
                break;
            case 8:
                value = jpart.getQuantityUnitId();
                break;
            case 9:
                value = jpart.getMountingPlaceRelatedMachineGroupId();
                break;
            case 10:
                value = jpart.getComponentSetup();
                break;
                //do i need the ID???////////////////
        }

        return value;
    }

        public JPartPosition getMatAt(int row) {
        return partPositionList.get(row);
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

}

我用来分配表模型的代码行很好,例如,如果它是一个包含在JFrame中的JTable,但它在JDialog中不起作用。我需要这个表在JDialog中的原因是因为我需要在用户在JDialog中选择一个值然后在主应用程序中使用时停止主应用程序。我发布了另一个与此相关的问题,我之前尝试使用JFrame,但这不是我需要的方法。我将保留链接以供参考。 Continue code execution after new JFrame is created and then closed

1 个答案:

答案 0 :(得分:0)

看起来你有一个大小为11的列名数组,但你的getColumnCount方法返回12。