1

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?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Chris Collins
  • 89
  • 2
  • 11
  • 1
    Think about your data in terms of its relationship to its other data. For example, each bed is contained inside a room, which I assume is contained in some other structure. This way, you can constructor your Action in such away that it knows at least the Room the Bed will need to be added to – MadProgrammer Jul 22 '15 at 21:53

2 Answers2

3

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.

Names aren't nearly as important as many new coders think they are, certainly not variable names. What is much more important are references and how references can be accessed.

The best solution is to use an MVC or Model-View-Control program structure to help organize your code. But if that's too complex, then the view representation of the room, here the RoomFrame would hold this list, and would have an addBed(Bed bed) method. Most important, the Room or RoomFrame class should hold an ArrayList of its own Bed objects, something like:

List<Bed> bedList = new ArrayList<>(),

And so the Room itself would keep track of its own beds, and have a ready reference to all the bed instances that it holds. Bottom line: make your objects smart so they know their own states and can change their behaviors depending on that state.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

Pass a unique ID into your AddBedListener constructor that can be used to identify the button uniquely, or just force the unique behavior into each instance of the AddBedListener.

andrewdleach
  • 2,458
  • 2
  • 17
  • 25