Login button code not working(model view controller)

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2075 Views - Last Post: 15 April 2013 - 11:00 AM Rate Topic: -----

#1 mattylynch1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Login button code not working(model view controller)

Posted 27 March 2013 - 03:30 PM

Below is the code for my "controller" and below that is some relevant code from my "view".
When i enter the username and password nothing happens. IT should bring up the main GUI. I had this working before when i had the action performed code etc all in the LoginGUI. but now that I have tried to use MVC I cant get it to bring it up. What am I missing?
public class Controller 
{
	 private LoginGUI lGUI;
	 private DocAppMainGUI DAMGUI;

public Controller(LoginGUI lGUI,DocAppMainGUI DAMGUI) 
{

    this.lGUI = lGUI;
    this.DAMGUI = DAMGUI;
    
    // Tell the LoginGUI(View) that when ever the login button

    // is clicked to execute the actionPerformed method

    // in the LoginListener inner class
    
     
    lGUI.addLoginListener(new LoginListener());

  
}
class LoginListener implements ActionListener{

	 

	 public void actionPerformed(ActionEvent login)
	 {

	        if(login.getSource() == lGUI.loginButton)
	        {
	        	
	        	String pass = new String(lGUI.passwordField.getPassword());

	            if(lGUI.usernameField.getText().compareTo("Matt") == 0 &&  pass.compareTo("password") == 0)
	            {
	            	
	            	JOptionPane.showMessageDialog( null, "Logging in.....");
	            	//LoginGUI.setVisible(false);
	            	//lGUI.frame.dispose();
	            	//DocAppMainGUI damg = new DocAppMainGUI();
	            	
	            	DAMGUI.createAndShowDocAppMainGUI();
	                
	                
	            }
	            
	            else
	            {
	            	JOptionPane.showMessageDialog( null, "Incorrect Details, Please check and try again" );
	            }

	        }
	    }
	}
}

}


// Button for Logging in
        loginButton = new JButton("Login");
        //loginButton.addActionListener(this);
        buttonPanel.add(loginButton, BorderLayout.SOUTH);
        
        //Button to cancel login and reset the login screen
        cancelButton = new JButton("Cancel");
        //cancelButton.addActionListener(this);
        buttonPanel.add(cancelButton, BorderLayout.SOUTH);

        LoginGUI.setOpaque(true);
        return LoginGUI;
    }
    void addLoginListener(ActionListener listenForLoginButton)
    {
    	loginButton.addActionListener(listenForLoginButton);
     }


Is This A Good Question/Topic? 0
  • +

Replies To: Login button code not working(model view controller)

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,238
  • Joined: 20-September 08

Re: Login button code not working(model view controller)

Posted 27 March 2013 - 04:22 PM

Add suitable debugging statements in you code, to, at least

a. ensure that you've added the right listener (you can call System.out.println(x.getClass()) here
b. ensure that the listener is called
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Login button code not working(model view controller)

Posted 27 March 2013 - 07:25 PM

View Postmattylynch1, on 27 March 2013 - 06:30 PM, said:

Below is the code for my "controller" and below that is some relevant code from my "view".

Not sure what you evaluate as "relevant code" is really relevant.
I guess if you knew what is really relevant you wouldn't be posting here :)
Was This Post Helpful? 0
  • +
  • -

#4 mattylynch1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Login button code not working(model view controller)

Posted 28 March 2013 - 01:50 PM

Well i added the actual code for the login button(relevant code from that class.)
Was This Post Helpful? 0
  • +
  • -

#5 mattylynch1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Login button code not working(model view controller)

Posted 28 March 2013 - 02:45 PM

Here is all the code for the login gui class.
public class LoginGUI {

    JPanel LoginGUI, labelPanel, textFieldPanel, buttonPanel;
    JLabel instructionLabel, usernameLabel, passwordLabel ;
    JTextField usernameField;
    JPasswordField passwordField;
    JButton loginButton, cancelButton;
     static JFrame frame;

    public JPanel createContentPane (){

      
        
         LoginGUI = new JPanel();
        LoginGUI.setSize(350,350);
        LoginGUI.setLayout(new BorderLayout(2, 2));
        LoginGUI.setBorder( new TitledBorder("LoginScreen") );
        

        // Setting the preferredSize is basically making sure the widget
        // is not re-sized to be smaller or bigger than this unless it needs to be.
        // Also, when adding, we have the area of the JPanel as a second argument.
        
        instructionLabel = new JLabel("To Login, enter the username and password for DocApp");
        instructionLabel.setForeground(Color.BLUE);
        instructionLabel.setPreferredSize(new Dimension(100, 15));
        
        instructionLabel.setHorizontalAlignment(0);
        LoginGUI.add(instructionLabel, BorderLayout.NORTH);
        
        
   	 	// Username Label
        usernameLabel = new JLabel("UserName");
        usernameLabel.setPreferredSize(new Dimension(70, 30));
        //usernameLabel.setBackground(Color.BLUE);
        //usernameLabel.setOpaque(true);
        
      
       
        // Password Label
        passwordLabel = new JLabel("Password");
        passwordLabel.setPreferredSize(new Dimension(70,30));
        //passwordLabel.setBackground(Color.red);
       // passwordLabel.setOpaque(true);
      
        // Username Textfield
        usernameField = new JTextField(8);
        //usernameField.setPreferredSize(new Dimension(70, 30));
       // usernameField.setHorizontalAlignment(0);
        usernameField.setMaximumSize( usernameField.getPreferredSize() );
        
        // Password Passwordfield
        passwordField = new JPasswordField(8);
        //passwordField.setPreferredSize(new Dimension(70, 30));
        //passwordField.setHorizontalAlignment(0); 
       // passwordField.setMaximumSize( passwordField.getPreferredSize() );

        
  //Creation of a Panel to contain the JLabels and text fields
        labelPanel = new JPanel();
        labelPanel.setPreferredSize(new Dimension(250, 150));
        //labelPanel.setBackground(Color.GREEN);
       // labelPanel.setMaximumSize( labelPanel.getPreferredSize() );
        
        Box verticalBoxforLabels = Box.createVerticalBox();  
        //Box row = Box.createHorizontalBox();
        //verticalBoxforLabels.setBorder( new TitledBorder("VertBOx4labels") );
        
        
        verticalBoxforLabels.add(usernameLabel);
        verticalBoxforLabels.add(Box.createVerticalGlue());
        verticalBoxforLabels.add(passwordLabel);
        
        
       
        
       
        Box verticalBoxforInputfields = Box.createVerticalBox();
        //verticalBoxforInputfields.setBorder( new TitledBorder("VertBOx4FIELDS") );
        verticalBoxforInputfields.add(usernameField);
        verticalBoxforInputfields.add(passwordField);
        
        labelPanel.add(verticalBoxforLabels );
        labelPanel.add(verticalBoxforInputfields);
        labelPanel.setVisible(true);
        LoginGUI.add(labelPanel, BorderLayout.CENTER);
        
        
       

       
    
        
     //Creation of a Panel to contain the textField and password field
       // textFieldPanel = new JPanel();
        //textFieldPanel.setPreferredSize(new Dimension(100, 70));
        //textFieldPanel.setBackground(Color.BLUE);
       // LoginGUI.add(textFieldPanel, BorderLayout.EAST);
        //textFieldPanel.setMaximumSize( textFieldPanel.getPreferredSize() );
       //textFieldPanel.add(vertBox);
        //textFieldPanel.setVisible(true);
        //LoginGUI.add(textFieldPanel, BorderLayout.EAST);
       
        //Image on left
        JPanel imagePanel = new JPanel(new BorderLayout(40,40));
        imagePanel.setPreferredSize(new Dimension(250, 200));
        
       // imagePanel.setBorder(
          //  new TitledBorder("Image goes here") );
        LoginGUI.add(imagePanel, BorderLayout.WEST);

        
        ImageIcon DOCICON = new ImageIcon("doc.gif");
        JLabel Image = new JLabel(DOCICON);
        imagePanel.add( Image, BorderLayout.NORTH );
       

     // Creation of a Panel to contain the Login/cancel buttons
        buttonPanel = new JPanel();
        buttonPanel.setPreferredSize(new Dimension(150, 35));
       // buttonPanel.setBackground(Color.RED);
        LoginGUI.add(buttonPanel, BorderLayout.SOUTH);
        
        // Button for Logging in
        loginButton = new JButton("Login");
        
        //loginButton.addActionListener(this);
        buttonPanel.add(loginButton, BorderLayout.SOUTH);
        
        //Button to cancel login and reset the login screen
        cancelButton = new JButton("Cancel");
        //cancelButton.addActionListener(this);
        buttonPanel.add(cancelButton, BorderLayout.SOUTH);

        LoginGUI.setOpaque(true);
        return LoginGUI;
    }
    void addLoginListener(ActionListener listenForLoginButton)
    {
    	loginButton.addActionListener(listenForLoginButton);
    	
    	
     }

   //Check to see if correct username and password are entered.
    

  /*  public void actionPerformed(ActionEvent login) {

        if(login.getSource() == loginButton)
        {
        	String pass = new String(passwordField.getPassword());

            if(usernameField.getText().compareTo("Matt") == 0 &&  pass.compareTo("password") == 0)
            {
            	
            	JOptionPane.showMessageDialog( null, "Logging in.....");
            	//LoginGUI.setVisible(false);
            	frame.dispose();
            	DocAppMainGUI damg = new DocAppMainGUI();
                damg.createAndShowDocAppMainGUI();
                
                
            }
            
            else
            {
            	JOptionPane.showMessageDialog( null, "Incorrect Details, Please check and try again" );
            }

        }
    }*/

   
    private static void createAndShowLoginGUI() 
    {

        
        frame = new JFrame("DOCAPP");

        LoginGUI docapp = new LoginGUI();
        frame.setContentPane(docapp.createContentPane());
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // We use pack to automatically size the frame.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) 
    {

		//Set look and feel to NImbus.
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception  UnsupportedLookAndFeelException) {
		    // If Nimbus is not available
			UIManager.getSystemLookAndFeelClassName();
		}
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable()
        {
     
     public void run() {
    	 
                createAndShowLoginGUI();
            }
        });
    }
}


View Postg00se, on 27 March 2013 - 04:22 PM, said:

Add suitable debugging statements in you code, to, at least

a. ensure that you've added the right listener (you can call System.out.println(x.getClass()) here
b. ensure that the listener is called

Could you clarify please? What is x exactly?
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,238
  • Joined: 20-September 08

Re: Login button code not working(model view controller)

Posted 28 March 2013 - 02:56 PM

Well 'x' could be several things, e.g.

a. a reference to the listener you're adding
b. a reference to the source of the event
Was This Post Helpful? 0
  • +
  • -

#7 mattylynch1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Login button code not working(model view controller)

Posted 28 March 2013 - 03:59 PM

View Postg00se, on 28 March 2013 - 02:56 PM, said:

Well 'x' could be several things, e.g.

a. a reference to the listener you're adding
b. a reference to the source of the event

Could you give me an example of what to do in the code. Ive been trying to figure this out with hours and everything has lost all meaning!
Was This Post Helpful? 0
  • +
  • -

#8 mattylynch1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Login button code not working(model view controller)

Posted 29 March 2013 - 01:36 PM

When i add the code as suggested in here nothing happens. Therefore the listener is not being added i think. But I have no idea what is wrong. Any suggestiong gladly welcomed.
void addLoginListener(ActionListener listenForLoginButton)
	    {
	    	loginButton.addActionListener(listenForLoginButton);
	    	System.out.println(loginButton.getClass());
	    	System.out.println("TEST");
	    	
	     }

Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Login button code not working(model view controller)

Posted 29 March 2013 - 02:56 PM

One thing for sure, the only thing to which your addLoginbListener() method adds a listener is the JButton

So when the actionPerformed() of your LoginListener is fired there is no need to validate who is the getSource(). Can't be anything else than the JButton
Was This Post Helpful? 0
  • +
  • -

#10 mattylynch1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Login button code not working(model view controller)

Posted 29 March 2013 - 04:22 PM

Im aware of that.Im just trying to get the login button before i start adding the rest of the listeners. is there anything else i should try or if there is any more info i can give to allow you to help?
Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Login button code not working(model view controller)

Posted 29 March 2013 - 06:01 PM

View Postmattylynch1, on 29 March 2013 - 07:22 PM, said:

is there anything else i should try or if there is any more info i can give to allow you to help?

You already give us too much info like having code testing consitions that can't happen and that we waste our time validating for nothing

Clean up your code !
Was This Post Helpful? 0
  • +
  • -

#12 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,238
  • Joined: 20-September 08

Re: Login button code not working(model view controller)

Posted 30 March 2013 - 07:26 AM

Quote

System.out.println(loginButton.getClass());
is a waste of time. You know it's a JButton

System.out.println(listenForLoginButton.getClass());
would be more useful. One thing's for sure - if you can't understand your own code and you can't keep track of it - it's a sign that things are designed wrongly

This post has been edited by g00se: 30 March 2013 - 07:26 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#13 mattylynch1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Login button code not working(model view controller)

Posted 07 April 2013 - 01:52 PM

Ok Guys, I've cleaned up the code. The login screen launches but i get the following message
Exception in thread "main" java.lang.NullPointerException
at LoginGUI.addLoginListener(LoginGUI.java:146)
at Controller.<init>(Controller.java:24)
at MVCDocApp.main(MVCDocApp.java:19)....
Still really confused...cant understand whats not initialised..See classes below:

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginGUI  
{

    JPanel LoginGui, labelPanel, textFieldPanel, buttonPanel;
    JLabel instructionLabel, usernameLabel, passwordLabel ;
    JTextField usernameField;
    JPasswordField passwordField;
    JButton loginButton, cancelButton;
    static JFrame frame;
    

    public JPanel createContentPane ()
    {
    	LoginGui = new JPanel();
    	LoginGui.setSize(350,350);
    	LoginGui.setLayout(new BorderLayout(2, 2));
    	LoginGui.setBorder( new TitledBorder("LoginScreen") );
        

        // Setting the preferredSize is basically making sure the widget
        // is not re-sized to be smaller or bigger than this unless it needs to be.
        // Also, when adding, we have the area of the JPanel as a second argument.
        
        instructionLabel = new JLabel("To Login, enter the username and password for DocApp");
        instructionLabel.setForeground(Color.BLUE);
        instructionLabel.setPreferredSize(new Dimension(100, 15));
        instructionLabel.setHorizontalAlignment(0);
        LoginGui.add(instructionLabel, BorderLayout.NORTH);
        
        
   	 	// Username Label
        usernameLabel = new JLabel("UserName");
        usernameLabel.setPreferredSize(new Dimension(70, 30));
        
        // Password Label
        passwordLabel = new JLabel("Password");
        passwordLabel.setPreferredSize(new Dimension(70,30));
       
       // Username Textfield
        usernameField = new JTextField(8);
        usernameField.setMaximumSize( usernameField.getPreferredSize() );
        
        // Password Passwordfield
        passwordField = new JPasswordField(8);
        passwordField.setPreferredSize(new Dimension(70, 30));
        passwordField.setMaximumSize( passwordField.getPreferredSize() );

        
  //Creation of a Panel to contain the JLabels and text fields
        labelPanel = new JPanel();
        labelPanel.setPreferredSize(new Dimension(250, 150));
       // labelPanel.setMaximumSize( labelPanel.getPreferredSize() );
        
        Box verticalBoxforLabels = Box.createVerticalBox();  
        verticalBoxforLabels.add(usernameLabel);
        verticalBoxforLabels.add(Box.createVerticalGlue());
        verticalBoxforLabels.add(passwordLabel);
    
        Box verticalBoxforInputfields = Box.createVerticalBox();
        verticalBoxforInputfields.add(usernameField);
        verticalBoxforInputfields.add(passwordField);
        
        labelPanel.add(verticalBoxforLabels );
        labelPanel.add(verticalBoxforInputfields);
        labelPanel.setVisible(true);
        LoginGui.add(labelPanel, BorderLayout.CENTER);
        
     
       
        //Image on left
        JPanel imagePanel = new JPanel(new BorderLayout(40,40));
        imagePanel.setPreferredSize(new Dimension(250, 200));
        LoginGui.add(imagePanel, BorderLayout.WEST);

        
        ImageIcon DOCICON = new ImageIcon("doc.gif");
        JLabel Image = new JLabel(DOCICON);
        imagePanel.add( Image, BorderLayout.NORTH );
       

     // Creation of a Panel to contain the Login/cancel buttons
        buttonPanel = new JPanel();
        buttonPanel.setPreferredSize(new Dimension(150, 35));
        LoginGui.add(buttonPanel, BorderLayout.SOUTH);
        
        // Button for Logging in
        loginButton = new JButton("Login");
        buttonPanel.add(loginButton, BorderLayout.SOUTH);
        
        //Button to cancel login and reset the login screen
        cancelButton = new JButton("Cancel");
        buttonPanel.add(cancelButton, BorderLayout.SOUTH);
        
        LoginGui.setOpaque(true);
        return LoginGui;
      }
    
    // If the loginButton is clicked execute a method
    // in the Controller named actionPerformed
   
 public void createAndShowLoginGUI() 
    {
	 try 
	 {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) 
		    {
		        if ("Nimbus".equals(info.getName())) 
		        {
		            UIManager.setLookAndFeel(info.getClassName());
		           
		            //break;
		        }
		    }
		} 
	 catch (Exception  UnsupportedLookAndFeelException)
	 {
		    // If Nimbus is not available
			UIManager.getSystemLookAndFeelClassName();
	 }
        frame = new JFrame("DOCAPP");
        LoginGUI docapp = new LoginGUI();
        frame.setContentPane(docapp.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // We use pack to automatically size the frame.
        frame.pack();
        frame.setVisible(true);
      }
public void addLoginListener(ActionListener listenForbuttonclick)
 
 {
	

	System.out.println(listenForbuttonclick.getClass());
 	loginButton.addActionListener(listenForbuttonclick);
 	cancelButton.addActionListener(listenForbuttonclick);
}
    
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

public class Controller 
{
	
	 private LoginGUI lGUI;
	 private DocAppMainGUI DAMGUI;

public Controller(LoginGUI lGui,DocAppMainGUI DAMGui) 
{

    this.lGUI = lGui;
    this.DAMGUI = DAMGui;
    
    lGUI.createAndShowLoginGUI();
    
    // Tell the LoginGUI(View) that when ever the login button
    // is clicked to execute the actionPerformed method
    // in the LoginListener inner class
    
    lGUI.addLoginListener(new LoginListener());
  

  
}
public class LoginListener implements ActionListener
{
	
	


	 public void actionPerformed(ActionEvent login)
	 {
		 
		 System.out.println(lGUI.getClass());
	        if(login.getSource() == lGUI.loginButton)
	        {
	        	
	        	String pass = new String(lGUI.passwordField.getPassword());

	            if(lGUI.usernameField.getText().compareTo("Matt") == 0 &&  pass.compareTo("password") == 0)
	            {
	            	
	            	JOptionPane.showMessageDialog( null, "Logging in.....");
	            	//LoginGUI.setVisible(false);
	            	//lGUI.frame.dispose();
	            	//DocAppMainGUI damg = new DocAppMainGUI();
	            	
	            	DAMGUI.createAndShowDocAppMainGUI();
	            }
	            
	            else
	            {
	            	JOptionPane.showMessageDialog( null, "Incorrect Details, Please check and try again" );
	            }

	        }
	        	
	    }

	
	
	}
}

public class MVCDocApp 
{
public static void main(String[] args) 
    {

    	LoginGUI theView = new LoginGUI();
    	DocAppMainGUI theView1 = new DocAppMainGUI();
        Controller theController = new Controller(theView,theView1);
     }

}

Was This Post Helpful? 0
  • +
  • -

#14 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Login button code not working(model view controller)

Posted 07 April 2013 - 05:36 PM

If you have correctly post your code your error is at

cancelButton.addActionListener(listenForbuttonclick);

and the line before is

loginButton.addActionListener(listenForbuttonclick);

so how can the loginButton being created and not the cancelButton ?

loginButton and cancelButton are created just one after the other...
so you posted code is not the relevant code
Was This Post Helpful? 0
  • +
  • -

#15 mattylynch1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: Login button code not working(model view controller)

Posted 08 April 2013 - 02:46 AM

I added the listener to cancel since the original post. Both these statements are causing error now(if i comment out 1 line the next one give the same error) and i dont know why this would cause java.lang.NullPointerException
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2