I Have an already given JFrame frame
, that I want to show it (insert it) into a tab
of JTabbedPane
, but that was not possible explicitely like that:
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainTabs.addTab("Editor", null, frame, null);
And the error was:
java.lang.IllegalArgumentException: adding a window to a container
I tried some solutions like to insert the frame into a JPanel
but also in vain. I have the idea to convert it into an InternalJFrame
but I don't have any idea about that.
Is that any solution to insert that frame
?
UPDATE:
I tried that soluion:
mainTabs.addTab("Editor", null, frame.getContentPane(), null);
But i lost the JMenuBar
that I added.
You can't add JFrame
(or another top-level component) to another component/container, but you can use getContentPane()
method of frame, to get main panel of your frame and add that to JTabbedPane
tab. Like next:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
frame.add(new JButton("button"));
tabs.addTab("1", frame.getContentPane());
Also you can change JFrame
to JPanel
and use that.
Read about JInternalFrame
, top-level containers.
EDIT: getContentPane()
doesn't return any decorations or JMenuBar
, this components you need to add manually, like in next example with menu:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
JMenuBar bar = new JMenuBar();
bar.add(new JMenu("menu"));
frame.setJMenuBar(bar);
frame.add(new JButton("button"));
JPanel tab1 = new JPanel(new BorderLayout());
tab1.add(frame.getJMenuBar(),BorderLayout.NORTH);
tab1.add(frame.getContentPane());
tabs.addTab("1", tab1);