I want to place a JComboBox into the last column of my JTable for users to select the no. of rooms needed.
I followed this tutorial closely
However, there seems to be an issue when I implement getColumnModel(). The following code result in a 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.
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. Until the model is set, the table model has no columns - 0, so you get ArrayIndexOutOfBoundsException
. Until the model is set, the table uses a default model that has 0 columns and 0 rows.
For example:
JTable table = new JTable();
FinalRoomsModel model = new FinalRoomsModel();
table.setModel(model);
setUpRoomColumn(table, table.getColumnModel().getColumn(4));