Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-13-2009, 03:04 AM
nfh nfh is offline
Member
 
Join Date: Nov 2009
Posts: 5
Rep Power: 0
nfh is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-13-2009, 03:11 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-13-2009, 04:06 AM
Senior Member
 
Join Date: Aug 2009
Location: Pittsburgh, PA
Posts: 265
Rep Power: 1
zweibieren is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-13-2009, 04:10 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
Default
For example:
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-13-2009, 01:30 PM
nfh nfh is offline
Member
 
Join Date: Nov 2009
Posts: 5
Rep Power: 0
nfh is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 12:01 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org