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:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TwoWindowEg {
private static void createAndShowUI() {
JFrame frame = new JFrame("Two Window Eg");
frame.getContentPane().add(new MainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MainPanel extends JPanel {
private JTextField textField = new JTextField(20);
private DialogPanel dialogPanel = null;
public MainPanel() {
textField.setEditable(false);
textField.setFocusable(false);
textField.setBackground(Color.white);
JButton openDialogBtn = new JButton("Open Dialog");
openDialogBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dialogPanel == null) {
dialogPanel = new DialogPanel();
}
int result = JOptionPane.showConfirmDialog(MainPanel.this, dialogPanel,
"Enter First and Last Name", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String text = dialogPanel.getText();
textField.setText(text);
}
}
});
add(textField);
add(openDialogBtn);
setPreferredSize(new Dimension(400, 300));
setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
}
}
class DialogPanel extends JPanel {
private static final Insets INSETS = new Insets(0, 10, 0, 10);
private JTextField firstNameField = new JTextField(10);
private JTextField lastNameField = new JTextField(10);
public DialogPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
INSETS, 0, 0);
add(new JLabel("First Name"), gbc);
gbc = new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.LINE_END, GridBagConstraints.BOTH,
INSETS, 0, 0);
add(new JLabel("Last Name"), gbc);
gbc = new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
INSETS, 0, 0);
add(firstNameField, gbc);
gbc = new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.LINE_END, GridBagConstraints.HORIZONTAL,
INSETS, 0, 0);
add(lastNameField, gbc);
}
public String getText() {
return firstNameField.getText() + " " + lastNameField.getText();
}
}