Adding a JMenu to a JPanel - java

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);

Related

How to Change setVisible() on Panels in different Java Class

package com.spiralfive.inventory;
import java.awt.Font;
import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.BorderLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
public class MainFile extends JApplet implements MouseListener {
// Declare the initial components
private JButton btnNewInventory;
private JButton btnMarkDown;
private JButton btnProcessSales;
private JButton btnActive;
private JButton btnStats;
private Panel mainPanel;
private Panel menuPanel;
private Panel inventoryPanel;
private Panel setactivePanel;
// Call the Initializer...
public void init() {
initComponents();
}
// Initialize the Main Components - Mostly Menu Items
private void initComponents() {
// Main Form Components
mainPanel = new Panel();
menuPanel = new Panel();
inventoryPanel = new Panel();
setactivePanel = new Panel();
btnNewInventory = new JButton("New Inventory");
btnActive = new JButton("Set Active");
btnMarkDown = new JButton("Mark Down");
btnProcessSales = new JButton("Process Sale");
btnStats = new JButton("Statistics");
// Enable Mouse Interactivity
btnNewInventory.addMouseListener(this);
btnMarkDown.addMouseListener(this);
btnProcessSales.addMouseListener(this);
btnActive.addMouseListener(this);
btnStats.addMouseListener(this);
// Set the Fonts
Font buttonFont = new Font("Arial", Font.PLAIN, 20);
// Apply Fonts To Menu
btnNewInventory.setFont(buttonFont);
btnMarkDown.setFont(buttonFont);
btnProcessSales.setFont(buttonFont);
btnActive.setFont(buttonFont);
btnStats.setFont(buttonFont);
// Set Panel Layout For Menu Items
menuPanel.setLayout(new java.awt.GridLayout(1,6));
// Add the Components
menuPanel.add(btnNewInventory);
menuPanel.add(btnActive);
menuPanel.add(btnMarkDown);
menuPanel.add(btnProcessSales);
menuPanel.add(btnStats);
// Add The Menu To the Main Panel
mainPanel.add(BorderLayout.NORTH, menuPanel);
// Make Inventory Load by Default
createPanels();
makeInvVisible();
// Set It All Visible
add(mainPanel);
}
// Create the Panels Used in this applet
public void createPanels() {
NewInventory inventoryPanel = new NewInventory();
inventoryPanel.setLayout(new java.awt.GridLayout(6,2));
mainPanel.add(BorderLayout.CENTER, inventoryPanel);
SetActive setactivePanel = new SetActive();
setactivePanel.setLayout(new java.awt.GridLayout(6,2));
mainPanel.add(BorderLayout.CENTER, setactivePanel);
}
public void makeInvVisible() {
inventoryPanel.setVisible(true);
mainPanel.repaint();
}
public void makeInvDisappear() {
inventoryPanel.setVisible(false);
mainPanel.repaint();
}
public void makeActiveVisible() {
setactivePanel.setVisible(true);
mainPanel.repaint();
}
public void makeActiveDisppear() {
setactivePanel.setVisible(false);
mainPanel.repaint();
}
public void mouseClicked(MouseEvent e) {
// Determine Which Button Has Been Clicked
JButton currentButton = (JButton)e.getComponent();
// New Inventory Button
if(currentButton == btnNewInventory) {
makeInvVisible();
makeActiveDisppear();
}
// Set Active Button
else if(currentButton == btnActive) {
makeActiveVisible();
makeInvDisappear();
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
I am using the code above attempting to make a new Panel() appear and the existing panel disappear when a button is clicked by using setVisible. However, I cannot get the Panel() to change. Nothing happens when the buttons are clicked, which are set to change setVisible to false on the current Panel and true on the requested Panel.
I am fairly certain it is because the panels are set a private in a different class (initComponents). How do I access the setVisible property on the panels? Or, is this even what's wrong?
Add
this.validate();
in the makeActiveVisible() method.
Swing components have a default state of being invalid and won't be painted to the screen unless validated (by calling the .validate() method on either the component itself or on one of the parent containers).
See also
repaint() in Java

JMenuItem shortcuts Ctrl-C / Ctrl-V or Ctrl-Insert / Shift-Insert don't work anymore

I have an older stand alone java swing-based app that uses JFrame with JMenuBar containing multiple Jmenu elements (with respective JMenuItem items).
After upgrading to the latest 1.6.0_41 (or 1.7.x) JVM on Windows (7 and vista) I've noticed that the menu item with the shortcut Ctrl-C (or Ctrl-Insert) doesn't receive its ActionEvent anymore if JTable is added to the frame. The menu ActionListener is invoked if the menu is accessed by the mouse click however. The shortcut works if JTable is removed. If I change the shortcut combination to something other than Ctrl-C or Ctrl-Insert (i.e. Ctrl-L) the ActionListener is invoked.
The way it used to work (I've just confirmed it with jvm 1.4, on Windows Vista - I know it's been awhile since that environment got any serious attention :) is that Ctrl-C will perform the standard copy to clipboard function inside of the JTable if the focus was inside of an editable field. Otherwise my menu ActionListener was invoked via shortcut assigned through setAccelerator() method.
It looks like JTable implementation changed in 1.6.* to process Ctrl-C bound event differently on Windows.
Running this app on Mac OS (JVM 1.6.0_43) I can see ActionListener is invoked via Ctrl-C shortcut. Although it might be because the JTable uses Command-C instead of Ctrl-C to copy to the clipboard under Mac OS.
I've extracted the relevant portion of the code that demonstrates the problem. Any suggestions are greatly appreciated.
public class TestFrame extends JFrame {
public TestFrame(String title) {
super(title);
}
private void init() {
getContentPane().setLayout(new BorderLayout());
addMenu();
addTable();
// Change default exit operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void addTable() {
JTable jTable = new JTable(createTableModel());
// Place table in JScrollPane
JScrollPane scrollPane = new JScrollPane(jTable);
// Add Table
add(scrollPane, BorderLayout.CENTER);
}
private TableModel createTableModel() {
Object[][] data = new Object[][]{
{new Date(), "First Row, 2nd column", "First Row, 3rd column"},
{new Date(), "Second Row, 2nd column", "Second Row, 3rd column"},
};
Object[] columnNames = new Object[]{"Date", "Type", "Description"};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
public boolean isCellEditable(int row, int column) {
return column != 0;
}
};
return model;
}
private void addMenu() {
// Create the menu bar.
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);
TestActionListener listener = new TestActionListener();
JMenuItem menuItem = null;
menuItem = new JMenuItem("Copy 1");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, ActionEvent.CTRL_MASK));
menuItem.addActionListener(listener);
editMenu.add(menuItem);
menuItem = new JMenuItem("Copy 2");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
menuItem.addActionListener(listener);
editMenu.add(menuItem);
menuItem = new JMenuItem("Copy 3");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
menuItem.addActionListener(listener);
editMenu.add(menuItem);
}
public static void main(String[] args) {
TestFrame frame = new TestFrame("Test");
frame.init();
}
private static class TestActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("TestFrame.TestActionListener.actionPerformed(): e="+ e);
}
}
}
The problem is that your frame is not focused and there are no elements in your whole component hierarchy which has the focus, meaning that no one will "grab" the event and try to do something with it. Since JMenuItem's bind their shortcut to the input map JComponent.WHEN_IN_FOCUSED_WINDOW, your shortcut never "answers" the event.
To fix this, either put the focus on one of the component or directly on the JFrame (for example with frame.requestFocusInWindow();). Small example here:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class TestFrame extends JFrame {
public TestFrame(String title) {
super(title);
}
private void init() {
getContentPane().setLayout(new BorderLayout());
addMenu();
addTable();
// Change default exit operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void addTable() {
JTable jTable = new JTable();
// Place table in JScrollPane
JScrollPane scrollPane = new JScrollPane(jTable);
// Add Table
add(scrollPane, BorderLayout.CENTER);
}
private void addMenu() {
// Create the menu bar.
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);
TestActionListener listener = new TestActionListener();
JMenuItem menuItem = null;
menuItem = new JMenuItem("Copy 1");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, KeyEvent.CTRL_MASK));
menuItem.addActionListener(listener);
editMenu.add(menuItem);
menuItem = new JMenuItem("Copy 2");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK));
menuItem.addActionListener(listener);
editMenu.add(menuItem);
menuItem = new JMenuItem("Copy 3");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, KeyEvent.CTRL_MASK));
menuItem.addActionListener(listener);
editMenu.add(menuItem);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TestFrame frame = new TestFrame("Test");
frame.init();
frame.requestFocusInWindow();
}
});
}
private static class TestActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("TestFrame.TestActionListener.actionPerformed(): e=" + e);
}
}
}
Additional remarks:
Don't extend JFrame if not needed
Start your UI from the Event Dispatching Thread (EDT) by using SwingUtilities.invokeLater()
If you question is how to remove the Control+C binding from the table then you can do:
KeyStroke copy = KeyStroke.getKeyStroke("control C");
InputMap im = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.getParent().remove(copy);
However, this will remove the binding from all tables.

MyInternalFrame cannot be resolved to a type

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.

How do I build a JMenu dynamically?

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);
}
}
});

Example Program JMenubar on JInternalFrame when i Maximize the JInternalFrame

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.

Categories

Resources