Reputation: 89
I am making a theatre seat bookings system for some coursework but am having trouble on creating a JComboBox to help the user select a seat from the set of 197 that there are altogether.
The seats are represented by "Seat" objects which are merely a collection of a few variables such as "isBooked" (Boolean). The seats are arranged into multiple seat arrays, each array represents a row of seats e.g. A[], B[]...
I can easily add a single array to the JComboBox and have it work fine but an attempt to add any more arrays to the list in the JComboBox fails. I can add the seats one at a time but this is obviously going to be inefficient.
How can I add multiple arrays to the JComboBox? i.e. A[1], A[2], A[3]... A[14], B[1], B[2]...
This is the code for my JComboBox at the moment and, as far as I can see, this should work - I cannot figure out why it isn't. I have a method in the Seat class called toString() which returns the String representing the seat.
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(A);
model.addElement(B);
model.addElement(C);
model.addElement(D);
model.addElement(E);
model.addElement(F);
model.addElement(G);
model.addElement(H);
model.addElement(J);
model.addElement(K);
model.addElement(L);
JComboBox seatCombobox = new JComboBox();
seatCombobox.setModel(model);
unbookSeatWindow.buttonsPanel.add(seatCombobox);
The result of this code is this: http://pasteboard.co/1eej5Be1.png In the JComboBox is the random code (I forget the name of this) representing each array, but nothing to represent each individual seat in the array. In total, there should be 197 entries in the JComboBox
All help will be appreciated.
As suggested, I tried using
`allSeats = ArrayUtils.addAll(A, B, C, D, E, F, G, H, J, K, L);`
but NetBeans told me to change this to
`allSeats = (Seat[]) ArrayUtils.addAll(A, B, C, D, E, F, G, H, J, K, L);`
NetBeans then said that everythign was alright but as soon as I tried to run the program, I got this exception:
BlException in thread "main" java.lang.IllegalArgumentException: Cannot store java.lang.Object in an array of Seat at org.apache.commons.lang3.ArrayUtils.addAll(ArrayUtils.java:3469) at BookingsSystem.main(BookingsSystem.java:267) Caused by: java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at org.apache.commons.lang3.ArrayUtils.addAll(ArrayUtils.java:3459) ... 1 more Java Result: 1
line 267 is the line above
Upvotes: 1
Views: 428
Reputation: 550
Combine arrays using the method suggested by @Apurv.
Seat[] seats = ArrayUtils.addAll(A, B, C);
Create your JComboBox like this:
JComboBox seatCombobox = new JComboBox(seats);
This will create a new JComboBox using a default model and the first item selected
Upvotes: 2
Reputation: 3753
You can use ArraysUtil.addAll() to combine all the arrays to one and then add the single array to JComboBox
Upvotes: 2