简体   繁体   English

在JTable中添加JCombobox-ArrayIndexOutOfBoundException

[英]Adding JCombobox in JTable - ArrayIndexOutOfBoundException

I want to place a JComboBox into the last column of my JTable for users to select the no. 我想将JComboBox放在JTable的最后一列中,以供用户选择否。 of rooms needed. 需要的房间数。

I followed this tutorial closely 密切关注了教程

However, there seems to be an issue when I implement getColumnModel(). 但是,当我实现getColumnModel()时似乎出现了问题。 The following code result in a java.lang.ArrayIndexOutOfBoundsException: 4 >= 0. 以下代码导致java.lang.ArrayIndexOutOfBoundsException:4> = 0。

//This is in the main form.

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

Additional codes 附加代码

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

I also created a separate table model class that extends AbstractTableModel. 我还创建了一个单独的表模型类,该类扩展了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];
}

How can I handle this problem, please? 请问该如何处理?

Exception 例外

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)

First initialize and set the model on the table. 首先初始化并在桌子上设置模型。 Then you can access the 4th column. 然后,您可以访问第4列。 Until the model is set, the table model has no columns - 0, so you get ArrayIndexOutOfBoundsException . 在设置模型之前,表模型没有任何列-0,因此您将获得ArrayIndexOutOfBoundsException Until the model is set, the table uses a default model that has 0 columns and 0 rows. 在设置模型之前,表将使用具有0列和0行的默认模型。

For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM