0

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.

mkab
  • 933
  • 4
  • 16
  • 31

1 Answers1

1

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();
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • If I understand you well, is it that I should create a class that extends JDialog and not JFrame or in the action Listener I should just create a JDialog and use it from there? Another problem is that my first window that creates the animals is kinda complex. It contains combo boxes, check boxes, panels etc. That's why I used a JFrame. Would this be possible with JDialog? – mkab Apr 09 '11 at 18:56
  • @mkab: Any type of complex GUI that is displayed in a JFrame can be displayed in a JDialog. For that matter, you can display complex GUI's in JOptionPanes since the second parameter of the JOptionPane's show method, the one that takes an object, can be any Swing component. For example, see the code that I've added above in the edit to my answer. – Hovercraft Full Of Eels Apr 09 '11 at 18:58
  • Wow i get it now. I always used the default JOptionPane.showXXX dialogs for confirming if the user wants to quit an application for example or if the user wants to save his work. I never thought it could be customizable to contain more complex GUI because for the component parameters, i just put null. Thanks for the help and insight. Cheers. – mkab Apr 09 '11 at 19:08