-1

I am sure at the end of reading answers I will feel like a complete idiot, but I need help anyways. I am trying to create my first Java app with switching between forms. I am using IntelliJ IDE and using provided form designer I created some forms. My wish is to be able to switch between forms when clicking proper buttons. I tried to find answers to my questions but my problems come from my lack of experience and some basic knowledge about how to properly implement forms. I have found this, for example:

btnLogin.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) {
        this.setVisible(false);
        new FrmMain().setVisible(true); // Main Form to show after the Login Form..
    }
});

And I am sure using this would help me achieve what I want to achieve, BUT this is not working. Maily because I have no idea what form's class file actually should contain. While making a single-form app I don't need to extend JFrame in form class, but I suppose (?) I need to when I am trying to switch between forms with this method? I don't know what should appear in contructors, I don't know if I need main functions (don't really think so) in classes other than the starting form's class. I will paste what I have now. Feel free to educate me on what should it look like. Right now I am unable to close the menu form and while opening the other form there are no contents (buttons etc, don't know why).

First form:

package ImplementedUCs;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuForm extends JFrame
{
    private JPanel MenuPanel;
    private JButton loginButton;
    private JButton profileButton;
    private JButton conferencesButton;
    private JButton usersButton;
    private JButton settingsButton;
    private JButton exitButton;
    private JLabel titleLabel;
    private JLabel logLabel;
    private JLabel userLabel;

    public static boolean loginStatus = false;
    public static UserTypes loginType = UserTypes.NOT_LOGGED_IN;
    public static MenuForm myMenuForm;

    public MenuForm()
    {
        myMenuForm = this;

        logLabel.setText("You are currently not logged in!");
        userLabel.setText("Please log in to use this application!");

        loginButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                // want to close/hide menu form here
                new LoggingForm().setVisible(true);
            }
        });
    }

    public static void main(String[] args)
    {
        JFrame menuFrame = new JFrame("MenuForm");
        menuFrame.setContentPane(new MenuForm().MenuPanel);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menuFrame.pack();
        menuFrame.setTitle("ConferenceStation v1.0");
        menuFrame.setSize(800,600);
        menuFrame.setLocationRelativeTo(null);
        menuFrame.setResizable(false);
        menuFrame.setVisible(true);
    }
}

Second form:

package ImplementedUCs;

import javax.swing.*;

public class LoggingForm extends JFrame
{
    private JPanel loggingPanel;
    private JTextField loginField;
    private JPasswordField passwordField;
    private JButton remindButton;
    private JButton noAccButton;
    private JButton loginButton;
    private JLabel titleLabel;
    private JPanel properPanel;
    private JLabel loginLabel;
    private JLabel passwordLabel;
    private JLabel noAccLabel;
    private JLabel forgotLabel;

    public LoggingForm()
    {
        this.setTitle("ConferenceStation v1.0");
        this.setSize(800,600);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setVisible(true);
    }
}

I am really trying to understand how it should look like and how to make it work, please, I need help.

David Yee
  • 3,515
  • 25
  • 45
Sigmar
  • 11
  • 1
  • 1
  • 1
    Please rephrase your question, as is, it's unclear what you're asking. Are you trying to open another `JFrame` from the 1st one? – Frakcool Feb 02 '16 at 16:47
  • 1
    Possible duplicate of [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice). – Hovercraft Full Of Eels Feb 02 '16 at 16:50
  • 5
    The best answer: **don't do it**. No user likes to have windows thrown at them, and so instead use one JFrame and swap **views**, e.g., JPanels, using a CardLayout. Also, you should learn the basic rudiments of Java first, including how to create instances of a class of interest, before trying GUI coding, else you're in for a very frustrating experience. – Hovercraft Full Of Eels Feb 02 '16 at 16:51
  • Rephrasing my question: I want to know what my form's files should contain in order to switch between them using this code: btnLogin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { this.setVisible(false); new FrmMain().setVisible(true); // Main Form to show after the Login Form.. } }); – Sigmar Feb 02 '16 at 17:08
  • I believe you should have a look at the link @HovercraftFullOfEels posted. Using multiple JFrames is **not** good practice. Instead have a look at a [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Jonah Feb 02 '16 at 17:28
  • Ok, problem is I need this kind of solution for university (unfortunately...) and card layout is not an option here :/ Need to work with this for now... I would be glad if you helped me get to the solution, even though it is a bad pactise... – Sigmar Feb 02 '16 at 17:32
  • Why not use a modal dialog to login? – trashgod Feb 02 '16 at 17:46
  • 1
    You need to think about your design a little. What responsibilities does the login form have? Get details from the user? Should it be responsible for validating those details? Should it be responsible for making decisions about navigation? The answer to the last two questions is no. Validation of the details should be done be somebody else and the decision about what to do when based on the results of that validation should also be made by someone else. This makes trashgod's suggestion an important one. – MadProgrammer Feb 02 '16 at 20:20
  • Have a look at [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) for more details – MadProgrammer Feb 02 '16 at 20:20

1 Answers1

0

Here is an example on how to use multiple JFrames even though I don't recommend it. For future reference, I suggest you use a CardLayout

MainFrame.java

import javax.swing.*;

public class MainFrame
{
    JFrame mainFrame = new JFrame("Main Frame");

    public MainFrame()
    {
        addComponents();
    }

    public void addComponents()
    {
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(LoginFrame::new);
    }
}

LoginFrame.java

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

public class LoginFrame
{
    JFrame loginFrame = new JFrame("Login Frame");

    public LoginFrame()
    {
        addComponents();
    }

    public void addComponents()
    {
        JButton loginBtn = new JButton("Login");

        loginBtn.addActionListener(e -> {
            loginFrame.dispose();
            SwingUtilities.invokeLater(MainFrame::new);
        });


        loginFrame.add(loginBtn);
        loginFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        loginFrame.setLocationRelativeTo(null);
        loginFrame.setPreferredSize(new Dimension(300, 300));
        loginFrame.pack();
        loginFrame.setVisible(true);
    }
}
Jonah
  • 1,013
  • 15
  • 25
  • 2
    *"I suggest you use a CardLayout"* - Then I'd recommend doing an example which uses it. Also, I'd consider using `JDialog` over `JFrame` for the login view, but that's just me :P – MadProgrammer Feb 02 '16 at 20:16
  • @MadProgrammer, I did offer the suggestion of a CardLayout in the comments on the question. He replied by saying it has to use multiple JFrames which is why I chose not to make my answer be a example of a CardLayout. – Jonah Feb 03 '16 at 04:01