【问题标题】:Unable to set a DefaultTableModel to a JTable contained in a JDialog无法将 DefaultTableModel 设置为 JDialog 中包含的 JTable
【发布时间】:2015-10-15 18:01:02
【问题描述】:

我创建了一个包含 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

【问题讨论】:

    标签: java jtable jdialog defaulttablemodel abstracttablemodel


    【解决方案1】:

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

    【讨论】:

    • 哇,我觉得自己好蠢!哈哈哈谢谢好心的陌生人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 2012-05-07
    • 2018-03-26
    • 1970-01-01
    相关资源
    最近更新 更多