Results 1 to 5 of 5
  1. #1
    nfh
    nfh is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default Is there any standard way to manage multiple JFrames?

    Say you have JFrames A, B and C. When you click on a JButton that is on the A JFrame, it opens the B JFrame (instantiates it and sets visible to true) and B is hidden.
    Now the same happens on B: it opens C by instantiating and setting its visible property to true and hides itself. So now we have 3 instances of those JFrames on memory but only one visible.
    Now comes the problem: when C does its job, it is supposed to close (with dispose) and to bring again the B JFrame. It can't be done again by instantiating it because we already have an instance of B running and C doesn't have any reference to the instance of B.

    What can be done to solve this? I was thinking if there's any standard way of managing windows on a multiple window aplication such as this example (a set of steps on a wizard, for instance).

    I could think of a WindowManager class implemented with the singleton pattern that was responsible for all the opening and closing of windows, but I'm not sure if there's anything else more common that could be done.

    Any suggestions?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have only one JFrame and the others are modal JDialogs. When a modal JDialog is set to visible with setVisible, the program flow in the component that calls the dialog halts until the dialog is dealt with, and then the program flow begins again at the spot just after the dialog's setVisible(true) was called. Experiment with this as it will work.

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Are you writing all the code for windows A, B, and C?

    If not, things will be difficult. And system dependent.

    If you are, then
    • Modify the code for windows B and C to so they are generators of ActionEvents.
      (They need to implement addActionListener and create events for completion.)
      Then Window A can listen for the events and do what it must.
    • Rethink your UI. It is likely to be really hard for the users.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For example:
    Java Code:
    import java.awt.*;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    public class MultipleWindows {
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Window A");
          frame.getContentPane().add(new WindowA());
          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 WindowA extends JPanel {
       public WindowA() {
          setPreferredSize(new Dimension(400, 400));
          add(new JButton(new NewWindowAction(new WindowB(), "Window B")));
          add(new JButton(new CloseAction()));
       }
    }
    
    class WindowB extends JPanel {
       public WindowB() {
          setPreferredSize(new Dimension(350, 350));
          add(new JButton(new NewWindowAction(new WindowC(), "Window C")));
          add(new JButton(new CloseAction()));      
       }
    }
    
    class WindowC extends JPanel {
       public WindowC() {
          setPreferredSize(new Dimension(300, 300));
          add(new JButton(new CloseAction()));
       }
    }
    
    class NewWindowAction extends AbstractAction {
       private JPanel panel;
       private String title;
       
       public NewWindowAction(JPanel panel, String title) {
          super("New Window");
          this.panel = panel;
          this.title = title;
       }
       
       public void actionPerformed(ActionEvent e) {
          Window win = SwingUtilities.getWindowAncestor((Component) e.getSource());
          win.setVisible(false);
          
          JDialog dialog = new JDialog(win, title, ModalityType.APPLICATION_MODAL);
          dialog.getContentPane().add(panel);
          dialog.pack();
          dialog.setLocationRelativeTo(null);
          dialog.setVisible(true);
          
          win.setVisible(true);
       }
    }
    
    class CloseAction extends AbstractAction {
       public CloseAction() {
          super("Close");
       }
    
       public void actionPerformed(ActionEvent e) {
          Window win = SwingUtilities.getWindowAncestor((Component) e.getSource());
          win.dispose();
       }
    }
    but I agree with zweibieren: this is not a user-friendly user interface. Much better would be to use CardLayout to swap JPanels, for instance.

  5. #5
    nfh
    nfh is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    I do understand that having a common area on a main JFrame (using a JPanel with CardLayout) and I totally agree with this approach. Nonetheless, there might me some times where multiple windows might come in handy and that's why I was trying to figure out the easiest way to do it.

    Changing JFrames to JDialogs might do the trick, as the "setVisible(true)" call is synchronous.
    The event approach (B listens for a specific event of C to know when it should set its visibility to true again) is also an option, although it is a bit more complex.

    Thank you all for your suggestions!

Similar Threads

  1. Web application to manage music
    By Thomasheen in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 01:20 AM
  2. How to manage/access variables between classes
    By dan0 in forum New To Java
    Replies: 2
    Last Post: 04-03-2009, 01:53 AM
  3. JMX to to manage server application
    By shiva in forum Advanced Java
    Replies: 0
    Last Post: 03-26-2009, 08:47 AM
  4. How I manage Adobe eForm(pdf)
    By fastspeed33 in forum Advanced Java
    Replies: 0
    Last Post: 09-04-2008, 12:16 PM
  5. can't manage to get this
    By mollentze in forum New To Java
    Replies: 3
    Last Post: 01-12-2008, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •