簡體   English   中英

在JTable中添加JCombobox-ArrayIndexOutOfBoundException

[英]Adding JCombobox in JTable - ArrayIndexOutOfBoundException

我想將JComboBox放在JTable的最后一列中,以供用戶選擇否。 需要的房間數。

密切關注了教程

但是,當我實現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)

首先初始化並在桌子上設置模型。 然后,您可以訪問第4列。 在設置模型之前,表模型沒有任何列-0,因此您將獲得ArrayIndexOutOfBoundsException 在設置模型之前,表將使用具有0列和0行的默認模型。

例如:

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