I have a loop to create buttons like so. These buttons act as beds in a room (the room is an InternalFrame). addButtons is a JButton array.
for (int i = 0; i < addButtons.length; i++) {
addButtons[i] = new JButton(" Add Bed "); // make text big
addButtons[i].addActionListener(new AddBedListener());
addButtons[i].setActionCommand("" + i);
gbc.fill = GridBagConstraints.BOTH;
room1Panel.add(addButtons1[i]);
}
I want to add a function that allows me to create more rooms, so Ive created a function
public void createRoom(int nBeds, String bName){
RoomFrame roomFrame = new RoomFrame();
JPanel panel = new JPanel();
for (int i = 0; i < nBeds; i++) {
addButtons[i] = new JButton(" Add Bed "); // make text big
addButtons[i].addActionListener(new AddBedListener());
addButtons[i].setActionCommand("" + i);
gbc.fill = GridBagConstraints.BOTH;
room4Panel.add(addButtons4[i]);
}
BackButton backButton = new BackButton("Back");
}
The problem is, I don't know how to dynamically create an array of buttons for use in my code. The code needs to know that an array of buttons in one room is different to an array of buttons in another room, hence the name must be unique. How do I have a function that dynamically creates rooms but allows me to reference each button as a unique entity?