1

I need some help in JInternalFrame within JPanel's Area.I have a JFrame which contains JPanel added to its ContentPane.JFrame Contains Menu when i click one of its Menu item i need JInternal Frame to be added on top of the contentpane.The Code i have given so far,

    JDesktopPane desktop = new JDesktopPane();
    desktop.setLayout(new BorderLayout());

    JPanel topPanel = new JPanel();
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0 };
    gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0 };
    gbl_contentPane.columnWeights = new double[] { 1.0, 6.0, 1.0,
            Double.MIN_VALUE };
    gbl_contentPane.rowWeights = new double[] { 0.0, 8.0, 0.0,
            Double.MIN_VALUE };
    topPanel.setLayout(gbl_contentPane);

    JPanel left = new JPanel();
    GridBagConstraints gbc_left = new GridBagConstraints();
    gbc_left.insets = new Insets(0, 0, 5, 5);
    gbc_left.fill = GridBagConstraints.BOTH;
    gbc_left.gridx = 0;
    gbc_left.gridy = 1;
    topPanel.add(left, gbc_left);

    JPanel middle = new JPanel();
    GridBagLayout gbl_middle = new GridBagLayout();
    gbl_middle.columnWeights = new double[] { 1.0 };
    middle.setLayout(gbl_middle);
    GridBagConstraints gbc_middle = new GridBagConstraints();
    gbc_middle.insets = new Insets(0, 0, 5, 5);
    gbc_middle.fill = GridBagConstraints.BOTH;
    gbc_middle.gridx = 1;
    gbc_middle.gridy = 1;
    topPanel.add(middle, gbc_middle);

    GridBagConstraints gbc = new GridBagConstraints();
    JPanel panel1 = new JPanel();
    Border eBorder = BorderFactory.createEtchedBorder();
    panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = gbc.weighty = 30;
    middle.add(panel1, gbc);
    panel1.setLayout(new MigLayout("", "[944.00,grow][353.00]",
            "[6.00][128.00,grow][]"));

    /*lblHeader = new JLabel(
            "<html>Indira Institute of Technology<br>Tatabad<br>Karpagam Complex Stop<br>Coimbatre</html>");
    lblHeader.setIcon(new ImageIcon(
            "C:\\Users\\Prakash\\Desktop\\images.jpg"));
    panel1.add(lblHeader, "cell 0 1 2 1,alignx center,aligny center");*/

    JPanel panel2 = new JPanel();
    gbc = new GridBagConstraints();
    panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
    gbc.gridy = 1;
    gbc.gridwidth = gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = gbc.weighty = 70;
    gbc.insets = new Insets(2, 2, 2, 2);
    middle.add(panel2, gbc);
    panel2.setLayout(new MigLayout(
            "",
            "[30px][69.00px][144.00][68.00][][159.00px][59.00px][65px][28.00][]",
            "[20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][30.00][48.00:n,grow 50,shrink 50]"));

    getContentPane.add(topPanel);

I have never used the DesktopPane in this(I don't know how to make use of this in this situation) And The Screen So far is as follows, a busy cat

Now I need the JInternalFrame to be added for the Previous Screen as Follows, a busy cat

I am aware that i can only be able to add a JInternalFrame to the DesktopPane.But i Already Filled my ContentPane with JPanel to show its content.How can i achieve Jinternal Frame to be added in this JFrame.Kindly give your valuable suggestions.

Community
  • 1
  • 1
Prakash
  • 61
  • 7

2 Answers2

2

Not really the right direction. You original panel is under the control of layout manager, this means that when you add the JInternalFrame to it, the layout manager wants to try and layout it out.

Generally speaking, a JInternalFrame wants to reside in a container which is unmanaged, allowing it to be positioned and sized independently of the content.

A possible solution might be to take advantage of the glass pane for the JInternalFrame instead, for more details see How to Use Root Panes

Another solution might be to use a JLayeredPane. Basically, you would start by setting the layout manager of the JLayeredPane to something link BorderLayout add the first panel to it and then add a second, transparent pane, with no layout, above it. You would add the JInternalFrames to this second panel.

See How to Use Layered Panes for more details

The question that jumps out at me though is...why? Why wouldn't you just use some kind of dialog instead? See How to Make Dialogs for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yup i can use dialogs.But If its a dialog it'd completely take control over focus.We can't minimize it,we can't open another instance of it.right?So why i am choosing JinternalFrame. – Prakash Oct 03 '14 at 07:28
  • Technically, you should be able to create as many instances of the application as you want, assuming you mean creating a new instance of the JVM. You could setup a non-modal dialog, but then you have to ask the question of why? What is it you are trying to achieve. A dialog is a great way to get quick information from the user which can then be dismissed... – MadProgrammer Oct 03 '14 at 07:31
  • sorry.I didnt mean instance of an application.What do i really need is,Consider i need a registration form to be opened when i click on a menu item.If i use dialog i can be able to open only one registration form at a time.isn't it?But what do i need is user can open any number of login screens.He doesn't have complete one screen before he opens another.This is the situation i am working.Please guide me in a right direction. – Prakash Oct 03 '14 at 07:38
  • Start by taking a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) and you can restrict the modility of the dialog to a single frame (from memory), see [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) and [How to Use Modality in Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html) for more details – MadProgrammer Oct 03 '14 at 07:44
0

What is it what you really want?

You wrote you already have your content pane added to your frame. JDesktopPane has to have its own space reserved. If you don't have or you don't want to reserve space for the internal frames in your main frame, then maybe you don't even want it to be part of the main frame. In this case you might want to use child JDialogs instead of JInternalFrames.

So either add your JDesktopPane to your main frame (having its space reserved) or use child JDialogs which can be modal or not and can overlap any part of the main frame. JInternalFrames are only visible in the area of the JDesktopPane while child JDialogs can "float" over your main frame.

Check out this Oracle documentation: How to Use Internal Frames

And How to Make Dialogs.

icza
  • 389,944
  • 63
  • 907
  • 827