Inserting the panel from a form into another form

I've build a form using the UI builder that I would like to use in a couple of other forms. Since the top level of a form is a JPanel, I figured that I could just insert that JPanel into the other form. Unfortunately, nothing that I've tried has worked.

For what follows, the form that I'd like to use in a couple of other forms is called SubForm and the first form that I would like to insert it in is called MainForm.

My best shot (I think) was to define a JPanel called _xxxPanel in MainForm with the custom create flag set. I then created that JPanel in mainForm's implementation class as follows:

private void createUIComponents() {
xxxPanel = new SubForm().panel1; // not pretty but semantically equivalent to what I really did
}

Notes:
1. MainForm is declared to extend JFrame and the constructor
uses "setContentPane()" to make MainForm's top level panel the
contents of the JFrame.
2. _panel1 is the very top level JPanel in SubForm.

What I get when I try to create an instance of MainPanel is

Exception in thread "main" java.lang.IllegalArgumentException: component was not added: javax.swing.JPanel[,0,0,0x0,invalid,layout=com.intellij.uiDesigner.core.GridLayoutManager,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.TitledBorder@df83e5,flags=9,maximumSize=,minimumSize=,preferredSize=]
at com.intellij.uiDesigner.core.AbstractLayout.getConstraintsForComponent(AbstractLayout.java:209)
at com.intellij.uiDesigner.core.GridLayoutManager.checkSetSizesFromParent(GridLayoutManager.java:536)
at com.intellij.uiDesigner.core.GridLayoutManager.layoutContainer(GridLayoutManager.java:401)
at java.awt.Container.layout(Container.java:1401)
at java.awt.Container.doLayout(Container.java:1390)
at java.awt.Container.validateTree(Container.java:1473)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validate(Container.java:1448)
at java.awt.Window.pack(Window.java:495)
at com.invidi.cabot.cartier.MainForm.]]>(MainForm.java:72)
at com.invidi.cabot.cartier.MainForm.main(MainForm.java:177)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: component was not added: javax.swing.JPanel[,0,0,0x0,invalid,layout=com.intellij.uiDesigner.core.GridLayoutManager,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.TitledBorder@df83e5,flags=9,maximumSize=,minimumSize=,preferredSize=]
at com.intellij.uiDesigner.core.AbstractLayout.getConstraintsForComponent(AbstractLayout.java:209)
at com.intellij.uiDesigner.core.GridLayoutManager.checkSetSizesFromParent(GridLayoutManager.java:536)
at com.intellij.uiDesigner.core.GridLayoutManager.layoutContainer(GridLayoutManager.java:401)
at java.awt.Container.layout(Container.java:1401)
at java.awt.Container.doLayout(Container.java:1390)
at java.awt.Container.validateTree(Container.java:1473)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validate(Container.java:1448)
at java.awt.Window.dispatchEventImpl(Window.java:1772)
at java.awt.Component.dispatchEvent(Component.java:3869)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Is what I'm trying to do possible? If so, how might I go about doing it?

I have also tried:

private void createUIComponents() {
_xxxPanel = new JPanel();
xxxPanel.add( new SubForm().panel1 ); // not pretty but semantically equivalent to what I really did
}

I also tried the same thing without setting the custom create flag on _xxxPanel (i.e. _xxxPanel is just an empty JPanel in the form designer GUI and the xxxPanel.add( new SubForm().panel 1) is in MainForm's constructor instead of in createUIComponents).

I've also tried a few other things which are pretty hard to explain (I will explain them if folks are interested). Nothing has worked.

-Danny

2 comments

Hello Daniel,

I've build a form using the UI builder that I would like to use in a
couple of other forms. Since the top level of a form is a JPanel, I
figured that I could just insert that JPanel into the other form.
Unfortunately, nothing that I've tried has worked.


Have you actually tried using the nested forms feature which was specifically
designed for this case? You can either add your SubForm to the palette, or
use the "Non-Palette Component..." item on the palette and select your form
in the "Form" field if you do not intend to use this form often.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

No. I wasn't aware of the feature. Thanks.

-Danny

0

Please sign in to leave a comment.