0

BACKGROUND

I'm creating a hostel booking software as a project to learn java. I have my main frame which holds buttons that open each room by setting the containing frame to visible. I have an add room button that asks me how many beds the room as and stores this as an int.

QUESTION

Would it be possible to have a room class that would allow me to create new rooms by pressing the button? That means creating an InternalFrame, JPanel and JButtons, adding the buttons to a grid based off the bed number int.

I feel like the idea is there but the implementation is proving tricky since im new to java. Any ideas are appreciated, thanks!

Chris Collins
  • 89
  • 2
  • 11
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jul 21 '15 at 14:23
  • *"Would it be possible to have a room class that would allow me to create new rooms by pressing the button?"* Yes. What have you tried? – Andrew Thompson Jul 21 '15 at 14:24
  • Currently most of my code is in one file, nested in the main frame class.. As im learning more about java Ive started moving things into their own files slowly.. I was using ArrayList and trying to initialise my frames all internally until I thought about classes – Chris Collins Jul 21 '15 at 14:36
  • And im using internal frames for the windows sorry – Chris Collins Jul 21 '15 at 14:38

1 Answers1

0

Read the section from the Swing tutorial on How to Use Internal Frames.

The demo in the tutorial shows how to dynamically create internal frames when clicking in a menu item. The concept is the same for buttons. You just add the ActionListener to the button.

Of course you will need to change the code to create your own custom panel. You will also probably want to use a JOptionPane to prompt the user for the number of rooms. Again the tutorial has a section on How to Use Dialogs for an example of using a JOptionPane.

camickr
  • 321,443
  • 19
  • 166
  • 288