Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes MDI with a Windows menu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MDI with a Windows menu" Watch "MDI with a Windows menu" New topic
Author

MDI with a Windows menu

David McCormick
Greenhorn

Joined: Jun 26, 2001
Posts: 5
Has anybody ever figured out how to build an MDI interface with a Windows menu? I've seen an example on JavaWorld (mditest) but it doesn't sync the Windows menu with the JInternalFrames. I've tried this:
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('f');
mb.add(fileMenu);
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic('n');
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JInternalFrame jif = new JInternalFrame("Document" + ++frameCount, true, true,
true, true);
Dimension d = desktop.getSize();
jif.setBounds(frameCount * 25,frameCount * 25, d.width/2, d.height/2);
jif.show();
desktop.add(jif);
desktop.moveToFront(jif);
if (windowMenu == null) {
windowMenu = new JMenu("Window");
windowMenu.setMnemonic('w');
mb.add(windowMenu);
}
JMenuItem windowItem = new JMenuItem(frameCount + ". Document" + frameCount);
windowMenu.add(windowItem);

}
};
newItem.addActionListener(lst);
fileMenu.add(newItem);
but I can't figure out how to put a listener or action on the JMenuItem that ties it to a particular JInternalFrame.
Thanks in advance.
Martin webster
Greenhorn

Joined: Jun 20, 2001
Posts: 20
Hi,
This a class that I've created that manages Internal Frames for a windows menu. It is giving a windows menu on construction, and then every time you create an internal frame you would just call
frameCreated(JInternalFrame jrm). This class also contains the ability to change the menu item when the frame Icon or title changes.
<PRE>
/**
* Manages the active window menu. A menu that is to be used to contain the active
* windows is passed in as part of the constructor to this class. When a frame is
* launched then the frameCreated method must be called to add a menu item to the
* menu. When this frame is closed then it automatically calls the frameClosed method
* which will remove the menu item

*
* Currently these are just standard mnu items.
*/
public class InternalFrameMenuManager
{
HashMap _map = new HashMap();
JMenu _windowMenu;
private int _windowCount = 0;
private JSeparator _seperator = new JSeparator();
/**
* Constructor accepting a menu
* @param windowMenu the menu to be used to attach the active window menu items
*/
public InternalFrameMenuManager(JMenu windowMenu)
{
_windowMenu = windowMenu;
}
/**
* called automatically when the frame is closed. This removes the menu item
* from the menu
* @param frm The closing internal frame
*/
public void frameClosed(JInternalFrame frm)
{
_windowCount--;
JMenuItem item = (JMenuItem)_map.get(frm);
_windowMenu.remove(item);
if (_windowCount==0)
{
_windowMenu.remove(_seperator);
}
}
/**
* Called when the frame is created. This adds a menu item to the menu
* param frm The internal frame
*/
public void frameCreated(final JInternalFrame frm)
{
_windowCount++;
if (_windowCount==1)
{
_windowMenu.add(_seperator);
}
frm.addInternalFrameListener(new InternalFrameAdapter()
{
public void internalFrameClosed(InternalFrameEvent e)
{
frameClosed(frm);
}
});
frm.addPropertyChangeListener (new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent pce)
{
if (pce.getPropertyName().equals("title"))
{
JMenuItem item = (JMenuItem)_map.get(frm);
item.setText(frm.getTitle());
}
else if (pce.getPropertyName().equalsIgnoreCase("frameIcon"))
{
JMenuItem item = (JMenuItem)_map.get(frm);
item.setIcon(frm.getFrameIcon());
}
}
});
JMenuItem item = new JMenuItem(frm.getTitle(), frm.getFrameIcon());
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try
{
frm.setSelected(true);
if (frm.isIcon())
{
frm.setIcon(false);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
_map.put(frm, item);
_windowMenu.add(item);
}
}
</PRE>


Martin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MDI with a Windows menu