3

I have three buttons close,min and max.
When i want to max it then it will take the shape of the main container and overlaps all the panel and when i close it then only that panel gets affected. But when i hit the min button it gets minimized to the task bar which i do not want.
I want it inside conatiner like that of internalFrame when you click the minimize the button then it gets minimized inside the main frame.

here is the code

package Project;

import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;


public class MinPanel {

    public MinPanel() {
        createAndShowGui();
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MinPanel();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setSize(300, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel mainPanel = new JPanel(new GridLayout(2, 2));

        for (int i = 0; i < 4; i++) {
            final int num = i;
            OmniPanel op = new OmniPanel(mainPanel, frame) {
                @Override
                public JPanel createPanel() {
                    JPanel p = createSimplePanelInterface();
                    p.add(new JLabel("Panel " + (num + 1)));
                    return p;
                }

                @Override
                void toPanel() {
                    super.toPanel();
                    System.out.println("Frame requested to be brought to panel");
                }
            };
            mainPanel.add(op.getPanel());
        }

        frame.add(mainPanel);

        //frame.pack();
        frame.setVisible(true);
    }
}

abstract class OmniPanel {

    protected JFrame frame;
    protected JPanel panel;
    boolean maximized = false;
    private final JComponent owner;
    private final JFrame ownerFrame;

    public OmniPanel(JComponent owner, JFrame con) {
        this.owner = owner;
        initOmniPanel();
        this.ownerFrame = con;
    }

    private void initOmniPanel() {
        panel = createPanel();
        createFrame();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowDeiconified(WindowEvent we) {
                super.windowDeiconified(we);
                toPanel();
            }
        });
    }

    public JPanel getPanel() {
        return panel;
    }

    public JFrame getFrame() {
        return frame;
    }

    public boolean goFrame() {
        frame.add(panel);
        frame.pack();
        frame.setState(JFrame.ICONIFIED);
        frame.setVisible(true);
        return true;
    }

    protected void createFrame() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    void toPanel() {
        frame.remove(panel);
        frame.dispose();
        owner.add(panel);
        owner.revalidate();
        owner.repaint();
    }

    public JPanel createSimplePanelInterface() {
        JPanel p = new JPanel();
        JButton close = new JButton("X");
        JButton minimize = new JButton("_");
        JButton maximize = new JButton("[]");
        p.add(close);
        p.add(minimize);
        p.add(maximize);
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    maximized = false;
                    ownerFrame.setGlassPane(new JComponent() {
                    });
                    ownerFrame.revalidate();
                    ownerFrame.repaint();
                } else {
                    removePanelFromOwner();
                    getFrame().dispose();
                }
            }
        });
        minimize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    maximized = false;
                   ownerFrame.setGlassPane(new JComponent() {
                    });

                    owner.add(panel);
                    owner.revalidate();
                    owner.repaint();
                    ownerFrame.revalidate();
                    ownerFrame.repaint();
                } else {
                    removePanelFromOwner();
                    goFrame();


                frame.setState(Frame.ICONIFIED);
                }
            }
        });
        maximize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    return;
                }
                maximized = true;
                removePanelFromOwner();
                ownerFrame.setGlassPane(panel);
                ownerFrame.revalidate();
                ownerFrame.repaint();
                panel.setVisible(true);//
            }
        });
        p.setBorder(new LineBorder(Color.black));
        return p;
    }

    private void removePanelFromOwner() {
        owner.remove(getPanel());
        owner.revalidate();
        owner.repaint();
    }

    abstract JPanel createPanel();
}
oliholz
  • 7,447
  • 2
  • 43
  • 82
  • 1
    I think that similair way how is described in your last question, I'd be use BoxLayout (accepting min, max and preferred size) or GridBagLayout – mKorbel Feb 14 '13 at 07:35
  • @mKorbel does it matter what kind of layout i should use.I want the panel to get minimized with in container not to the task bar.Please run this code and you will understand what i mean to say –  Feb 14 '13 at 07:38
  • Is there a reason why your iconified panel is added to a top-level-frame ? The `frame.setState(Frame.ICONIFIED)` is only a platform depended mechanism for the 'taskbar' – oliholz Feb 14 '13 at 07:48
  • 2
    Windows can't contain other Windows. `frame.setState(Frame.ICONIFIED)` is doing exactly what it should do. – MadProgrammer Feb 14 '13 at 07:58
  • @oliholz so do you think that this is not possible.I had create internalframe instead of panel –  Feb 14 '13 at 08:16
  • @MadProgrammer then tell me how to do please –  Feb 14 '13 at 08:17
  • On the [original question](http://stackoverflow.com/questions/14807095/how-to-minimize-one-panel-out-of-four-panel/14810481) I proposed JDesktopPane, and JInternalFrame which would allow everything by default. but you said this is not what you wanted it should minimize to taskbar... but Have a look at it/comments on your original question – David Kroukamp Feb 14 '13 at 08:22
  • @DavidKroukamp see it was asked to design in that way so i said you .Now they are asking to minimize it within the same frame so what would i do now? –  Feb 14 '13 at 08:32
  • 2
    Use a `JDesktopPane` and `JInternalFrames` which by default gives you what you want (minimized maximize and close features are built into the `JInteralFrame`). See [here](http://stackoverflow.com/a/13815721/1133011) for an example. To make `JInternalFrame` look like a `JPanel` [see here](http://stackoverflow.com/a/14603306/1133011). Now you would simply add your own buttons which would be like wrappers for the `JInternalFrame`s minimized maximized and close buttons i.e see [here](http://stackoverflow.com/a/6476636/1133011) which shows how to programatically minimize JInternalFrame – David Kroukamp Feb 14 '13 at 08:39
  • @DavidKroukamp thanks for suggesting a new idea.I am trying now but if i stuck anywhere then can i ask you again? –  Feb 14 '13 at 08:41
  • sure... the solution is straight out of the head (could not work As I do forsee problems with minimzed internalframe which does not have a NorthPane i.e titlebar with max min and close, you would have to create your own north pane not buttons) or alteratively scrap the *your own buttons* thing as really unless it is a requirement as IMO its actually duplicating code which is already there)... but ill try put it to some code – David Kroukamp Feb 14 '13 at 08:43
  • @DavidKroukamp what is IMO? –  Feb 14 '13 at 08:49
  • @DavidKroukamp i would be grateful to you if you please post the code here –  Feb 14 '13 at 08:55
  • @sukant see the answer of javaprogrammer. Looks like he got it down nicely... – David Kroukamp Feb 14 '13 at 08:57
  • @DavidKroukamp yeh i am trying with it but i am not getting multiple frames –  Feb 14 '13 at 08:58
  • @sukant change `setLocation(0,0)` to something that has space for all internalframes i.e make `createAndAddInternalFrame(..)` accept `x` and `y` and use thats as `setLocation(..)` parameters. Or else right now each frame will appear on top of the other. Simply minimize one frame and the other should show – David Kroukamp Feb 14 '13 at 09:01
  • @DavidKroukamp 2nd frame is totally not adding –  Feb 14 '13 at 09:06
  • @sukant please it out? whats that I see the problem is JInternalFrame also fills the container. I updated java programmers answer please see it – David Kroukamp Feb 14 '13 at 09:24
  • @DavidKroukamp sorry for the previous comment –  Feb 14 '13 at 09:38
  • @DavidKroukamp i have done it but i need a little help.Actually the layout is not proper.If you do not mind can you please check it –  Feb 14 '13 at 10:06
  • @DavidKroukamp i am waiting for your reply –  Feb 14 '13 at 10:33

1 Answers1

3

I would suggest you to create an internalframe and then add buttons.If you do not want to add buttons then you make internalframe arguments true

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class MinPanel {

    public MinPanel() throws HeadlessException, PropertyVetoException {
        createAndShowGUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new MinPanel();
                } catch (HeadlessException ex) {
                } catch (PropertyVetoException ex) {
                }

            }
        });
    }

    private void createAndShowGUI() throws HeadlessException, PropertyVetoException {
        JFrame frame = new JFrame();
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JDesktopPane jdp = new JDesktopPane() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
        };

        frame.setContentPane(jdp);
        frame.pack();

        createAndAddInternalFrame(jdp, 0, 0);
        createAndAddInternalFrame(jdp, 200, 0);

        frame.setVisible(true);
    }

    private void createAndAddInternalFrame(final JDesktopPane jdp, int x, int y) throws PropertyVetoException {
        final JInternalFrame jInternalFrame = new JInternalFrame("Test1", false, false, false, false);
        jInternalFrame.setLocation(x, y);

        jInternalFrame.setLayout(new GridLayout(2, 2));
        jInternalFrame.setSize(200, 200);//testing
        JButton jb = new JButton("min");
        jInternalFrame.add(jb);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    jInternalFrame.setIcon(true);
                } catch (PropertyVetoException ex) {
                }

            }
        });
        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
        jInternalFrame.remove(titlePane);


        jInternalFrame.setVisible(true);
        jdp.add(jInternalFrame);
    }
}
  • I edited your answer to show OP whats happening (i.e make JInternalFrame smaller and make createAndAddInternalFrame accept x and y parameters). Please feel free to revert – David Kroukamp Feb 14 '13 at 09:27