I have some questions regarding JFrame. Here's the scenario:
I have made some object classes in java and I want to make the GUI for these objects. So i created 2 JFrames. One JFrame manipulating these different classes and the other JFrame manipulating the first one.
The first JFrame i'll call it "TypesGUI". It manipulates differents instances of animals (Lion, tiger etc). The second JFrame i'll call it AnimalGUI.
Now AnimalGUI is just JFrame that contains menus and textareas. In one of the menus there is a menu item "Create animal". Now i want it in a way that when I click on "Create animal", TypesGUI should appear so as to create what i want to create. And in TypesGUI, there is a button. SO if i should click on that button after creating what i want to create, the window should disappear and what i created the should appear in the textareas of AnimalGUI. In other words, I want to be able to get the different characteristics of the animals I created.
So far I added an action listener to the menu item and the button and used setVisible method with values true or false for each of them respectively. I feel like using setVisible(true or false) does not really free the memory used by the TypesGUI. Is using setVisible a good approach? Also how can I be able to get the characteristics of the different animals created in TypesGUI and show them in AnimalGUI? Thanks.
First off, the second window, the one that allows the user to select a type of animal is not acting as an indepent application window but more of as a dialog that is related to and dependent on the main window. For example, you'd never display this second window on its own, but rather you'd only display it to get information that is destined for the main window/class. Thus it should be a dialog type of window, either a JOptionPane, or a modal JDialog, and not a JFrame. This has several other advantages including guaranteeing that it remains in front of the main window z-order wise, and that it won't ever shut down the entire application if it is disposed of.
Regarding setVisible(true/false) this should work just fine.
Regarding extracting the Animal type information from the second window/dialog: if you call the second window as a modal JDialog or JOptionPane (which is really a specialized modal JDialog), you will know exactly when the user has completed his work with the second window, since your main window's code will start up right after the spot where you called setVisible(true) on the second window or called JOptionPane.showXXXX(). At this point you could have your main window/class request the animal type from the second window by giving the second window/class a public method, say getAnimalType() which returns this information.
For example, here's a two-window program that show's a JPanel that uses GridBagLayout and accepts two JTextField texts in a JOptionPane: