Unable to set a DefaultTableModel to a JTable contained in a JDialog

徘徊边缘 提交于 2019-12-25 19:37:24

问题


I have created a JDialog that contains a JTable, when I try to assign a DefaultTableModel to it, it gives me an exception and the JDialog does not even appear. java.lang.ArrayIndexOutOfBoundsException: 11.

Code to assign the table model:

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

My AbstractTableModel class:

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];
    }

}

The line of code I use to assign the table model works fine for example if its a JTable contained in a JFrame, but it will not work in a JDialog. The reason I need this table to be in a JDialog is because I need to main app to be halted while the user selects a value in the JDialog to then be used in the main app. I posted another question relating to this, I was previously trying to use a JFrame for this but that was not the way to go for what I need. I'll leave the link for reference. Continue code execution after new JFrame is created and then closed


回答1:


It looks like you have an array of column names with size of 11, but your getColumnCount method returns 12.



来源:https://stackoverflow.com/questions/33155416/unable-to-set-a-defaulttablemodel-to-a-jtable-contained-in-a-jdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!