I want to design a relatively complex UI in NetBeans IDE. I believed that bring all these components together in one Panel can intensify difficulty of maintenance of such Panel.
For this purpose I've broken down this structure in multiple custom beans extending JPanel
. And in the main Panel I've included them by Choose Bean button in the palette toolbar.
Every things looks OK but when I change one of the child beans, the change does not take place on the imported custom bean in main Panel.
Please point me out how can I reload that bean? Thanks
Just as a starter I'd like to say it is a perfectly valid apporach and for future visitors, you are following the steps described here to add your panels to the main panel. The same can be done via drag & drop the bean directly from the Projects explorer to the Design view.
The short answer would be as follows:
However it doesn't work and it seems there is a long standing issue with a bug report on this matter where they told it's not a bug but a layout manager related stuff (not very clear explanation though):
So, it isn't very clear why GroupLayout causes this behavior but empirically to solve it you would have to:
Before go any further I'd suggest you don't set back the layout manager to GroupLayout (step 3) and use GridBagLayout instead. This is not my favorite either but IMHO it's a better choice than GroupLayout. You might want to take a look to the reasons against GroupLayout exemplified here and here (I'm sure there are more about it here in SO).
Now, please consider the following example (sorry for the extension).
1. Create a custom panel
Go to New File → Swing GUI Forms → JPanel. Name it
CustomPanel
2. Set the layout manager to FlowLayout
Note: where it says Default it doesn't mean it's the default used but the default por
JPanel
class. As I've said the layout manager used by default is GroupLayout.3. Add some component to the panel and then compile the file
Note the generated code should look like this (no GroupLayout at all):
4. Create the main panel
Follow step 1 to create a new
JPanel
and name itMainPanel
.5. Add
CustomPanel
toMainPanel
Either by using palette and Custom Bean or by drag and drop from the Projects explorer and save the changes.
Note: you can resize this panel if you want. If you go to Navigator tab you'll see the custom panel inside the main panel:
6. Modify the custom panel
Set the layout back to GroupLayout (Free design) and modify it as needed. Save the changes and compile the file.
7. Reload main panel's form
Go to
MainPanel
design view: it still unchanged. Go to Navigator tab and reload the main panel's form:Awesome, the main panel is now updated!
From now, every time we modify the custom panels we have to compile them and reload main panel's form (only steps 6 and 7 :)