Skip to content
Advertisement

Is there a way to hide all elements in JFrame

I’m currently working on Java application and I’m using JFrame and inside of JFrame i have some buttons, labels and textfields. I’m in need of hiding all the elements that are in JFrame. I don’t need to hide JFrame itself, only the elements in it so I can display some others. Is there a way to hide them all without manually writing element.setVisibility(false); for every single one of them?

import javax.swing.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
import java.util.ArrayList;
public class GraphicUserInterface implements ActionListener {
    /**
     * Constructor for objects of class GraphicUserInterface
     */
    private JFrame window;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
   
    private JLabel title;
    private JLabel username;
    private JLabel password;
    private JTextField textF1;
    private JPasswordField passF;
    private JTextArea title2;
    
    
    private int prevMenu;
    private int menu ;

    private Admin admin;

    public GraphicUserInterface() {
        // initialise instance variables
        this.admin = new Admin();
        this.window = new JFrame();
        this.button1 = new JButton();
        this.button2 = new JButton();
        this.button3 = new JButton();
        this.button4 = new JButton();
        this.button5 = new JButton();
        this.button6 = new JButton();
        
        this.title2 = new JTextArea();
        
        this.title = new JLabel();
        this.username = new JLabel();
        this.password = new JLabel();
        this.textF1 = new JTextField();
        this.passF = new JPasswordField();
        this.button1.addActionListener(this);
        this.button2.addActionListener(this);
        
    }

    public void start() {
        
        switch(this.menu) {
            case 0:
            this.window.setTitle("Študentský systém");
            this.button1.setText("Admin");
            this.button2.setText("Študent");
            this.title.setText("Zvoľ typ prihlásenia"); 

            this.title.setBounds(130,50,200,70);
            this.title.setFont(new Font("Calibri", Font.BOLD, 20));

            this.button1.setBounds(140,130,120,55);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,5));

            this.button2.setBounds(140,215,120,55);
            this.button2.setBackground(new Color(156,156,156));
            this.button2.setBorder(new LineBorder(Color.black,5));
            this.button2.setForeground(Color.white);

          
            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.title); 
            visOfUIElements(false);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(400,400);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            

            //this.button1.addActionListener(this);
                    
            //this.button2.addActionListener(this);
            break;
            case 1:
            
            this.window.setTitle("Študentský systém");
            this.button1.setText("Prihlásiť");
            this.button2.setText("Späť");
            this.title.setText("Prihlásenie Admin");
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);
            this.username.setText("Login:");
            this.password.setText("Heslo:");
            
            this.title.setBounds(100,50,200,70);
            this.title.setFont(new Font("Calibri", Font.BOLD, 20));
            
            this.username.setBounds(80,100,50,70);
            this.username.setFont(new Font("Calibri", Font.BOLD, 15));
       
            this.password.setBounds(80,130,50,70);      
            this.password.setFont(new Font("Calibri", Font.BOLD, 15));

            this.button1.setBounds(130,180,60,30);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,3));

            this.button2.setBounds(195,180,60,30);
            this.button2.setBackground(new Color(156,156,156));
            this.button2.setBorder(new LineBorder(Color.black,3));
            this.button2.setForeground(Color.white);
            
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);

          
            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.title); 
            this.window.add(this.textF1);
            this.window.add(this.passF);
            this.window.add(this.username);
            this.window.add(this.password);
            visOfUIElements(true);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(400,400);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            

            
            break;
            case 2:
            
            this.window.setTitle("Študentský systém");
            this.button1.setText("Prihlásiť");
            this.button2.setText("Späť");
            this.title.setText("Prihlásenie Študent");
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);
            this.username.setText("Login:");
            this.password.setText("Heslo:");
            
            this.title.setBounds(100,50,200,70);
            this.title.setFont(new Font("Calibri", Font.BOLD, 20));
            
            this.username.setBounds(80,100,50,70);
            this.username.setFont(new Font("Calibri", Font.BOLD, 15));
       
            this.password.setBounds(80,130,50,70);      
            this.password.setFont(new Font("Calibri", Font.BOLD, 15));

            this.button1.setBounds(130,180,60,30);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,3));

            this.button2.setBounds(195,180,60,30);
            this.button2.setBackground(new Color(156,156,156));
            this.button2.setBorder(new LineBorder(Color.black,3));
            this.button2.setForeground(Color.white);
            
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);
            
            this.textF1.setText("Marek");
          
            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.title); 
            this.window.add(this.textF1);
            this.window.add(this.passF);
            this.window.add(this.username);
            this.window.add(this.password);
            visOfUIElements(true);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(400,400);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            
            break;
            case 3:
            this.window.setTitle("Študentský systém");
            this.button1.setText("Pridať študenta");
            this.button2.setText("Vyhľadať študenta");
            this.button3.setText("Pridať predmet");
            this.button4.setText("Pridať odbor");
            this.button5.setText("Pridať fakultu");
            this.button6.setText("Odhlásiť");
            
            this.title.setText("Admin");
            this.textF1.setBounds(325,125,125,20);
            this.passF.setBounds(325,155,125,20);
            this.username.setText("Login:");
            this.password.setText("Heslo:");
            
            this.title2.setBounds(380,50,200,200);
            this.title2.setFont(new Font("Calibri", Font.BOLD, 20));
            
            this.username.setBounds(80,100,50,70);
            this.username.setFont(new Font("Calibri", Font.BOLD, 15));
       
            this.password.setBounds(80,130,50,70);      
            this.password.setFont(new Font("Calibri", Font.BOLD, 15));

            this.button1.setBounds(350,130,120,55);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,3));

            this.button2.setBounds(350,215,120,55);
            this.button2.setBackground(Color.white);
            this.button2.setBorder(new LineBorder(Color.black,3));
            this.button2.setForeground(Color.black);
            
            
            this.button3.setBounds(350,300,120,55);
            this.button3.setBackground(Color.white);
            this.button3.setBorder(new LineBorder(Color.black,3));

            this.button4.setBounds(350,385,120,55);
            this.button4.setBackground(Color.white);
            this.button4.setBorder(new LineBorder(Color.black,3));
            
            
            this.button5.setBounds(350,470,120,55);
            this.button5.setBackground(Color.white);
            this.button5.setBorder(new LineBorder(Color.black,3));

            this.button6.setBounds(350,555,120,55);
            this.button6.setBackground(new Color(156,156,156));
            this.button6.setBorder(new LineBorder(Color.black,3));
            this.button6.setForeground(Color.white);
            
            
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);

          
            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.button3);
            this.window.add(this.button4);
            this.window.add(this.button5);
            this.window.add(this.button6);
            
            this.window.add(this.title); 
            this.window.add(this.textF1);
            this.window.add(this.passF);
            this.window.add(this.username);
            this.window.add(this.password);
            visOfUIElements(false);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(820,800);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            break;
            case 4:
            this.window.setTitle("Študentský systém");
            this.button1.setText("Predmety");
            this.button2.setText("Voliteľné predmety");
            this.button3.setText("Zmeniť heslo");
            this.button4.setText("Profilové údaje");
            this.button5.setText("Odhlásiť");
            String a = "nID: 560269 ";
            String b ="nMeno: Marek Magula ";
            String c = "nŠtudijná skupina: 5ZYS11";
            
            this.title2.setEditable(false);
            this.title2.append(a);
            this.title2.append(b);
            this.title2.append(c);
            this.textF1.setBounds(325,125,125,20);
            this.passF.setBounds(325,155,125,20);
            this.username.setText("Login:");
            this.password.setText("Heslo:");
            
            this.title2.setBounds(350,50,200,200);
            this.title2.setFont(new Font("Calibri", Font.BOLD, 15));
            this.title2.setBackground(new Color(235, 235, 235));
            
            this.username.setBounds(80,100,50,70);
            this.username.setFont(new Font("Calibri", Font.BOLD, 15));
       
            this.password.setBounds(80,130,50,70);      
            this.password.setFont(new Font("Calibri", Font.BOLD, 15));

            this.button1.setBounds(350,130,120,55);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,3));

            this.button2.setBounds(350,215,120,55);
            this.button2.setBackground(Color.white);
            this.button2.setBorder(new LineBorder(Color.black,3));
            this.button2.setForeground(Color.black);
            
            
            this.button3.setBounds(350,300,120,55);
            this.button3.setBackground(Color.white);
            this.button3.setBorder(new LineBorder(Color.black,3));

            this.button4.setBounds(350,385,120,55);
            this.button4.setBackground(Color.white);
            this.button4.setBorder(new LineBorder(Color.black,3));

            this.button5.setBounds(350,470,120,55);
            this.button5.setBackground(new Color(156,156,156));
            this.button5.setBorder(new LineBorder(Color.black,3));
            this.button5.setForeground(Color.white);
  
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);

            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.button3);
            this.window.add(this.button4);
            
            this.window.add(this.button5);
            
            this.window.add(this.title2); 
            this.window.remove(this.title); 
            this.window.add(this.textF1);
            this.window.add(this.passF);
            this.window.add(this.username);
            this.window.add(this.password);
            visOfUIElements(false);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(820,800);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            
            break;
        
        }

    }
    public void actionPerformed(ActionEvent e) {
        switch (this.menu) {
            case 0:
            if (e.getSource() == this.button1) {
                this.menu = 1;            
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 2;           
                start();
            }
            break;
            case 1:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 2:
            if (e.getSource()== this.button1) {
                this.menu = 4;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 3:
            if (e.getSource() == this.button1) {
                this.menu = 5;            
                start();
            } else if (e.getSource() == this.button2) {
                this.menu = 6;           
                start();
            } else if (e.getSource() == this.button3) {
                this.menu = 7;           
                start();
            } else if (e.getSource() == this.button4) {
                this.menu = 8;           
                start();
            } else if (e.getSource() == this.button5) {
                this.menu = 9;           
                start();
            } else if (e.getSource() == this.button6) {
                this.menu = 0;           
                start();
            }
            break;
            case 4:
            if (e.getSource() == this.button1) {
                this.menu = 10;            
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 11;           
                start();
            } else if (e.getSource()== this.button3) {
                this.menu = 12;           
                start();
            } else if (e.getSource()== this.button4) {
                this.menu = 13;           
                start();
            } else if (e.getSource()== this.button5) {
                this.menu = 0;           
                start();
            } 
            break;
            case 5:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 6:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 7:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 8:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 9:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 10:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 11:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 12:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 13:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
           
        }    
    }
    
    /*public void visOfUIElements(boolean visibility) {
        this.username.setVisible(visibility);
            this.password.setVisible(visibility);
            this.textF1.setVisible(visibility);
            this.passF.setVisible(visibility);
    }*/
}

Here is my code. I’m changing the layout of a UI window and need to make it more efficient so I don’t write the same code hundreds of times.

Advertisement

Answer

Add the components you want to control to a common container, then simply hide/show it

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBorder(new EmptyBorder(new Insets(8, 8, 8, 8)));
            setLayout(new BorderLayout());
            JPanel basePane = new JPanel(new GridBagLayout());
            basePane.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));
            basePane.add(new JLabel("I can see you"));
            add(basePane);

            JButton btn = new JButton("Hide");
            add(btn, BorderLayout.SOUTH);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (basePane.isVisible()) {
                        basePane.setVisible(false);
                        btn.setText("Show");
                    } else {
                        basePane.setVisible(true);
                        btn.setText("Hide");
                    }
                }
            }); 
        }

    }
}

I think this will not do the job …

Actually, I think it will, but, you’re missing a number very important, core concepts.

Decouple

You’re operating in a OO language, one of things you’re going to want to focus on is “decoupling” the dependencies your functionality has to other functionality. Ask yourself the simple question. If I had to modify this one part of the system, what ramifications will it have. If you find yourself in a cascade of changes, then your system is to tightly coupled

Single responsibility

You want to isolate functionality, as much as possible, so that it has one single job to do. Rather then using a “god” class (like you have), which tries to do everything, break down your problem into small chunks an isolate the functionality, so that it’s doing as simple a job as it possibly can.

As much as possible, each element shouldn’t be reliant on knowing the current state of the system, beyond what it’s been told, which is part of the first and third points.

Dependency injection

Pass the information that each sub system needs to them, rather than having globally reachable resources. This is really good for testing, but it further reduces the coupling

Code to interface

Make use of interface to describe high level concepts of functionality. For example, the “student details” view doesn’t care about how the student information is structured, obtained or managed, it only cares about presenting some information maintained by the interface (or contract). This will make it easier to make core changes to the underlying implementation without adversely effecting all those parts of the system which want to use it, as they are only relying on the “contract” defined by the interface

Example…

Now, typically, I use a CardLayout, but a CardLayout isn’t as dynamic as something like this problem might need.

So, instead, I rolled my own “navigation” workflow. This allows me more control over the information which needs to be injected and allows for a more dynamic presentation – you don’t want to present the student login screen filled in with the student credentials when a student logs out.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayDeque;
import java.util.Deque;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setTitle("Študentský systém");
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public interface Authenticator {

        public Student authenticateStudent(String userName, char[] password);

        public void logout(Student student);
    }

    // This is how you would return information
    public interface Student {
    }

    // You'll need to fill this out...
    public class DefaultAuthenticator implements Authenticator {

        @Override
        public Student authenticateStudent(String userName, char[] password) {
            return null;
        }

        @Override
        public void logout(Student student) {
        }

    }

    public class NavigationManager {

        private Deque<JPanel> viewHierarchy = new ArrayDeque<>(16);

        private JPanel parent;

        public NavigationManager(JPanel parent) {
            this.parent = parent;
        }

        protected void push(JPanel view) {
            viewHierarchy.add(view);
            present(view);
        }

        protected void present(JPanel view) {
            parent.removeAll();
            parent.add(view);
            parent.revalidate();
            Window window = SwingUtilities.windowForComponent(parent);
            if (window != null) {
                window.pack();
                window.setLocationRelativeTo(null);
            }
            parent.repaint();
        }

        protected void pop() {
            if (viewHierarchy.size() > 1) {
                viewHierarchy.removeLast();
            }
            present(viewHierarchy.getLast());
        }
    }

    public class MainPane extends JPanel {

        private MenuPane menuPane;

        private Authenticator authenticator = new DefaultAuthenticator();
        private NavigationManager navigationManager;

        public MainPane() {
            setLayout(new BorderLayout());
            navigationManager = new NavigationManager(this);

            push(getMenuPane());
        }

        protected void push(JPanel view) {
            navigationManager.push(view);
        }

        protected void pop() {
            navigationManager.pop();
        }

        protected void present(JPanel view) {
            navigationManager.present(view);
        }

        protected Authenticator getAuthenticator() {
            return authenticator;
        }

        protected MenuPane getMenuPane() {
            if (menuPane == null) {
                menuPane = new MenuPane(new MenuPane.NavigationController() {
                    @Override
                    public void presentAdmin() {
                    }

                    @Override
                    public void presentStudent() {
                        present(getStudentLoginPane());
                    }
                });
            }
            return menuPane;
        }

        protected StudentLoginPane getStudentLoginPane() {
            return new StudentLoginPane(getAuthenticator(), new StudentLoginPane.NavigationController() {
                @Override
                public void back() {
                    pop();
                }

                @Override
                public void studentAuthentictated(Student student) {
                    push(getStudentDetailsPane(student));
                }
            });
        }

        protected StudentPane getStudentDetailsPane(Student student) {
            return new StudentPane(student, new StudentPane.NavigationController() {
                @Override
                public void logout(Student student) {
                    getAuthenticator().logout(student);
                    pop();
                }
            });
        }

    }

    public class MenuPane extends JPanel {

        public static interface NavigationController {

            public void presentAdmin();

            public void presentStudent();
        }

        private JButton adminBtn;
        private JButton studentBtn;
        private JLabel title;

        public MenuPane(NavigationController navigationController) {
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(new Insets(25, 25, 25, 25)));
            adminBtn = new JButton("Admin");
            studentBtn = new JButton("Študent");
            title = new JLabel("Zvoľ typ prihlásenia");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(4, 4, 4, 4);

            add(title, gbc);
            add(adminBtn, gbc);
            add(studentBtn, gbc);

            adminBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigationController.presentAdmin();
                }
            });
            studentBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigationController.presentStudent();
                }
            });
        }
    }

    public class StudentLoginPane extends JPanel {

        public static interface NavigationController {

            public void back();

            public void studentAuthentictated(Student student);
        }

        private JLabel title;
        private JButton signInBtn;
        private JButton backBtn;

        private JLabel loginLabel;
        private JLabel passwordLabel;
        private JTextField userNameField;
        private JTextField passwordField;

        public StudentLoginPane(Authenticator authenticator, NavigationController navigationController) {
            title = new JLabel("Študentský systém");
            title.setHorizontalAlignment(JLabel.CENTER);
            title.setBorder(new EmptyBorder(16, 16, 16, 16));
            signInBtn = new JButton("Prihlásiť");
            backBtn = new JButton("Späť");
            loginLabel = new JLabel("Login:");
            passwordLabel = new JLabel("Heslo:");

            userNameField = new JTextField(10);
            passwordField = new JPasswordField(10);

            setLayout(new BorderLayout());
            add(title, BorderLayout.NORTH);

            JPanel contentPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.LINE_END;
            gbc.insets = new Insets(4, 4, 4, 4);

            contentPane.add(loginLabel, gbc);
            gbc.gridy++;
            contentPane.add(passwordLabel, gbc);

            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.gridx = 1;
            gbc.gridy = 0;
            contentPane.add(userNameField, gbc);
            gbc.gridy++;
            contentPane.add(passwordField, gbc);

            add(contentPane);

            JPanel buttonPane = new JPanel(new GridBagLayout());
            buttonPane.setBorder(new EmptyBorder(16, 16, 16, 16));

            buttonPane.add(signInBtn);
            buttonPane.add(backBtn);

            add(buttonPane, BorderLayout.SOUTH);

            signInBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Perform the required authentication
                    // If you're wondering, yes, I'd probably pass in a reference
                    // to a "authenticator" which would do the actual work for
                    // me
                    // Passing null here is just for demonstration purposes
                    navigationController.studentAuthentictated(null);
                }
            });

            backBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigationController.back();
                }
            });
        }
    }

    public class StudentPane extends JPanel {

        public static interface NavigationController {

            public void logout(Student student);
        }

        private JLabel idLabel;
        private JLabel nameLabel;
        private JLabel groupLabel;

        // And here we have a demonstration of a decoupled workflow
        // The MainPane shouldn't care about anything other then logging
        // the student out, all other "sub flows" can become the responsbility
        // of the sub views themselves
        private NavigationManager navigationManager;

        public StudentPane(Student student, NavigationController navigationController) {
            navigationManager = new NavigationManager(this);

            idLabel = new JLabel("560269"); // Use the reference to student to get this
            nameLabel = new JLabel("Marek Magula"); // Use the reference to student to get this
            groupLabel = new JLabel("5ZYS11"); // Use the reference to student to get this

            setLayout(new BorderLayout());

            JPanel detailsPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.LINE_END;
            gbc.insets = new Insets(4, 4, 4, 4);

            detailsPane.add(new JLabel("ID:"), gbc);
            gbc.gridy++;
            detailsPane.add(new JLabel("Meno:"), gbc);
            gbc.gridy++;
            detailsPane.add(new JLabel("Študijná skupina:"), gbc);

            add(detailsPane, BorderLayout.NORTH);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            detailsPane.add(idLabel, gbc);
            gbc.gridy++;
            detailsPane.add(nameLabel, gbc);
            gbc.gridy++;
            detailsPane.add(groupLabel, gbc);

            JButton subjectsBtn = new JButton("Predmety");
            JButton optioinalSubjectsBtn = new JButton("Voliteľné predmety");
            JButton changePassword = new JButton("Zmeniť heslo");
            JButton profileBtn = new JButton("Profilové údaje");
            JButton logoutBtn = new JButton("Odhlásiť");

            JPanel buttonPane = new JPanel(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.gridwidth = gbc.REMAINDER;
            gbc.fill = gbc.HORIZONTAL;
            gbc.ipadx = 8;
            gbc.ipady = 8;
            gbc.insets = new Insets(4, 4, 4, 4);

            buttonPane.add(subjectsBtn, gbc);
            buttonPane.add(optioinalSubjectsBtn, gbc);
            buttonPane.add(changePassword, gbc);
            buttonPane.add(profileBtn, gbc);
            buttonPane.add(logoutBtn, gbc);

            add(buttonPane);

            subjectsBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Create a student's subject pane;
                    // Inject in the student reference
                    // Use the NavigationManager to push it onto the stack
                }
            });
            optioinalSubjectsBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Create a student's optional subject pane;
                    // Inject in the student reference
                    // Use the NavigationManager to push it onto the stack
                }
            });
            changePassword.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Okay, this is one of those "questionable" options
                    // Is it better to pass the a reference of the 
                    // "authenticator" service to this class, then use a 
                    // simple dialog to get the password from the user
                    // and then pass it to the "authenticator" service
                    // OR
                    // should we present another screen?!
                    // Personally, I prefer option 1

                    JOptionPane.showMessageDialog(StudentPane.this, "All your password belong to us");
                }
            });
            profileBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Create a student's profile pane;
                    // Inject in the student reference
                    // Use the NavigationManager to push it onto the stack
                }
            });
            logoutBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigationController.logout(student);
                }
            });
        }
    }
}

This is an incomplete implementation, only intended to provide a jumping off point.

I strongly encourage you to have look at Laying Out Components Within a Container

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement