I am trying to add an InternalFrameListener to a JMenu. When I create a JInternalFrame I create a corresponding JMenuItem, and when I select the JMenuItem from the menu bar I want the it's corresponding JInternalFrame to be activated.
Here is my code so far, but I am struggling with the concepts.
import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import java.awt.event.*;
import java.awt.*;
public class InternalFrame extends JFrame
{
JDesktopPane desktop;
static int openMenuCount = 0;
InternalFrame frame;
static int frameCount = 0;
static final int xOffset = 30, yOffset = 30;
private JMenu menu;
MyInternalFrame listenedToWindow;
private
String name;
public InternalFrame() {
int inset = 50;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset,
screenSize.width - inset*2,
screenSize.height - inset*2);
JMenuBar menuBar = new JMenuBar();
desktop = new JDesktopPane();
setContentPane(desktop);
setJMenuBar(menuBar);
//Set up the lone menu.
menu = new JMenu("Document");
menu.setMnemonic(KeyEvent.VK_D);
menuBar.add(menu);
//Set up the first menu item.
JMenuItem menuItem = new JMenuItem("New");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.addActionListener(new ComputeButtonActionListener());
// menuItem.addActionListener(this);
menu.add(menuItem);
//Set up the second menu item.
menuItem = new JMenuItem("Quit");
menuItem.setMnemonic(KeyEvent.VK_Q);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_Q, ActionEvent.ALT_MASK));
// menuItem.setActionCommand("quit");
// menuItem.addActionListener(this);
menu.add(menuItem);
}
public class ComputeButtonActionListener extends JMenuItem implements ActionListener {
public void actionPerformed(ActionEvent e)
{
++frameCount;
++openMenuCount;
MyInternalFrame listenedToWindow = new MyInternalFrame ( );
MyInternalFrame.MyMenuItem aMenuItem = listenedToWindow.new MyMenuItem("Document #" + openMenuCount);
desktop.add(listenedToWindow);
listenedToWindow.setSize(300, 100);
listenedToWindow.setVisible(true);
listenedToWindow.setLocation(xOffset*frameCount, yOffset*frameCount);
//listenedToWindow.addInternalFrameListener(this);
System.out.println(listenedToWindow.getTitle());
menu.add(aMenuItem);
aMenuItem.addInternalFrameListener(new BringForward());
}
}
public class BringForward implements InternalFrameListener {
public void actionPerformed(ActionEvent e)
{
System.out.println(listenedToWindow);
}
}
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
InternalFrame frame = new InternalFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.setVisible(true);
}
public static void main(String[] args)
{
createAndShowGUI();
}
}
import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.*;
import java.awt.*;
public class MyInternalFrame extends JInternalFrame {
static int openFrameCount = 0;
public MyInternalFrame() {
super("Document #" + (++openFrameCount),
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
// LabelDemo demo = new LabelDemo("Document #" + openFrameCount);
// getContentPane().add(demo);
//...Create the GUI and put it in the window...
//...Then set the window size or call pack...
//setSize(300,100);
//setVisible(true);
//Set the window's location.
//setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
public class MyMenuItem extends JMenuItem implements InternalFrameListener{
public MyMenuItem(String s)
{
super(s);
}
}
}