+ 1

Creating multiple windows/frames GUI

i need to create a main window with 5 additional windows. So the main window will have a menubar and menus to let user choose what they want. if menu chosen a new pop up window will appear without closing the main window. the question is whether it is better creating all windows in 1 class (in 1 constructor) and set it visible if it chosen or creating it separately in their own class and reference it?

3rd Jun 2018, 9:50 PM
Ferdinand Rosmi
Ferdinand Rosmi - avatar
10 Respuestas
+ 5
Okay then :) Your menubar should contain a menuitem. Then simply add an action listener to this menuitem and create a new JFrame there. You could as well create a custom class that extends JFrame and create a new instance from it (helpful if you want to override some default behaviors of JFrame, but not a must). Examples for the action listener of the menuitem: https://stackoverflow.com/questions/9778621/how-to-make-a-jmenu-item-do-something-when-its-clicked Have a look at the first answer there, the part where you create and show a new JFrame goes in the overridden actionPerformed method.
4th Jun 2018, 7:55 AM
Tashi N
Tashi N - avatar
+ 6
I tend to say that you don't need an array for the frames... but never say never ;) Do you need to make changes in a loop (the same action for every frame) or something else that makes you think you actually need to put all frames to an array (in general a collection like ArrayList would be better, if really necessary)? I never needed all frames in an array to handle them... but it depends on your use case. I don't think it's necessary to override the default action listener for the JMenu (again depends on what you want to happen on an event - maybe you don't want the default behavior, then just override it like mentioned for the menuitem).
4th Jun 2018, 8:53 AM
Tashi N
Tashi N - avatar
+ 6
You're welcome, have fun coding ^^
4th Jun 2018, 12:42 PM
Tashi N
Tashi N - avatar
+ 5
Sorry, I was wrong, you need a MenuListener for the JMenu, here's the ref: https://docs.oracle.com/javase/7/docs/api/javax/swing/event/MenuListener.html
4th Jun 2018, 12:35 PM
Tashi N
Tashi N - avatar
+ 4
Which gui framework do you use? Swing (sounds like it)? JavaFx?
3rd Jun 2018, 11:00 PM
Tashi N
Tashi N - avatar
+ 2
thanks for the wonderful answer Tashi N . mind if i ask another question? what will be better create the frames as an array or individually with unique naming so easy to understand? as well, what should i use as an event listener for JMenu?
4th Jun 2018, 8:17 AM
Ferdinand Rosmi
Ferdinand Rosmi - avatar
+ 2
thanks for that appreciate your help
4th Jun 2018, 12:41 PM
Ferdinand Rosmi
Ferdinand Rosmi - avatar
+ 1
yeah its swing
3rd Jun 2018, 11:21 PM
Ferdinand Rosmi
Ferdinand Rosmi - avatar
+ 1
well, all the frames arent neccessary have same condition. one frame as a main window, one as a list of people, one for add, one for edit, and then one frame for calculate.
4th Jun 2018, 8:57 AM
Ferdinand Rosmi
Ferdinand Rosmi - avatar
+ 1
also one of the menu in the menubar has to have a function to save changes in database. I tried to have a JMenu uses an ActionListener but it wont work.
4th Jun 2018, 9:00 AM
Ferdinand Rosmi
Ferdinand Rosmi - avatar