+ 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?
10 ответов
+ 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.
+ 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).
+ 6
You're welcome, have fun coding ^^
+ 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
+ 4
Which gui framework do you use?
Swing (sounds like it)? JavaFx?
+ 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?
+ 2
thanks for that appreciate your help
+ 1
yeah its swing
+ 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.
+ 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.