Results 1 to 4 of 4
  1. #1
    Turyturbo is offline Member
    Join Date
    Dec 2008
    Location
    Colorado Springs
    Posts
    5
    Rep Power
    0

    Default Window Stacking in Order

    Hi:

    The parent component is JFrame. I used the JDesktopPane to store all JInternalFrames (ex. JDesktopPane.add(JInternalFrame)).

    From my understanding, addWindowListener() can be used for JFrame or JDialog but not for JInternalFrame. (I tried this: JInternalFrame.addWindowListener() and it is not part of JInternalFrame API).

    I was trying to create a stack in order for windows for saved settings.

    For example:
    Open a main window (JFrame), click a menu item (to display a JInternalFrame window A). It automatically adds to the JDesktopPane. At that moment, window A is on focus. After you open several windows (B, C, D, E, F, G).

    Now, on JFrame, it contains window A-G. After you play around with those opened windows and it is not in order. From back to front in order on screen, it is G, A, B, E, F, C. Window C is on focus right now.

    Then you try to save the settings in order. I was thinking about using addWindowListener() (i.e. gainFocus, lostFocus) to reorder the array by "pull" the current selected window out of the array and push into the stack each time any window got selected so that way the array contains right stack in order from back to front.

    Unfortunately, it is not working that way. I am wondering if any of you have a better idea how to make this works?

    Thank you,
    TT

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

    Default

    JInternalFrames use InternalFrameListeners. Have you tried this? Though something tells me that there's a better way... Examining...

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

    Default

    On further testing, it seems that the JDesktopPane method getAllFrames() returns an array of JInternalFrames in their z-order. Note that this is not specified in the API as I understand it; rather, this is just the result of some quick and dirty testing:

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class InternalFrameOrder {
      private static final Dimension APP_SIZE = new Dimension(500, 500);
      private static final Dimension INTERNAL_FRAME_SIZE = new Dimension(200, 200);
      private JDesktopPane desktopPane = new JDesktopPane();
      private JMenuBar menuBar = new JMenuBar();
    
      public InternalFrameOrder() {
        desktopPane.setPreferredSize(APP_SIZE);
        
        JMenuItem addInternalFrame = new JMenuItem(new AddInternalFrameAction());
        addInternalFrame.setMnemonic('a');
        JMenuItem enumerateInternalFrames = new JMenuItem(new EnumerateInternalFramesAction());
        enumerateInternalFrames.setMnemonic('e');
    
        JMenu myMenu = new JMenu("My Menu");
        myMenu.setMnemonic('m');
        
        myMenu.add(addInternalFrame);
        myMenu.add(enumerateInternalFrames);
        menuBar.add(myMenu);
      }
    
      public JComponent getMainPanel() {
        return desktopPane;
      }
      
      public JMenuBar getMenuBar() {
        return menuBar;
      }
      
      
      private class AddInternalFrameAction extends AbstractAction {
        int internalFrameCount = 1;
        
        public AddInternalFrameAction() {
          super("Add Internal Frame");
        }
        
        public void actionPerformed(ActionEvent e) {
          JInternalFrame internalFrame = new JInternalFrame("Frame " + internalFrameCount);
    
          internalFrame.setSize(INTERNAL_FRAME_SIZE);
          int delta = 10 * internalFrameCount;
          internalFrame.setLocation(delta, delta);
          desktopPane.add(internalFrame);
          internalFrame.setVisible(true);
          internalFrameCount++;      
        }
      }
      
      private class EnumerateInternalFramesAction extends AbstractAction {
        public EnumerateInternalFramesAction() {
          super("Enumerate Internal Frames");
        }
        
        public void actionPerformed(ActionEvent arg0) {
          JInternalFrame[] intFrames = desktopPane.getAllFrames();
          for (JInternalFrame internalFrame : intFrames) {
            System.out.println(internalFrame.getTitle());
          }
          System.out.println();
        }
      }
    
      private static void createAndShowUI() {
        InternalFrameOrder intFrameOrder = new InternalFrameOrder();
        
        JFrame frame = new JFrame("InternalFrameOrder");
        frame.getContentPane().add(intFrameOrder.getMainPanel());
        frame.setJMenuBar(intFrameOrder.getMenuBar());
        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();
          }
        });
      }
    }

  4. #4
    Turyturbo is offline Member
    Join Date
    Dec 2008
    Location
    Colorado Springs
    Posts
    5
    Rep Power
    0

    Default

    Hey Fubarable:

    Nice quick and dirty testing and, yes, it answers my question. Thank you.

    Thumb ups,
    TT

Similar Threads

  1. How to order a vector?
    By tyang in forum New To Java
    Replies: 2
    Last Post: 02-06-2010, 08:24 AM
  2. Replies: 4
    Last Post: 01-10-2010, 07:48 AM
  3. Instantiation order
    By Jeremy720 in forum New To Java
    Replies: 3
    Last Post: 07-17-2009, 03:19 PM
  4. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 12:17 PM
  5. how can i move one frame window to another window
    By santhosh_el in forum AWT / Swing
    Replies: 8
    Last Post: 06-10-2009, 03:36 PM

Posting Permissions

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