permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes adding an InternalFrameListener to a JMenu Item Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding an InternalFrameListener to a JMenu Item" Watch "adding an InternalFrameListener to a JMenu Item" New topic
Author

adding an InternalFrameListener to a JMenu Item

Gerald Spica
Ranch Hand

Joined: Jul 28, 2003
Posts: 30
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);


}

}



}
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

I am trying to add an InternalFrameListener to a JMenu.

This makes no sense because it can't be done. I gave this a few days in the hopes that someone else might understand what you are trying to do. But I guess no one does.

Can you try and explain better what you are wanting to do? Here is what I think you are trying to do:

A. Create JInternalFrame
B. When JInternalFrame is created, create a JMenuItem
C. Add JMenuItem to JMenu
D. If JInternalFrame == selected from JMenuItem being clicked, Bring JInternalFrame to front.

If this is correct let me know. If not, tell me what I have wrong. We'll go from there.


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding an InternalFrameListener to a JMenu Item
 
Similar Threads
help with GUI practice
Adding JMenuBar, after user Authentication.. Please Help
Is this a bug..?
mnemonics are not getting focus for the menubar
Adding Components in to JInternalFrame