9 Replies - 6086 Views - Last Post: 13 January 2011 - 03:26 PM Rate Topic: -----

#1 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 02:42 PM

Hi,

I've been doing Java for a little while, but all of my small programs so far have had just one class. I'm currently working on a project, and I would like to use one class called GUI for the interface, and others for various functions, in this case the login code.

Right now, the login code is in the GUI class, my main class.

public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == btnLogin)
		{
			String user = txtUser.getText();
			char[] pass = txtPass.getPassword();
			
			if (user.equals("John") && isPasswordCorrect(pass)) 
			{
				JOptionPane.showMessageDialog(null, "Access granted", "Login", JOptionPane.INFORMATION_MESSAGE);
				Tabs.setEnabledAt(0,false);
				Tabs.setEnabledAt(1,true);
				Tabs.setSelectedIndex(1);
            } 
			else 
			{
				txtUser.setText("");
				txtPass.setText("");
				txtUser.requestFocusInWindow();
				JOptionPane.showMessageDialog(null, "Login failed. Please check your username and password before trying again.", "Login Failed", JOptionPane.ERROR_MESSAGE);
            }

            //Zero out the possible password, for security.
            Arrays.fill(pass, '0');

		} 
	}
	
	private static boolean isPasswordCorrect(char[] pass) 
	{
        boolean isCorrect = true;
        char[] correctPassword = { 'h', 'o', 'n', 'e', 'y', 'c', 'o', 'm', 'b' };

        if (pass.length != correctPassword.length) 
		{
            isCorrect = false;
        } 
		else 
		{
            isCorrect = Arrays.equals (pass, correctPassword);
        }

        //Zero out the password.
        Arrays.fill(correctPassword,'0');

        return isCorrect;
    }



However, I would like this to be in the Login class to keep the GUI class strictly for the code of the interface itself. How do I take this action listener from GUI.class to Login.class so that is still functions when btnLogin is pressed? I'm not very well versed on using multiple classes.

Many thanks, any guidance would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Using Action Listener In Separate Class From Main

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 02:50 PM

So, move it into another class and add an instance of that class as an actionListener.

class Handler implements ActionListener {
    // Your actionPerformed() method here
}

class GUI ... {
    ...
    public GUI() {
        ...
        addActionListener(new Handler());
        ...
    }
    ...
}

Was This Post Helpful? 0
  • +
  • -

#3 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 02:53 PM

addActionListener(new Handler());
(correction ;))

Also, beware that trying to access anything created in the GUI class can't be easily accessed from other classes. (Though, that's not to say it's impossible...)

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 02:58 PM

Unless you make it an inner class. Then, that's certainly an option.
Was This Post Helpful? 0
  • +
  • -

#5 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 03:05 PM

Ah ok, so if I had, say

class Login implements ActionListener 
{
	public void actionPerformed(ActionEvent e)
  {
        //code
  }
}



It would be able to do the same job it was doing in the main GUI class?

And then, am I correct in assuming that I would have to place the code

        addActionListener(new Login());



In the GUI class?
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 03:08 PM

Yes, assuming you didn't need any information from the GUI class. If you do, you can pass them into the constructor and handled in the Login class or in a subclass.
Was This Post Helpful? 0
  • +
  • -

#7 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 03:11 PM

Ok, thanks.

So, if I needed, say "user" and "pass" from the GUI, I could pass them with...

addActionListener(new Login(text, pass));



And then use them in the other class? Makes enough sense!
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 03:19 PM

Precisely. If you ever needed to change those values, just put a method in the class to change it. Then, instead of making it an anonymous object, you can make a full object (normally created objects).
Was This Post Helpful? 1
  • +
  • -

#9 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 03:22 PM

View PostDogstopper, on 13 January 2011 - 02:19 PM, said:

Precisely. If you ever needed to change those values, just put a method in the class to change it. Then, instead of making it an anonymous object, you can make a full object (normally created objects).


Ok, thanks for your help I appreciate it.

Will take a closer look at modifying what I have and get things working!
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Using Action Listener In Separate Class From Main

Posted 13 January 2011 - 03:26 PM

You're welcome! Come back if you have any other questions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1