Code-Apprentice
Code-Apprentice

Reputation: 83577

Inheritting from custom JPanel using NetBeans GUI Builder

ContainerPanel is a custom JPanel class using a BorderLayout. The SOUTH contains a JPanel with a button. I want the CENTER to be an instance of another custom JPanel, say AbstractPanel, which provides an abstract method which will be called when the button is clicked. I also want to set this JPanel programmatically (at run-time). So far, I can do all of this as you can see in the following code (some of which is generated by the NetBeans GUI Builder):

package jpaneldemo;

import java.awt.BorderLayout;

public class ContainerPanel extends javax.swing.JPanel {

    public ContainerPanel() {
        initComponents();
    }

    public ContainerPanel(AbstractPanel abPanel) {
        initComponents();

        this.abPanel = abPanel;
        this.add(this.abPanel, BorderLayout.SOUTH);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        add(buttonPanel, java.awt.BorderLayout.PAGE_END);
    }

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
        this.abPanel.abstractMethod();
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton okButton;
    // End of variables declaration

    private AbstractPanel abPanel = null;

}

I also created the AbstractPanel class:

package jpaneldemo;

public abstract class AbstractPanel extends javax.swing.JPanel {

    public AbstractPanel() {
        initComponents();
    }

    protected abstract void abstractMethod();

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }
    // Variables declaration - do not modify
    // End of variables declaration
}

Now I want to create subclasses of this AbstractPanel class which I can edit in the NetBeans GUI. Typically, I right-click on a package name in the Projects window and then navigate to "New -> JPanel..." to create a custom JPanel. How do I get AbstractPanel to appear in the "New" menu so that I can edit the new class with the NetBeansGUI Builder? Or is there another way to accomplish the same thing?

Upvotes: 2

Views: 2975

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

If your intention is to provide a "template" component that can then be added to the palette and included in other containers, then yes you can.

Have a read through FaqFormCustomContainerBean

The basic idea (apart from creating a BeanDescriptor is you will need to provide a "content" panel of some kind, where additional content can be added at design time.

Now, if you're interested in providing a custom template, that's something I've not done before.

You could try reading through http://netbeans.org/competition/win-with-netbeans/customize-java-template.html. It may be a little out of date, but might help you in the right direction

Upvotes: 5

Related Questions