0

what I'm trying to do is open up a main application from a login screen, only after the login information has been verified in the connected database.

Using Eclipse, what I have so far:

database.java: connection to MS Access Database using UCanAccess. (Success)

login.java: A login window that extends JFrame. When a username and password is entered, it is verified with the database. (Success)

Home.java: The main application window, that I want to only be accessible with a correct username and password. Does not extend JFrame, but has a JFrame within it.

Now, I have been able to set it up so that if the entered username and password are correct, a window pops up saying "Successful login". However, how do I approach setting it up so that after the successful login, it opens up Home.java?

I have looked at: Open a new JFrame - I have tried the setVisible with my home but Eclipse returns an error saying to create a setVisible method in Home...I thought this is supposed to be an automatic control? After trying to create the method, more issues just arise.

JFrame Open Another JFrame - which suggests adding actionListener and then setting it visible..which I have done: public void actionPerformed(ActionEvent e) {this.setVisible(false); new Home().setVisible(true); but Eclipse just doesn't open up the login window at all. Initially, I thought it could be because my success message is in the actionListener, however even after removing that it still does not work.

Call Jframe from Java class and Open window after button click - My only conclusion is that this is not working since Home.java does not extend JFrame? However, I read through other sources that it is not good to use "extends JFrame"?

I guess I also don't have an understanding of the difference between "extends JFrame" vs a new JFrame within a class? I have been learning java on my own and I'm new to GUI creation. Maybe I am missing something very obvious, but I just can't find a solution.

Any ideas? Thanks

To give an idea, my Home.java starts like this:

public class Home {

    private JFrame frame;
    private JTable data;
    private JTextField Column1;
    private JTextField Column2;
    private JTable table;

    // Launch the application.

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

    // Create the application.

    public Home() {
        initialize();
    }

    //Initialize the contents of the frame.

    private void initialize() {


        frame = new JFrame();
        frame.setBounds(100, 100, 697, 518);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Community
  • 1
  • 1
code2learn
  • 9
  • 1
  • 5
  • Start by defining a simple work flow which allows you to understand the logical path you want your code to take. Break down the areas of responsibility so that you objects are only do the work that they absolutely have to, for example, your login component should only collect the credentials from the user, it should be responsible for validating them, that should be the responsibility of some kind of controller. Equally, neither the login component or it's controller should be responsible for determine what happens after a successful login, that is the responsibility for another controller – MadProgrammer Nov 02 '15 at 22:07
  • For [example](http://stackoverflow.com/questions/26517856/java-swing-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274) – MadProgrammer Nov 02 '15 at 22:08
  • What I got from it is that the controller idea is to assign responsibility to the actual mouse clicks. So, for example, when the login button is clicked it should send some sort of signal to then validate the entered credentials? Is that not what is already being done? I don't think I see the difference, or is this simply to keep the code more organized by breaking down the responsibility? – code2learn Nov 02 '15 at 22:26
  • The concept of a controller in Swing is a little different then a normal MVC implementation, because Swing is already a form of MVC. What I tend to do instead, is define a contract between the controller and the view which describes what events the view generates (and the events that the controller can expect), for example `attemptLogin`. This disconnects the controller from the view's implementation, so the view is free to form the view in what ever way it feels like, so long as when it wants to validate the actual credentials it calls `attemptLogin` – MadProgrammer Nov 02 '15 at 22:29
  • Ok, I think I get it....I think I will create a verifyLogin method in my database.java, and then a separate class that waits and is called when the login button is clicked; the entered username and password will then be sent via the verifyLogin and return either true or false that way. Now, the actual login isn't handling the validation, just the credentials. I'll definitely look more into controllers in Swing. Thanks – code2learn Nov 02 '15 at 22:43

2 Answers2

2

Start by defining a simple work flow which allows you to understand the logical path you want your code to take.

Break down the areas of responsibility so that your objects only do the work that they absolutely have to, for example, your login component should only collect the credentials from the user, it should not be responsible for validating them, that should be the responsibility of some kind of controller.

Equally, neither the login component or it's controller should be responsible for determine what happens after a successful login, that is the responsibility for another controller

This decouples the code and encourages reuse, so the next time you need to present some "login" view, you don't have to recode the whole thing, simply update the controller, model and/or view as required, or re-use it as it is

The concept of a controller in Swing is a little different then a normal MVC implementation, because Swing is already a form of MVC.

What I tend to do instead, is define a contract between the controller and the view which describes what events the view generates (and the events that the controller can expect), for example attemptLogin. This disconnects the controller from the view's implementation, so the view is free to form the view in what ever way it feels like, so long as when it wants to validate the actual credentials it calls attemptLogin

So, you would start with a "main controller" which is responsible for controlling the login and main application controllers. It defines the work flow between the login and the main application and monitors for appropriate events which the controllers may generate to make decisions about what it should do next

Responsibility groups

A basic flow of operation might look something like

Basic Flow

This concept is demonstrated in Java and GUI - Where do ActionListeners belong according to MVC pattern?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Just create a method in your Home class that sets its JFrame to be visible:

public void setJFrameVisible(boolean visible)
{
    frame.setVisible(visible);
}

Then, assuming your instance of your Home class is called "home", all you would have to do is:

home.setJFrameVisible(true);

Let me add a bit more context. When you're extending JFrame, the class inherits all the methods/properties of the JFrame class. That's why when you extend JFrame you can just call obj.setVisible(true), because your class inherited the setVisible method from the JFrame class. What you have is a class that contains a JFrame, so you have to call the setVisible method on the internal JFrame, not the class.

Cody Woolsey
  • 131
  • 5
  • Ok great, that edit worked. Had to create a new frame in the login code first and then use the setJFrameVisible on that newly created frame. – code2learn Nov 02 '15 at 21:39