1

I have a JFrame which uses JPanel initialized from the JPanel.

This is the JFrame class : LoginPage

public class LoginPage extends JFrame
{
private JPanel contentPane;
static int cnf;
static String data;
private static LoginPage frame;

/**
 * Launch the application.
 */
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                frame = new LoginPage();
                frame.setVisible(true); 
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });

    //cnf = chk;
    if( cnf == 1)
    {
        frame.dispose();
        JFrame m = new MainPage();
        m.setVisible(true);
    }
}

/**
 * Create the frame.
 */
public LoginPage()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel m = new MainLogin();
    m.setBounds(0, 0, 448, 271);
    contentPane.add(m);
}
}

And, this is the JPanel class : MainLogin

public class MainLogin extends JPanel
{
private JTextField uname;
private JPasswordField pass;
public static int chk;

public MainLogin()
{
    setLayout(null);

    uname = new JTextField();
    uname.setBounds(236, 22, 167, 25);
    add(uname);
    uname.setColumns(10);

    pass = new JPasswordField();
    pass.setBounds(236, 53, 167, 25);
    add(pass);

    JButton login = new JButton("Login");
    login.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            String u = uname.getText();
            char[] tp = pass.getPassword();
            String p = new String(tp);

            chk = authentication.verify(u, p);
            System.out.println(chk);
        }
    });
    login.setBounds(235, 90, 117, 25);
    add(login);
}
}

As in the MainLogin Panel, there is a class authentication who has a method verify(), which returns an integer, and this integer is stored in chk. Since, this chk resides in MainLogin JPanel class, I want to pass it to the LoginPage JFrame class. Is there any way to do this other than using a File?

Ritwik Jamuar
  • 351
  • 1
  • 4
  • 17
  • As for your implementation, you could get it just by refering to `MainLogin.chk`in you `LoginPage` class. – lkq May 06 '16 at 17:44
  • 1) `setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) What is the reason for extending frame or panel in this case? Prefer composition over inheritance. – Andrew Thompson May 06 '16 at 23:32

2 Answers2

1

Open the main page from LoginPage instance not from the main method.

Add a login() method to the LoginPage

public class LoginPage extends JFrame {

    //other parts

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    frame = new LoginPage();
                    frame.setVisible(true); 
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public LoginPage() {
        //...
        JPanel m = new MainLogin(this);
        //...
    }

    public void login(int chk) {
        JFrame m = new MainPage();
        m.setVisible(true);
        this.dispose();
    }
}

And pass login frame to the panel as a parameter

public class MainLogin extends JPanel
{

private int chk;//no need to be static

public MainLogin(final LoginFrame loginFrame)
{
    setLayout(null);//null layout is bad

    //...
    login.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //...
            chk = authentication.verify(u, p);
            loginFrame.login(chk);
        }
    });
    //...
}
}
rdonuk
  • 3,921
  • 21
  • 39
1

Not only does this question get asked quite a bit, it also has a number of possible solutions, including using a modal dialog or Observer Pattern depending on your needs

See How to Make Dialogs for more details

You might also like to take a look at

for more discussion on the subject

The basic answer here is, you want to separate the areas of responsibility.

You need to:

  • Gather the user credentials
  • Validate those credentials
  • Take a appropriate action based on the success of that check

These are three distinct actions, all which should be separated, it's not the responsibility of the login panel to validate the credentials, that's someone else's responsibility, equally, it's not the validators responsibility to decide what should be done when the validation fails or succeeds, that's someone else's responsibility

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • to be precise, I want to close this `LoginPage` JFrame and instantiate a new `MainPage` JFrame once the User is Validated, ie. chk value changes from 0 to 1. – Ritwik Jamuar May 08 '16 at 19:18
  • Yes, precisely, the login panel gathers the user details, then on so event, notified the controller which either directly or indirectly validates the credentials, then based on the result of that, notifies some navigation controller which decides where it should go next – MadProgrammer May 08 '16 at 19:57