-1

This is my LoginGUI class, I want to move from this GUI class to another "StudentGUI" class. It seems so simple but I can't figure out

    JButton btnNewButton_1 = new JButton("Log In");
    btnNewButton_1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {

            if(Username.equals(textField1.getText())){

                    if(Password.equals(textField2.getText())){
                        // close(LoginGUI);
                        // run(StudentGUI); 

                    **Missing function**

                        msg = "Loged in!";
                    }else{
                        msg = "Login Denied";
                    }
                }else{
                    msg = "Login Denied";
                }   
                JOptionPane.showMessageDialog(null,msg);                    
            }




        });
    btnNewButton_1.setBounds(157, 230, 89, 23);
    frame.getContentPane().add(btnNewButton_1);
}

}

  • 1
    you need to have an instance of the other ui. without any more code of the other class it is not possible to help much more. Besides this - try to grab a java basic book and learn about object orientated programming before starting with UI programming – Emerson Cod Nov 09 '15 at 20:00
  • I refer you to the use of the [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Using multiple JFrames is bad practice. – Jonah Nov 09 '15 at 20:03
  • 1
    In your specific case, you don't. Allow the UI to simple act as a mechanism for gathering information from the user, allow another class (the controller) to determine which should happen when the user attempts to valid their credentials and a model to perform the actual validation. When validated, a separate controller would then make determinations about navigation. See [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) for more details – MadProgrammer Nov 09 '15 at 20:05
  • 1
    For [example](http://stackoverflow.com/questions/33487157/open-jframe-only-after-successfull-login-verification-with-database-using-ecli/33488721#33488721) – MadProgrammer Nov 09 '15 at 20:09

1 Answers1

1

If you would like to switch between multiple views, use the CardLayout Here's a simple example

package main.frames;

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame
{
static JPanel homeContainer;
static JPanel homePanel;
static JPanel otherPanel;
static CardLayout cl;

public MainFrame()
{
    JButton showOtherPanelBtn = new JButton("Show Other Panel");
    JButton backToHomeBtn = new JButton("Show Home Panel");

    cl = new CardLayout(5, 5);
    homeContainer = new JPanel(cl);
    homeContainer.setBackground(Color.black);

    homePanel = new JPanel();
    homePanel.setBackground(Color.blue);
    homePanel.add(showOtherPanelBtn);

    homeContainer.add(homePanel, "Home");

    otherPanel = new JPanel();
    otherPanel.setBackground(Color.green);
    otherPanel.add(backToHomeBtn);

    homeContainer.add(otherPanel, "Other Panel");

    showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel"));
    backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home"));

    add(homeContainer);
    cl.show(homeContainer, "Home");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setTitle("CardLayout Example");
    pack();
    setVisible(true);
}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(MainFrame::new);
}
}
Jonah
  • 1,013
  • 15
  • 25