1

To start I am just playing with buttons and JFrame. The issue is that I can switch from one Jframe to the next with a few lines of code here...

    JButton studentLoginButton = new JButton("Student");
    studentLoginButton.setBounds(85, 80, 80, 20);
    studentLoginButton.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {

           LoginFrame.super.setVisible(false);
           student.setVisible(true);
       }
   });
    add(studentLoginButton);

BUT when I set the student JFrame as visible and use the code here...

    JButton cancelButton = new JButton("Go Back");
    cancelButton.setBounds(205, 80, 80, 20);
    cancelButton.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {
           StudentFrame.super.setVisible(false);
           login1.setVisible(true);
       }
   });
    add(cancelButton);

to cancel and go back it does not work. Nothing shows and the application is not terminated. What could I do to solve this issue? I can provide more code if it helps others find a solution.

It looks like more code would help to here are both classes. LoginFrame.java...

package system;

import java.awt.FlowLayout;
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.JPasswordField;
import javax.swing.JTextField;

public class LoginFrame extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;

StudentFrame student;

public static void main(String[] args) {
    new LoginFrame().setVisible(true);
}


LoginFrame(){
    super(" User Login ");
    setSize(400, 80); 
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    StudentFrame student = new StudentFrame(null);

    setLayout(new FlowLayout());

    //student
    JButton studentLoginButton = new JButton("Student");
    studentLoginButton.setBounds(85, 80, 80, 20);
    studentLoginButton.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {

           LoginFrame.super.setVisible(false);
           student.setVisible(true);
       }
   });
    add(studentLoginButton);

    //Dept. staff
    JButton deptStaffLoginButton = new JButton("Department Staff");
    deptStaffLoginButton.setBounds(85, 80, 80, 20);
    deptStaffLoginButton.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {

           LoginFrame.super.setVisible(false);
           student.setVisible(true);
       }
   });
    add(deptStaffLoginButton);

    //Instructor
    JButton InstructorLoginButton = new JButton("Instructor");
    InstructorLoginButton.setBounds(85, 80, 80, 20);
    InstructorLoginButton.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {

           LoginFrame.super.setVisible(false);
           student.setVisible(true);
       }
   });
    add(InstructorLoginButton);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

}

and then the next is... StudentFrame.java package system;

import java.awt.FlowLayout;
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.JPasswordField;
import javax.swing.JTextField;

public class StudentFrame extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;

static LoginFrame login1;

public static void main(String[] args) {
    new StudentFrame(login1).setVisible(true);
}

StudentFrame(LoginFrame login){
    super(" User Login ");
    setSize(600, 600); 
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(null);

    login1 = login;

//      LoginFrame login = new LoginFrame();

    JButton loginButton = new JButton("Login");
    loginButton.setBounds(85, 80, 80, 20);
    loginButton.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {

 //            LoginFrame.super.setVisible(false);
 //            student.setVisible(true);
       }
   });
    add(loginButton);

    JButton cancelButton = new JButton("Go Back");
    cancelButton.setBounds(205, 80, 80, 20);
    cancelButton.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {
           StudentFrame.super.setVisible(false);
           login1.setVisible(true);
       }
   });
    add(cancelButton);

    JLabel passLabel = new JLabel("Password: ");
    passLabel.setBounds(10, 50, 80, 25);
    add(passLabel);

    JPasswordField passwordText = new JPasswordField(20);
    passwordText.setBounds(85, 50, 240, 25);
    add(passwordText);

    JLabel userLabel = new JLabel("User name: ");
    userLabel.setBounds(10, 10, 80, 25);
    add(userLabel);

    JTextField userNameText = new JTextField();
    userNameText.setBounds(85, 10, 240, 25);
    add(userNameText);

}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}
}
Layman
  • 89
  • 3
  • 9
  • 1
    Please provide an [MCVE](http://stackoverflow.com/help/mcve) that demonstrates exactly what you're doing. As of now we can only guess what student and login1 are. Also, you should probably use a CardLayout instead of multiple JFrames. – Kevin Workman Apr 01 '15 at 02:37

1 Answers1

2

It's hard to know why your current code is not working since we don't have a running example, but I do worry when I see static calls, and calls to super like this, StudentFrame.super.setVisible(false);.

But regardless of the directly solution to your problem, and while I know that this isn't answering your question, the best solution is this: don't. Don't throw different JFrames at the user as that can be quite an annoying thing for the user to experience, especially when there are better ways of swapping views, the best here being to use a CardLayout to swap JPanel views, and this is exactly what I suggest. You can find the tutorial here: CardLayout Tutorial.

Also please look at this link: The Use of Multiple JFrames, Good/Bad Practice?

Another option, if you want to use a login dialog is to do just that, use a modal JDialog to show the Login window, and make it modal off of the main application.

For details on either of these solutions, show more pertinent code.

As a "side" recommendation, I have to mention your call to setBounds. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.


On further review of your code, I believe your best option is to use a modal JDialog to display the login window. This behaves similar to a JFrame, however it uses a different constructor (the API can help you with this). The main difference in its behavior is that it halts the calling code immediately where the calling code sets the JDialog visible -- just like a JOptionPane does (and note that this is a specialized JDialog itself). This way the calling code will know whenever the JDialog has been dealt with and is no longer visible as its code flow resumes right after where it had set the JDialog visible. This way the code in the main GUI can display the dialog, and then acquire information from it when the user is done with it.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373