Pattern for the creation of tabs of a JTabbedPane

0

I have a class AdminUI2 , which contains a JTabbedPane with 6 tabs and each tab has its respective JPanel and each JPanel contains its respective components.

The problem that arises is that each JPanel created makes the class AdminUI2 become very long, and obviously I want to avoid that the code becomes spaghetti.

This is class AdminUI2 :

  package View;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPasswordField;

public class AdminUI2 extends JFrame {

    private JLabel lblUserID;
    private JLabel lblName;
    private JLabel lblSurname;
    private JLabel lblPassword;
    private JLabel lblCodiceFiscale;

    private JTextField textFieldUserID;
    private JTextField textFieldName;
    private JTextField textFieldSurname;
    private JTextField textFieldCodiceFiscale;
    private JPasswordField textFieldPassword;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AdminUI frame = new AdminUI();
                    frame.setTitle("AdminUI");
                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AdminUI2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 574, 512);
        setTitle("User Management");

        JTabbedPane tabbedPane = new JTabbedPane();

        JPanel panel1 = new JPanel();
        lblUserID = new JLabel("UserID:");
        lblUserID.setBounds(10, 47, 37, 14);
        lblName = new JLabel("Name:");
        lblName.setBounds(10, 87, 46, 14);
        lblSurname = new JLabel("Surname:");
        lblSurname.setBounds(10, 130, 46, 14);
        lblPassword = new JLabel("Password:");
        lblPassword.setBounds(10, 176, 63, 14);
        lblCodiceFiscale = new JLabel("Codice Fiscale:");
        lblCodiceFiscale.setBounds(10, 221, 78, 14);

        textFieldUserID = new JTextField();
        textFieldUserID.setBounds(98, 44, 46, 20);
        textFieldUserID.setColumns(5);
        textFieldName = new JTextField();
        textFieldName.setBounds(98, 84, 86, 20);
        textFieldName.setColumns(10);
        textFieldSurname = new JTextField();
        textFieldSurname.setBounds(98, 127, 86, 20);
        textFieldSurname.setColumns(10);
        textFieldCodiceFiscale = new JTextField();
        textFieldCodiceFiscale.setBounds(98, 218, 176, 20);
        textFieldCodiceFiscale.setColumns(10);
        textFieldPassword = new JPasswordField();
        textFieldPassword.setBounds(98, 173, 86, 20);
        JButton btnRegisterUser = new JButton("Register");
        btnRegisterUser.setBounds(95, 292, 89, 23);
        panel1.setLayout(null);
        panel1.add(lblName);
        panel1.add(lblSurname);     
        panel1.add(lblPassword);
        panel1.add(lblCodiceFiscale);
        panel1.add(lblUserID);
        panel1.add(textFieldUserID);
        panel1.add(textFieldName);
        panel1.add(textFieldSurname);
        panel1.add(textFieldCodiceFiscale);
        panel1.add(textFieldPassword);
        panel1.add(btnRegisterUser);

        tabbedPane.addTab("Register User", panel1);     

        JPanel panel2 = new JPanel();
        tabbedPane.addTab("Register Client", panel2);

        JPanel panel3 = new JPanel();
        tabbedPane.addTab("Modify User Data", panel3);

        JPanel panel4 = new JPanel();
        tabbedPane.addTab("Users and Clients", panel4);

        JPanel panel5 = new JPanel();
        tabbedPane.addTab("Add User Parameters", panel5);

        JPanel panel6 = new JPanel();
        tabbedPane.addTab("Policies", panel6);

        getContentPane().add(tabbedPane);

    }
}
    
asked by Bryan Romero 16.10.2018 в 12:01
source

1 answer

1

The option that I like the most is to make a subclass of JPanel for each instance; that way, each class takes care of its details (the JPanel knows what it shows, the client class decides where the JPanel is included).

 public class PanelRegistroUsuario extends JPanel() {
     ... atributos
     public PanelRegistroUsuario() {
         ... código inicialización
     }

     ... getters y setters para acceder a los valores de los componentes, si es necesario.
 }

and then in your code

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Register User", new PanelRegistroUsuario());     
    
answered by 16.10.2018 / 12:06
source