-
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
-
JInternalFrames use InternalFrameListeners. Have you tried this? Though something tells me that there's a better way... Examining...
-
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:
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();
}
});
}
}
-
Hey Fubarable:
Nice quick and dirty testing and, yes, it answers my question. Thank you.
Thumb ups,
TT