• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

adding an InternalFrameListener to a JMenu Item

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);


}

}



}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic