4

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 When the JInternalFrame is Maximized

When JInternalFrame is Minimized

Please provide me an Example Program in Java Swing

Zaheer
  • 123
  • 5
  • 14

2 Answers2

6

Seems to work fine, please post SSCCE to show specific problem:

enter image description here

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);
        }
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Sir i have tried the above code but when i maximize the JInternalFrame it is not capturing the JMenuBar so any solution that i can try in the above code – Zaheer Nov 30 '12 at 19:51
  • @ZaheerBoovaji Please see and try updated code... Does it work for you? you must not forget to add `JDesktopPane` and set that as your `contentPane`, all will than be added to the `desktopPane` – David Kroukamp Nov 30 '12 at 20:10
  • Sir I have tried the updated code What i want is When i maximize the JInternalFrame the JMenuBar should set on JinternalFrame but not on the JFrame – Zaheer Nov 30 '12 at 20:16
  • @ZaheerBoovaji that should not be done it violates coventions IMO. Hence why the `JDesktopPane` will sort out the `JMenuBar` for us – David Kroukamp Nov 30 '12 at 20:19
  • This answer doesn't actually do what the OP asked for. The OP wanted a maximizable `JInternalFrame` that would, for lack of a better description, "merge" the title bar with the menu bar. Notice in the OP's first image the three component control buttons are located on the menu bar when the component is maximized, but when it is not maximized it takes the controls with it. This SSCCE doesn't do that. – ryvantage Jan 23 '14 at 23:37
0

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.