无法将DefaultTableModel设置为JDialog中包含的JTable [英] Unable to set a DefaultTableModel to a JTable contained in a JDialog

查看:145
本文介绍了无法将DefaultTableModel设置为JDialog中包含的JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

分配表模型的代码:

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

}

我用来分配表模型的代码行可以很好地工作,例如,如果它的JTable包含在JFrame中,但是在JDialog中将不起作用.我需要将该表放入JDialog的原因是,当用户在JDialog中选择一个值然后在主应用程序中使用时,我需要暂停主应用程序.我发布了与此有关的另一个问题,以前我曾尝试为此使用JFrame,但这并不是我需要的方法.我将保留链接以供参考. 在新JFrame之后继续执行代码创建并关闭

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

推荐答案

看起来您有一个大小为11的列名称数组,但是您的getColumnCount方法返回了12.

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

这篇关于无法将DefaultTableModel设置为JDialog中包含的JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆