lundi 26 octobre 2020

How to reuse a common panel?

I have a panel for adding an item to the cart. For example:

public class SomePanel extends JPanel {

    private static final long serialVersionUID = 1L;

    private JLabel firstLabel;
    private JLabel secondNameLabel;
    private JLabel thirdLabel;
    // and more..


    private JTextField firstTextField;
    private JTextField firstTextField;
    private JTextField thirdTextField;
    // and more..


    private void init() {
        firstLabel = new JLabel("First label");
        secondLabel = new JLabel("Second label");
        thirdLabel = new JLabel("Third label");     
        // and more..

        firstTextField = new JTextField("first field");
        secondTextField = new JTextField("second field");
        thirdTextField = new JTextField("third field");
        // and more..
    }

This panel is located in the dialog. When I select "Add" in the menu, a dialog and this panel appear. There I can enter information about the product and add the product.

The problem is that I have another three areas on the main form. These areas also display the same panel as above. How can I reuse an existing SomePanel and would this be a good practice? Maybe it's better to create singletons for each element (JLabel and JTextField) instead? Maybe there is some special pattern for solving this problem?

Aucun commentaire:

Enregistrer un commentaire