Hi I need an Example program in which
When i maximize the JInternalFrame the JMenuBar of JFrame should set on JInternalFrame
and When i minimize the JInternalFrame again the JMenuBar should leave JinternalFrame and set
to JFrame as shown below
Please provide me an Example Program in Java Swing
Seems to work fine, please post SSCCE to show specific problem:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JInternalFrameDemo {
JDesktopPane jdpDesktop;
static int openFrameCount = 0;
public JInternalFrameDemo() {
JFrame frame = new JFrame("JInternalFrame Usage Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
};
createFrame(); // Create first window
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JInternalFrameDemo();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}
I also have similar problem. It is a bug in JInternalFrame LookAndFell. JInternalFrames using Windows L&F, should be
more Windows like. If I maximize a JInternalWindow,
that window is not working as in Windows.
Its top bar should be one with the MDI.
Meus and Toolbars should be inside.
Maximized Internal frames should not have their
own border.
Visit
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4102061
for datails.
Related
I am trying to change the image icon on a JFrame and it is not showing up. I have tried both the absolute path to my desktop and then the path that I have in Eclipse. Why is this not working. I have looked on stackoverflow and this is how it looks like that it is probably done, but for some reason the code below is not working.
code:
package TestMenu;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class TestJFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public TestJFrame() {
ImageIcon img = new ImageIcon("C:\\Users\\itpr13266\\workspace\\TestMenu\\src\\TestMenu\\img\\s.jpg");
setIconImage(img.getImage());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(117, 105, 10, 10);
contentPane.add(panel);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestJFrame frame = new TestJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
No exception was thrown.
The code is fix now. I had the wrong image format type.
Code that does not work:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
public class MenuLookDemo {
JTextArea output;
JScrollPane scrollPane;
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;
menuBar = new JMenuBar();
menu = new JMenu("A Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);
menuItem = new JMenuItem("A text-only menu item",
KeyEvent.VK_T);
//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
ImageIcon icon = createImageIcon("src\\TestMenu\\img\\stop.jpg");
menuItem = new JMenuItem("Both text and icon", icon);
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem = new JMenuItem(icon);
menuItem.setMnemonic(KeyEvent.VK_D);
menu.add(menuItem);
//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic(KeyEvent.VK_O);
group.add(rbMenuItem);
menu.add(rbMenuItem);
//a group of check box menu items
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
cbMenuItem.setMnemonic(KeyEvent.VK_C);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Another one");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
menu.add(cbMenuItem);
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem("Another item");
submenu.add(menuItem);
menu.add(submenu);
//Build second menu in the menu bar.
menu = new JMenu("Another Menu");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);
return menuBar;
}
public Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
//Create a scrolled text area.
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
//Add the text area to the content pane.
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = MenuLookDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MenuLookDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
MenuLookDemo demo = new MenuLookDemo();
frame.setJMenuBar(demo.createMenuBar());
frame.setContentPane(demo.createContentPane());
//Display the window.
frame.setSize(450, 260);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Error: (From the above example and it is just like the example above which I got to work)
Couldn't find file: src\TestMenu\img\stop.jpg
May be that BMP is not supported. If you follow the Java source code from the constructor of ImageIcon you end up at:
(java.awt.Toolkit.java)
/**
* Returns an image which gets pixel data from the specified file,
* whose format can be either GIF, JPEG or PNG.
* ...
*/
public abstract Image getImage(String filename);
According to this article, ImageIcon supports GIF, JPEG, or PNG. Try converting your image to another format using something like GIMP or Paint and see if you get the same results.
http://docs.oracle.com/javase/7/docs/api/java/awt/Toolkit.html#getImage%28java.lang.String%29
It worked for me, I substituted a jpeg on my c drive and changed the path accordingly. It placed the icon in the upper left corner of the frame. Try simplifying your path to C:\filename and see if it works.
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Swing {
public static void main(String[] args) {
////jframe = a GUI(graphical user interface) window to add components
JFrame frame = new JFrame();//we are creating a new frame
frame.setVisible(true);//this helps to make the frame visible
frame.setSize(500, 500);//this is for resizing our window into any size
frame.setTitle("Manoj sai");//to set the title for the window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//normally when we hit X button it will hide but it will not close so to close that we have to give that when we press X exit_on_close
frame.setResizable(false);//this makes the window not resizeable we cant resize the window
//ImageIcon will help to keep images in GUI(its a separate class)
ImageIcon image = new ImageIcon("C:\\Users\\manoj\\Pictures\\logos\\logo.jpg");//creates an image icon
frame.setIconImage(image.getImage());//sets the selected image for icon for our window
}
}
here use the file path which is in your computer then u will get ur icon displayed after importing to the eclipse use the path which is in ur local disc c ex:"C:\\Users\\manoj\\Pictures\\logos\\logo.jpg"
Copied and pasted this code straight from Oracle's Java tutorial on making internal frames:
(Comments are Oracle's, not mine.) Marked error location w/comment "Error occurs here" in right margin. Computer says MyInternalFrame "cannot be resolved to type."
package components;
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 InternalFrameDemo extends JFrame
implements ActionListener {
JDesktopPane desktop;
public InternalFrameDemo() {
super("InternalFrameDemo");
//Make the big window be indented 50 pixels from each edge
//of the screen.
int inset = 50;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset,
screenSize.width - inset*2,
screenSize.height - inset*2);
//Set up the GUI.
desktop = new JDesktopPane(); //a specialized layered pane
createFrame(); //create first "window"
setContentPane(desktop);
setJMenuBar(createMenuBar());
//Make dragging a little faster but perhaps uglier.
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
//Set up the lone menu.
JMenu 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.setActionCommand("new");
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);
return menuBar;
}
//React to menu selections.
public void actionPerformed(ActionEvent e) {
if ("new".equals(e.getActionCommand())) { //new
createFrame();
} else { //quit
quit();
}
}
//Create a new internal frame.
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame(); //Error occurs here//
frame.setVisible(true); //necessary as of 1.3
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}
//Quit the application.
protected void quit() {
System.exit(0);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
InternalFrameDemo frame = new InternalFrameDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
From the comments of InternalFrameDemo.java:
/*
* InternalFrameDemo.java requires:
* MyInternalFrame.java
*/
it says that you need MyInternalFrame.java which is here. Add this file to your build path.
I need to have a JMenu (the one with the arrow on right which can display JMenuItem) in a JPanel. The problem is that when I do that the JMenu is not activate on mouse rollover...
I don't know how to do that and if it's possible.
If you wrap your JMenu in a JMenuBar, it works as expected.
Here is a demo example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class TestMenus {
private JMenuBar createMenuBar(String name, int depth) {
JMenuBar bar = new JMenuBar();
bar.add(createMenu(name, depth));
return bar;
}
private JMenu createMenu(String name, int depth) {
JMenu menu = new JMenu(name);
for (int i = 0; i < 5; i++) {
if (depth > 0) {
menu.add(createMenu("sub-" + name, depth - 1));
}
}
for (int i = 0; i < 5; i++) {
menu.add(createMenuItem("Menu item " + (i + 1)));
}
return menu;
}
private JMenuItem createMenuItem(String name) {
final JMenuItem jMenuItem = new JMenuItem(name);
jMenuItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(jMenuItem, "Successfully pressed a menu item");
}
});
return jMenuItem;
}
protected void initUI() {
JFrame frame = new JFrame(TestMenus.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMenuBar("Root menu", 3));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestMenus().initUI();
}
});
}
}
An the result:
Here is another way to solve this which I think is closer to what you want. It involves extending JMenuBar to give it the look of a JMenu Object.
The class contains a JMenu Object called menu. The add methods are overridden so you are adding to menu instead of the JMenuBar (You may have to override a few more add methods to make this perfect).
There are a few options with painting. I wasn't sure if you wanted the button style look of the JMenuBar, so I included a few comments on some options to customize that, as well as the underline look of the JMenuBar.
Here is the result of the button look with no border:
Here is the result with no button look and no border:
import java.awt.*;
import javax.swing.*;
public class JPanelMenu extends JMenuBar{
public static void main(String[] args) {
JFrame f = new JFrame("Menu Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenu jmenu = new JMenu("J Menu");
jmenu.add(new JMenuItem("Menu Item"));
JPanelMenu m = new JPanelMenu("Menu");
m.add(jmenu);
m.add(new JMenuItem("Menu Item 1"));
m.add(new JMenuItem("Menu Item 2"));
JPanel background = new JPanel();
background.add(m);
f.setContentPane(background);
f.pack();
f.setVisible(true);
}
//This is the JMenu that is shown
private JMenu menu;
public JPanelMenu(String title) {
super();
menu = new JMenu(title);
super.add(menu);
}
#Override
public Component add(Component comp) {
//You add the the JMenu instead of the JMenuBar
return menu.add(comp);
}
#Override
public JMenu add(JMenu c) {
//You add the the JMenu instead of the JMenuBar
return (JMenu) menu.add(c);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Include these two lines to remove the button look
//Or remove this method to keep the button look
//g.setColor(getBackground());
//g.fillRect(0, 0, getWidth(), getHeight());
}
#Override
protected void paintBorder(Graphics g) {
//Remove this line to remove the underline look
//when you remove the button look
//An alternative is to you setBorderPainted(false);
//when you create the object or in the constructor
//Or remove this method to keep the border
//super.paintBorder(g);
}
}
You must pass a BorderLayout in JPanel layout then you can add menu bar in panel:
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(menubar, BorderLayout.NORTH);
How to change jdesktoppane background image in MDI (Multiple Documents interface) using java netbeans? Means I added the jdesktoppane to java MDI so now I want to change default background image of that jdesktoppane which I'm using in java MDI. Any easy way?
Check attached snapshot link may be you will better understand my question what I want.
http://i50.tinypic.com/iml1e9.jpg
+1 to MadProgrammers comment.
Simply override JDesktopPane paintComponent(..) and call drawImage(Image img,int x,int y,ImageObserver io) to draw an image.
Dont forget to honor the paint chain and call super.paintComponent(g) as first call in overridden paintComponent(..) method
Here is an example:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JInternalFrameDemo {
private JDesktopPane jdpDesktop;
private static int openFrameCount = 0;
private BufferedImage img;
public JInternalFrameDemo() {
JFrame frame = new JFrame("JInternalFrame Usage Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
img = ImageIO.read(new URL("http://images1.wikia.nocookie.net/__cb20120817224359/villains/images/6/6a/Nine-Tailed_Fox_(Naruto).jpg"));
} catch (Exception ex) {
ex.printStackTrace();
}
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
#Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(img, 0, 0, null);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(), img.getHeight());
}
};
createFrame(); // Create first window
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JInternalFrameDemo();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}
I resolve it in a separate function to create a desktop object.
Code as below
private JDesktopPane intializeDesktop(JDesktopPane mydesktop,String imagePath,int scalx,int scaly) {
// A specialized layered pane to be used with JInternalFrames
mydesktop = new JDesktopPane() {
ImageIcon icon = new ImageIcon(imagePath);
Image image = icon.getImage();
Image newimage = image.getScaledInstance(scalx, scaly, Image.SCALE_SMOOTH);
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(newimage, 0, 0, this);
}
};
return mydesktop;
}
I am trying to build a JMenu dynamically right when clicking in it (I'm only getting an empty menu), below is my code.
final JMenu JMWindows = new JMenu("Opened Windows");
JMWindows.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for(JInternalFrame ji : desktop.getAllFrames())
{
JMWindows.add(ji.getTitle());
}
}
});
I've realized that the actionperformed is never called, the JMenu is inside a JMenuBar. What could be the problem ?
You add ActionListener to JMenu this cannot be done for JMenu use MenuListener and set it via JMenu#addMenuListener(..)
Also you need to call revalidate() and repaint() on JMenu instance after adding/removing components from it or the changes will not be reflected (we could also call this on the containers instance i.e JMenuBar or JFrame, but we know only 1 specific JMenu will change so no need IMO).
Please watch your variable naming schemes JMWindow should be jmWindow/jMWindow variables always begin with no caps and the 1st letter of every new word thereafter gets capitalized (besides for constants and enums).
Here is an example using MenuListener:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
public class Test {
private JDesktopPane jdpDesktop;
private static int openFrameCount = 0;
final JFrame frame = new JFrame("JInternalFrame Usage Demo");
public Test() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
};
for (int i = 0; i < 3; i++) {
createFrame(); // Create first window
}
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
final JMenu jmWindows = new JMenu("Opened Windows");
jmWindows.addMenuListener(new MenuListener() {
#Override
public void menuSelected(MenuEvent me) {
jmWindows.removeAll();//remove previous opened window jmenuitems
for (JInternalFrame ji : jdpDesktop.getAllFrames()) {
JMenuItem menuItem = new JMenuItem(ji.getTitle());
jmWindows.add(menuItem);
}
jmWindows.revalidate();
jmWindows.repaint();
jmWindows.doClick();
}
#Override
public void menuDeselected(MenuEvent me) {
}
#Override
public void menuCanceled(MenuEvent me) {
}
});
menuBar.add(jmWindows);
return menuBar;
}
protected void createFrame() {
Test.MyInternalFrame frame = new Test.MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}
You need to repaint and or validate your frame after adding or removing something.
you can use the methods repaint() and validate() in your JFrame for this
You missed defining a JMenuItem.
final JMenu JMWindows = new JMenu("Opened Windows");
JMWindows.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for(JInternalFrame ji : desktop.getAllFrames())
{
JMenuItem menuItem = new JMenuItem(ji.getTitle());
JMWindows.add(menuItem);
}
}
});