0

I'm creating a GUI which has the following panels:

  1. The Main frame which holds all the panels in the program:

    public class Main extends JFrame {
        private Signup signupForm;
        private Login login;
        private UserScreen user;
        private JPanel cards;
        private CardLayout cl;  
    
        private static final int INNER_FRAME_WIDTH=800;
        private static final int INNER_FRAME_HEIGHT=800;
    
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public Main() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, INNER_FRAME_HEIGHT, INNER_FRAME_WIDTH);
        getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
    
        cards = new JPanel();
        getContentPane().add(cards);
    
        cl = new CardLayout();
        cards.setLayout(cl);
    
        login = new Login();
        cards.add(login,"login");
    
    
        signupForm = new Signup();
        cards.add(signupForm,"signup");
    
        ActionListener listen = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try{
                //check for a user with the parameters from userName and password fields . On success create a UserScreen for the current user.
                //This means that you need to send the User object to the UserScreen c'tor. It should look something like this. Might need to change UserScreen accordingly.
                user = new UserScreen();
                cards.add(user,"user");
                cl.show(cards,"user");
            }
            catch (Exception exception){ //TODO: Change to exception thrown from Client constructor
                //TODO: Have the exception popup the relevant screen.
            }
        }       
    
    };
    
        login.loginBtn.addActionListener(listen); << Example of setting a button's actionListener from Main.
    
        setLoginListeners();
        setSignupListeners();
    }
    

    }

  2. A Login panel - This is the screen you encounter when you open the program.

  3. A Signup panel - You get to this screen when pressing a 'signup' button in the login panel.

  4. A User's screen - A screen that display's a user's info.

I've encountered an issue which I'm not sure how to handle:

After clicking the 'Login' button in the Login panel, I want to switch to the new User's screen (UserScreen object). Since the class Login doesn't have access to the UserScreen object (only the Main frame does), I had to set the button's actionListener from the Main class which seemed like a terrible solution (since it required me to give the Main access to many fields from the Login panel so it can check if the user with the given username and password exists etc). Is there no better way?

Is there a better solution than the one I've got?

matanc1
  • 6,525
  • 6
  • 37
  • 57
  • 1
    You can implement a `Mediator`. – nachokk Jan 16 '14 at 19:07
  • 1
    *"JPanels and handling events between them"* Don't extend `JPanel` but simply use an instance of one. Once you figure that out, you will realize this is a non-issue. – Andrew Thompson Jan 16 '14 at 19:53
  • I was trying to find something like this have a look in this contenthttp://stackoverflow.com/questions/10781401/switching-between-screens-in-java-swing.see if this helps you any way – Manish Singh Jan 16 '14 at 23:00

1 Answers1

1

You should implement the model-view-controller pattern, the interaction between different views should always be through a controller.

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Typo
  • 1,875
  • 1
  • 19
  • 32
  • Can you elaborate on how? – matanc1 Jan 16 '14 at 19:23
  • 1
    @Shookie take a look at [**this**](http://www.oracle.com/technetwork/articles/javase/index-142890.html) – Paul Samsotha Jan 16 '14 at 19:27
  • The Login screen (view) tells de controller the login info, the controller authenticates, close the login (view), and habilitates the Main Frame (View) with the Users info (Model). – Typo Jan 16 '14 at 19:27