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[]...
For the booking of the seats, having the seats separated by row was necessary as they have different prices, however the JComboBox will be used as a way of selecting a seat to unbook and hence just a full list of seats is required.
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.
How can I add multiple arrays to the JComboBox? i.e. A[1], A[2], A[3]... A[14], B[1], B[2]...
I am not very experienced in Java so sorry if this is a stupid question. After lots of research over the past few days, I have tried experimenting with the DefaultComboBoxModel class but am obviously not using it correctly. This was my most recent attempt to solve my problem:
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);
All help will be appreciated.
Upvotes: 1
Views: 1680
Reputation: 13066
You can fill the model in following way using ArrayList
:
DefaultComboBoxModel model ;
JComboBox seatCombobox = new JComboBox();
public void fillModel()
{
ArrayList<String> elements = new ArrayList<String>();
elements.addAll(java.util.Arrays.asList(A));
elements.addAll(java.util.Arrays.asList(B));
elements.addAll(java.util.Arrays.asList(C));
elements.addAll(java.util.Arrays.asList(D));
elements.addAll(java.util.Arrays.asList(E));
elements.addAll(java.util.Arrays.asList(F));
elements.addAll(java.util.Arrays.asList(G));
elements.addAll(java.util.Arrays.asList(H));
elements.addAll(java.util.Arrays.asList(I));
elements.addAll(java.util.Arrays.asList(J));
elements.addAll(java.util.Arrays.asList(K));
elements.addAll(java.util.Arrays.asList(L));
model = new DefaultComboBoxModel(elements.toArray()) ;
seatCombobox.setModel(model);
}
Upvotes: 3
Reputation: 51445
Your DefaultComboBoxModel code is the correct answer. You just add elements from as many arrays as you have.
May I suggest that you use two JComboBox components. One for the section, and another for the seat number. Otherwise, your users will get frustrated reading a list of hundreds of seats.
Upvotes: 6