0

I'm using the netbeans GUI Builder for this! I've created an JFrame and put a JPanel inside with a CardLayout to switch between JPanels. The JFrame also has a Menu attached to it with diffrent options. Now i want the options to be grayed out until the user logs in. According to the premissions of the logged in user, options become available (see picture for reference). The login panel

The problem is that i have no idea on how to get what user is logged in when switching panels. The login panel knows what got typed in the fields and know if the login is correct. I've tryed using the .getParent() function to change a variable inside the Jframe but that does not seem to work. But how would i go about changing the JMenu items from the JPanel? (See this picture for child parent relations)

Relations

DjKillerMemeStar
  • 425
  • 5
  • 18
  • Check about model/view/controller - that username belongs into the model not into the view! – Jan Jun 06 '17 at 17:38
  • I'm sorry but i don't really get what you mean. In the JFrame i have a public variable with the name CurrentUser. How could i set this? (i have getters and setters) – DjKillerMemeStar Jun 06 '17 at 17:50
  • Create a Java class, for example: `LoginInformation` (Model) which contains `String email` and `String password`, then, save that information and use it to update your GUI (View) – Frakcool Jun 06 '17 at 18:50

1 Answers1

0

Hi I've developed this quick example. (Please focus on the way of mantain and share the login info).

As @Jan suggested, this is a coarse implementation of the MVC pattern. In which you try to separate: Model: The data you are using, and the component layer for your services and data transformation. View: The component which receives the data from the user Controller: The way of connect the view with your model Here you are a link to get more information: The MVC pattern and SWING

Class used to get the Login:

package com.stackoverflow.login;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class LoginDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    // This is the class who will mantain the logged used information
    private User user;
    private JTextField tfUsername;
    private JPasswordField pfPassword;
    private JLabel lbUsername;
    private JLabel lbPassword;
    private JButton btnLogin;
    private JButton btnCancel;
    private boolean succeeded;

    public LoginDialog(Frame parent) {
        super(parent, "Login", true);
        //
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints cs = new GridBagConstraints();

        cs.fill = GridBagConstraints.HORIZONTAL;

        lbUsername = new JLabel("Username: ");
        cs.gridx = 0;
        cs.gridy = 0;
        cs.gridwidth = 1;
        panel.add(lbUsername, cs);

        tfUsername = new JTextField(20);
        cs.gridx = 1;
        cs.gridy = 0;
        cs.gridwidth = 2;
        panel.add(tfUsername, cs);

        lbPassword = new JLabel("Password: ");
        cs.gridx = 0;
        cs.gridy = 1;
        cs.gridwidth = 1;
        panel.add(lbPassword, cs);

        pfPassword = new JPasswordField(20);
        cs.gridx = 1;
        cs.gridy = 1;
        cs.gridwidth = 2;
        panel.add(pfPassword, cs);
        panel.setBorder(new LineBorder(Color.GRAY));

        btnLogin = new JButton("Login");

        btnLogin.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (Login.authenticate(getUsername(), getPassword())) {
                    JOptionPane.showMessageDialog(LoginDialog.this,
                            "Hi " + getUsername() + "! You have successfully logged in.", "Login",
                            JOptionPane.INFORMATION_MESSAGE);
                    succeeded = true;
                    user = new User();
                    user.setPassword(getPassword());
                    user.setUsername(getUsername());
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(LoginDialog.this, "Invalid username or password", "Login",
                            JOptionPane.ERROR_MESSAGE);
                    // reset username and password
                    tfUsername.setText("");
                    pfPassword.setText("");
                    succeeded = false;

                }
            }
        });
        btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        JPanel bp = new JPanel();
        bp.add(btnLogin);
        bp.add(btnCancel);

        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(bp, BorderLayout.PAGE_END);

        pack();
        setResizable(false);
        setLocationRelativeTo(parent);
    }

    public String getUsername() {
        return tfUsername.getText().trim();
    }

    public String getPassword() {
        return new String(pfPassword.getPassword());
    }

    public boolean isSucceeded() {
        return succeeded;
    }

    public User getUser() {
        return user;
    }

}

Login service

package com.stackoverflow.login;
public class Login {

    public static boolean authenticate(String username, String password) {
        // hardcoded username and password
        if (username.equals("bob") && password.equals("secret")) {
            return true;
        }
        return false;
    }
}

Class for hold user information: package com.stackoverflow.login;

/**
 * Class used to mantain the login information, it's just for educational
 * purpouses, for prod environment you must implement it in a secure way!!
 */
public class User {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

Class for run the program

package com.stackoverflow.login;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * Run the program
 */
public class Main {
    public static void main(String[] args) {
        final JFrame frame = new JFrame("JDialog Demo");
        final JButton btnLogin = new JButton("Click to login");
        final JButton btnPanel = new JButton();

        LoginDialog loginDlg = new LoginDialog(frame);
        loginDlg.setVisible(true);
        // if logon successfully
        if (loginDlg.isSucceeded()) {
            btnLogin.setVisible(false);
            btnPanel.setText("Hi " + loginDlg.getUsername() + "! click to open new frame");
            frame.getContentPane().add(btnPanel);
        }

        btnPanel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JLabel label = new JLabel("Logged user is: " + loginDlg.getUser().getUsername());
                JDialog mydialog = new JDialog();
                mydialog.setSize(new Dimension(400, 100));
                mydialog.setTitle("Another frame");
                mydialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
                mydialog.add(label);
                mydialog.setVisible(true);
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 100);
        frame.setLayout(new FlowLayout());
        frame.getContentPane().add(btnLogin);
        frame.setVisible(true);
    }
}
sirandy
  • 1,834
  • 5
  • 27
  • 32