3

I'm working on a project, there are JInternalFrames in the mainframe. Now, we need to let them to be JFrame. I'm considering using a JFrame to hold on JInternalFrame. The problem is that the titlebar of Internalframe is there, and user can drag it around.

Is there any way to make the Internal frame work like a pane in the JFrame? After searching on the Internet, I found somebody removes the titlepane.

Do you have any good idea on this?

Thanks you!

update: Maybe I was on the wrong track. The real problem is the JInternal frame can not get out of the main Frame, or any way to make it look like it's out side of the frame?

xiaohei
  • 97
  • 1
  • 9
  • Simplest thing to do would be to remove the JDesktopPane and replace it with the content pane of your JInternalFrame. There is no method such as `JInternalFrame.setUndecorated()`. – Guillaume Polet Jan 30 '13 at 09:19

2 Answers2

4

Is there any way to make the Internal frame work like a pane in the JFrame

Im not sure by what you mean by pane, but I guess like a JPanel? Of course you can but why, would be my question, unless you want some sort of quick floating panel, but than you say you dont want it draggable? So Im bit unsure of your motives and makes me weary to answer....

The problem is that the titlebar of Internalframe is there

Well Here is code to remove the titlepane (found it here):

//remove title pane http://www.coderanch.com/t/505683/GUI/java/JInternalframe-decoration
BasicInternalFrameTitlePane titlePane =(BasicInternalFrameTitlePane)((BasicInternalFrameUI)jInternalFrame.getUI()).getNorthPane();
jInternalFrame.remove(titlePane);

and user can drag it around.

And I found this to make JInternalFrame unmovable by removing the MouseListeners which make it movable, but it is important to note its not necessary to remove the MouseListeners as the method used to make it undraggable will remove the NorthPane which the MouseListener is added too thus its unnecessary for us to remove it ourselves.:

//remove the listeners from UI which make the frame move
BasicInternalFrameUI basicInternalFrameUI = ((javax.swing.plaf.basic.BasicInternalFrameUI) jInternalFrame.getUI());
for (MouseListener listener : basicInternalFrameUI.getNorthPane().getMouseListeners()) {
    basicInternalFrameUI.getNorthPane().removeMouseListener(listener);
}

And as per your title:

how to make JInternalFrame fill the Container

Simply call setSize(int width,int height) on JInternalFrame with parameters of the JDesktopPanes width and height (JDesktopPane will be sized via overriding getPreferredSize()).

Which will give us this:

enter image description here

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.MouseListener;
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;

/**
 *
 * @author David
 */
public class Test {

    public Test() {
        createAndShowGUI();
    }

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

            }
        });
    }

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

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

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

        createAndAddInternalFrame(jdp);

        frame.setVisible(true);
    }

    private void createAndAddInternalFrame(final JDesktopPane jdp) {
        JInternalFrame jInternalFrame = new JInternalFrame("Test", false, false, false, false);
        jInternalFrame.setLocation(0, 0);
        jInternalFrame.setSize(jdp.getWidth(), jdp.getHeight());

        //remove title pane http://www.coderanch.com/t/505683/GUI/java/JInternalframe-decoration
        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
        jInternalFrame.remove(titlePane);

        /*
         //remove the listeners from UI which make the frame move
         BasicInternalFrameUI basicInternalFrameUI = ((javax.swing.plaf.basic.BasicInternalFrameUI) jInternalFrame.getUI());
         for (MouseListener listener : basicInternalFrameUI.getNorthPane().getMouseListeners()) {
         basicInternalFrameUI.getNorthPane().removeMouseListener(listener);
         }
         */
        jInternalFrame.setVisible(true);

        jdp.add(jInternalFrame);
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Sorry, I'm not a native speaker, and the post is not clear. I find your answer might be what I'm looking for, I'll try it and come back. Thank you!!! – xiaohei Jan 31 '13 at 01:28
  • Hi, I'm working your code on a mac machine, it seems getNorthPane returns null. How to get the title bar on mac? Is it possible to make the JInterFrame resize according to the JFrame? – xiaohei Jan 31 '13 at 03:54
2

Given your requirements, I suggest you just use a simple JPanel inside your JFrame content pane.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • 1
    +1 yup could be what the OP needs as he/she could than draw their own `Border` and change the background to make it look like `JInternalFrame` if thats what he/she is after. – David Kroukamp Jan 30 '13 at 11:55
  • Hi, I'm trying to replace some JInternalFrame s to JFrame as a user want to drag them out of the main window. But the JInternalFrame is quite complicated, it's hard to convert it to a JFrame. So I thought it would be easier to put them into another JFrame. After the modification, it works fine. But there are issues with UI. One is what I described in the post. Others might be some event transferings from JFrame to its inner JInternalFrame. Maybe you have better ideas about converting? – xiaohei Jan 31 '13 at 01:39