How to refresh custom bean in NetBeans?

2.1k views Asked by At

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

1

There are 1 answers

1
dic19 On BEST ANSWER

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:

  1. Open the main panel in Design view.
  2. Go to Navigator tab.
  3. Right click on Form MainPanel and choose Reload Form option.
  4. 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:

  1. Set a different layout manager to the custom panels (i.e.: FlowLayout or GridBagLayout).
  2. 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.
  3. Once previous steps are done, then custom panels layout manager can be set back to GroupLayout (Free design) and it still works.
  4. 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 FileSwing GUI FormsJPanel. 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.

Set layout manager to FlowLayout

3. Add some component to the panel and then compile the file

Adding a JButton to the panel

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.

Adding CustomPanel to MainPanel

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:

Added CustomPanel to MainPanel

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.

CustomPanel modified

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:

Reload MainPanel Form

Awesome, the main panel is now updated!

MainPanel 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 :)