Documentation

Nested Classes

One of the advanced features of JFormDesigner is the generation of nested classes. Normally, all code for a form is generated into one class. If you have forms with many components, e.g. a JTabbedPane with some tabs, it is not recommended to have only one class. If you hand-code such a form, you would create a class for each tab.

In JFormDesigner you can specify a nested class for each component. You do this in the Code Generation category in the Properties view. JFormDesigner automatically generates/updates the specified nested classes. This allows you to program more object-oriented and makes your code easier to read and maintain.

Nested Classes - Structure View   Nested Classes - Properties View

Components having a nested class are marked with a class overlay symbol in the Structure view.

Example source code:

public class NestedClassDemo
    extends JPanel
{
    public NestedClassDemo() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        tabbedPane = new JTabbedPane();
        tab1Panel = new Tab1Panel();
        tab2Panel = new Tab2Panel();

        //======== this ========
        setLayout(new BorderLayout());

        //======== tabbedPane ========
        {
            tabbedPane.addTab("tab 1", tab1Panel);
            tabbedPane.addTab("tab 2", tab2Panel);
        }
        add(tabbedPane, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTabbedPane tabbedPane;
    private Tab1Panel tab1Panel;
    private Tab2Panel tab2Panel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    //---- nested class Tab1Panel -----------------------------------------------------

    private class Tab1Panel
        extends JPanel
    {
        private Tab1Panel() {
            initComponents();
        }

        private void initComponents() {
            // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
            label2 = new JLabel();
            textField1 = new JTextField();
            CellConstraints cc = new CellConstraints();

            //======== this ========
            setBorder(Borders.TABBED_DIALOG_BORDER);
            setLayout(new FormLayout( ... ));

            //---- label2 ----
            label2.setText("text");
            add(label2, cc.xy(1, 1));

            //---- textField1 ----
            add(textField1, cc.xy(3, 1));
            // JFormDesigner - End of component initialization  //GEN-END:initComponents
        }

        // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
        private JLabel label2;
        private JTextField textField1;
        // JFormDesigner - End of variables declaration  //GEN-END:variables
    }

    //---- nested class Tab2Panel -----------------------------------------------------

    private class Tab2Panel
        extends JPanel
    {
        private Tab2Panel() {
            initComponents();
        }

        private void initComponents() {
            // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
            label3 = new JLabel();
            checkBox1 = new JCheckBox();
            CellConstraints cc = new CellConstraints();

            //======== this ========
            setBorder(Borders.TABBED_DIALOG_BORDER);
            setLayout(new FormLayout( ... ));

            //---- label3 ----
            label3.setText("text");
            add(label3, cc.xy(1, 1));

            //---- checkBox1 ----
            checkBox1.setText("text");
            add(checkBox1, cc.xy(3, 1));
            // JFormDesigner - End of component initialization  //GEN-END:initComponents
        }

        // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
        private JLabel label3;
        private JCheckBox checkBox1;
        // JFormDesigner - End of variables declaration  //GEN-END:variables
    }
}

When changing the nested class name in the Code Generation category, JFormDesigner also renames the nested class in the Java source code. When removing the nested class name, then JFormDesigner does not remove the nested class in the Java source code to avoid loss of own source code.