在JTable中添加JCombobox - ArrayIndexOutOfBoundException

时间:2014-08-02 17:41:17

标签: java swing jtable jcombobox tablecelleditor

我想将JComboBox放入JTable的最后一列,供用户选择no。 需要的房间。

我遵循了tutorial closely

但是,当我实现getColumnModel()时似乎存在问题。 以下代码导致java.lang.ArrayIndexOutOfBoundsException:4> = 0.

//This is in the main form.

table = new JTable();
setUpRoomColumn(table, table.getColumnModel().getColumn(4)); 

附加代码

 public void setUpRoomColumn(JTable table,TableColumn roomColumn) {
     //Set up the editor for the sport cells.
     JComboBox comboBox = new JComboBox();
     comboBox.addItem("0");
     comboBox.addItem("1");
     comboBox.addItem("2");
     comboBox.addItem("3");
     comboBox.addItem("4");
     comboBox.addItem("5");


     roomColumn.setCellEditor(new DefaultCellEditor(comboBox));

    }

    private void displayAvailableRooms(ArrayList<Rooms> rList) {    
    FinalRoomsModel finalModel = new FinalRoomsModel(rList);
    table.setModel(finalModel);
}

我还创建了一个扩展AbstractTableModel的单独的表模型类。

public class FinalRoomsModel extends AbstractTableModel{

private static final long serialVersionUID = 1L;
private int rowCount, colCount;
private String[] columnNames = {"Room Type", "Room Desc", "Max Guests", "Room   
Rate","No.Of Rooms"};
private Object [][] data;

public FinalRoomsModel(ArrayList<Rooms> listOfObjects) {
    // TODO Auto-generated constructor stub
     rowCount = listOfObjects.size();
        colCount = columnNames.length;
        data = new Object[rowCount][colCount];
        for (int i = 0; i < rowCount; i++) {
           /*Copy an ArrayList element to an instance of MyObject*/
            Rooms e1 = (Rooms)(listOfObjects.get(i)); 

            data[i][0] = e1.getType();
            data[i][1] = e1.getDesc();
            data[i][2] = e1.getMaxOccupancy();
            data[i][3] = e1.getPrice();
            data[i][4] = String.class; //combobox??
        }
    } 


public int getColumnCount() {
    // TODO Auto-generated method stub
    return colCount;
}
@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return rowCount;
}

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

public Class getColumnClass(int column) {
    switch (column) {
        case 0:
            return String.class;
        case 1:
            return String.class;
        case 2:
            return int.class;
        case 3:
            return int.class;
        case 4:
            return (String.class);


        default:
            return (getValueAt(0, column).getClass());
    }
}

//Allow fourth column to be editable
public boolean isCellEditable(int rowIndex, int columnIndex)
{

    if(columnIndex == 4){
        return true;
    }

    else
        return false;

}

public Object getValueAt(int rowIndex, int columIndex) {
    return data[rowIndex][columIndex];
}

我该如何处理这个问题?

异常

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at HotelReservation.ui.FinalRooms.<init>(FinalRooms.java:132)
at HotelReservation.ui.CheckAvailability$1.actionPerformed(CheckAvailability.java:202)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:2)

首先在表格上初始化并设置模型。然后,您可以访问第4列。在设置模型之前,表模型没有列 - 0,因此您得到ArrayIndexOutOfBoundsException。在设置模型之前,该表使用具有0列和0行的默认模型。

例如:

JTable table = new JTable();
FinalRoomsModel model = new FinalRoomsModel();
table.setModel(model);
setUpRoomColumn(table, table.getColumnModel().getColumn(4));