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;


    public int getRowCount() {
        return partPositionList.size();

    public int getColumnCount() {
        return 12;

    public Object getValueAt(int rowIndex, int columnIndex) {

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

        return value;

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

    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


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