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
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.
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.
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.
The short answer would be as follows:
- Open the main panel in Design view.
- Go to Navigator tab.
- Right click on Form MainPanel and choose Reload Form option.
- The main panel is updated.
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):
"The problem is that jPanel1 in MyFrame has a layout code generated
(GroupLayout) - setting layout causes the components are removed. The
GUI builder unfortunately does not know there is a different container
with its own layout since it is specified only via custom code and not
really instantiated in the GUI form. However you can fix this easily
- just set the layout of the jPanel1 in MyFrame to Flowlayout. This is default layout of JPanel, so the GUI builder will not generate any
layout code and your custom panel will be left intact."
So, it isn't very clear why GroupLayout causes this behavior but empirically to solve it you would have to:
- Set a different layout manager to the custom panels (i.e.: FlowLayout or GridBagLayout).
- Add those panels to the main panel. If it already has those panels, remove them and add them again. It is important that custom panels have at least one component.
- Once previous steps are done, then custom panels layout manager can be set back to GroupLayout (Free design) and it still works.
- When a custom panel is modified, it has to be compiled (F9) and main panel form needs to be updated through Navigator explorer.
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):
public class CustomPanel extends javax.swing.JPanel {
public CustomPanel() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton1.setText("jButton1");
add(jButton1);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
4. Create the main panel
Follow step 1 to create a new JPanel
and name it MainPanel
.
5. Add CustomPanel
to MainPanel
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 :)