Passing Variable from One class to another, using an ActionListener

  • Follow


I have three classes. The Main class that starts the application, the
ButtonListener class that listens for an events and the Processing
class which does all the data processing.

(Main
Class)----------------------------------------------------------------------------------------------------------------------------------------------------

package Program;

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

public class LoginGUI {
     public static void main(String arg[]){

        JFrame login_frame = new JFrame();
        login_frame.setName("");
        login_frame.setSize(1024,576);

        ImagePanel login_panel = new ImagePanel("pictures/
login_bg.gif");
        login_panel.setSize(1024,576);
        login_panel.setVisible(true);
        //login_panel.setLayout(new BoxLayout(login_panel,
BoxLayout.Y_AXIS));

        JLabel lblUsername = new JLabel();
        lblUsername.setText("USERNAME:");
        lblUsername.setOpaque(true);
        lblUsername.setVisible(true);

        JTextField txtbxUsername = new JTextField(10);

        JLabel lblPassword = new JLabel();
        lblPassword.setText("PASSWORD:");
        lblPassword.setOpaque(true);
        lblPassword.setVisible(true);

        JPasswordField txtbxPassword = new JPasswordField(10);

        JButton btnLogin = new JButton();
        btnLogin.setText("LOGIN");
        ButtonListener btnLoginListener = new ButtonListener();
        btnLogin.addActionListener(btnLoginListener);
        btnLogin.setActionCommand("btnLoginClicked");

        login_panel.add(lblUsername);
        login_panel.add(txtbxUsername);
        login_panel.add(lblPassword);
        login_panel.add(txtbxPassword);
        login_panel.add(btnLogin);

        login_frame.add(login_panel);
        login_frame.setVisible(true);
     }

    }

(ButtonListener
Class)---------------------------------------------------------------------------------------------------------------------------------------------------------------

package Program;

import java.awt.event.*;

public class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent evt){
        String Username = "";
        String Password = "";

        String action = evt.getActionCommand();
        if (action.equals("btnLoginClicked")){
            Processing LoginProcessing = new Processing();
            LoginProcessing.Login(Username,Password);
        }
     }
}

(Processing
Class)-------------------------------------------------------------------------------------------------------------------------------------------------------------------

package Program;

import javax.swing.*;

public class Processing {
    public void Login(String username, String password){
        JFrame fLogin = new JFrame();
        fLogin.setSize(1024,576);

        JPanel pLogin = new JPanel();
        pLogin.setSize(1024,576);

        fLogin.add(pLogin);
        fLogin.pack();
        fLogin.setVisible(true);
    }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Everything so far, works just fine. When I click on the Login Button
in the Main Class, the event is picked up by the ButtonListener Class
and the Processing Class creates a new frame. What I need to happen,
is for the text of the Username and Password textboxes in the Main
Class to be sent through the Event Listener and on to the Processing
class. Or whatever other method there is to get the username and
password over to the Processing Class, so I can then check it against
the database.

I have spent three days reading books and online information to see if
I can find a solution. All I can find is how to send variables from
one class to another, but that information doesn't seem to work with
an action listener.
0
Reply wyount (23) 12/14/2009 4:11:39 PM

Jerim wrote:

> What I need to happen,
> is for the text of the Username and Password textboxes in the Main
> Class to be sent through the Event Listener and on to the Processing
> class. 


There's a couple of ways to do this.  The most immediate solution is to 
add the text fields to the action listener so it can query them before 
dispatching the processing class.

 > public class ButtonListener implements ActionListener{

   private JTextField password;
   private JTextField username;

     public ButtonListener( JTextField password, JTextField username ) {
       this.password = password;
       this.username = username;
     }
 >     public void actionPerformed(ActionEvent evt){
 >
 >         String action = evt.getActionCommand();
 >         if (action.equals("btnLoginClicked")){
 >             Processing LoginProcessing = new Processing();

 >             LoginProcessing.Login(username.getText(),
                  password.getText() );

 >         }
 >      }
 > }

Then just create the ButtonListener object with the correct fields.

 > public class LoginGUI {
 >      public static void main(String arg[]){
 >
   // NOTE: THIS SHOULD BE DONE ON THE EDT, NOT THE MAIN THREAD!!!

 >         JFrame login_frame = new JFrame();
 >         login_frame.setName("");
 >         login_frame.setSize(1024,576);
....
 >         JTextField txtbxUsername = new JTextField(10);
....
 >         JPasswordField txtbxPassword = new JPasswordField(10);
 >
 >         JButton btnLogin = new JButton();
 >         btnLogin.setText("LOGIN");
 >         ButtonListener btnLoginListener =
            new ButtonListener( txtbxPassword, txbxUsername );
 >         btnLogin.addActionListener(btnLoginListener);


The other way is to define an interface on the JFrame object, and use 
that inside the ActionListener to get your strings.  Fundamentally, it's 
the same concept as what I've done here: inject the needed object into 
the button listener.

Interfaces are more work because it involves more classes, more design 
and conceptualization work, more moving parts to maintain, etc. 
Interfaces are nicer because the result should be more testable than 
using complicated classes like JTextField.


Other comments:  you absolutely MUST create your Swing objects on the 
EDT.  The program is broken if you don't.

<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html>

Also, Java by convention starts method names and field names with a 
lower case letter.  Login.Login() should be Login.login(), etc.  Don't 
use underscores in variable names (login_frame), it's considered gauche. 
  Don't call setName("") on a Swing component, it does nothing.  I've 
never had to set the opacity of a JLabel, it should work fine by default.

Good luck.


0
Reply markspace 12/14/2009 4:46:13 PM


On Dec 14, 10:46=A0am, markspace <nos...@nowhere.com> wrote:
> Jerim wrote:
> > What I need to happen,
> > is for the text of the Username and Password textboxes in the Main
> > Class to be sent through the Event Listener and on to the Processing
> > class.
>
> There's a couple of ways to do this. =A0The most immediate solution is to
> add the text fields to the action listener so it can query them before
> dispatching the processing class.
>
> =A0> public class ButtonListener implements ActionListener{
>
> =A0 =A0private JTextField password;
> =A0 =A0private JTextField username;
>
> =A0 =A0 =A0public ButtonListener( JTextField password, JTextField usernam=
e ) {
> =A0 =A0 =A0 =A0this.password =3D password;
> =A0 =A0 =A0 =A0this.username =3D username;
> =A0 =A0 =A0}
> =A0> =A0 =A0 public void actionPerformed(ActionEvent evt){
> =A0>
> =A0> =A0 =A0 =A0 =A0 String action =3D evt.getActionCommand();
> =A0> =A0 =A0 =A0 =A0 if (action.equals("btnLoginClicked")){
> =A0> =A0 =A0 =A0 =A0 =A0 =A0 Processing LoginProcessing =3D new Processin=
g();
>
> =A0> =A0 =A0 =A0 =A0 =A0 =A0 LoginProcessing.Login(username.getText(),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 password.getText() );
>
> =A0> =A0 =A0 =A0 =A0 }
> =A0> =A0 =A0 =A0}
> =A0> }
>
> Then just create the ButtonListener object with the correct fields.
>
> =A0> public class LoginGUI {
> =A0> =A0 =A0 =A0public static void main(String arg[]){
> =A0>
> =A0 =A0// NOTE: THIS SHOULD BE DONE ON THE EDT, NOT THE MAIN THREAD!!!
>
> =A0> =A0 =A0 =A0 =A0 JFrame login_frame =3D new JFrame();
> =A0> =A0 =A0 =A0 =A0 login_frame.setName("");
> =A0> =A0 =A0 =A0 =A0 login_frame.setSize(1024,576);
> ...
> =A0> =A0 =A0 =A0 =A0 JTextField txtbxUsername =3D new JTextField(10);
> ...
> =A0> =A0 =A0 =A0 =A0 JPasswordField txtbxPassword =3D new JPasswordField(=
10);
> =A0>
> =A0> =A0 =A0 =A0 =A0 JButton btnLogin =3D new JButton();
> =A0> =A0 =A0 =A0 =A0 btnLogin.setText("LOGIN");
> =A0> =A0 =A0 =A0 =A0 ButtonListener btnLoginListener =3D
> =A0 =A0 =A0 =A0 =A0 =A0 new ButtonListener( txtbxPassword, txbxUsername )=
;
> =A0> =A0 =A0 =A0 =A0 btnLogin.addActionListener(btnLoginListener);
>
> The other way is to define an interface on the JFrame object, and use
> that inside the ActionListener to get your strings. =A0Fundamentally, it'=
s
> the same concept as what I've done here: inject the needed object into
> the button listener.
>
> Interfaces are more work because it involves more classes, more design
> and conceptualization work, more moving parts to maintain, etc.
> Interfaces are nicer because the result should be more testable than
> using complicated classes like JTextField.
>
> Other comments: =A0you absolutely MUST create your Swing objects on the
> EDT. =A0The program is broken if you don't.
>
> <http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html=
>
>
> Also, Java by convention starts method names and field names with a
> lower case letter. =A0Login.Login() should be Login.login(), etc. =A0Don'=
t
> use underscores in variable names (login_frame), it's considered gauche.
> =A0 Don't call setName("") on a Swing component, it does nothing. =A0I've
> never had to set the opacity of a JLabel, it should work fine by default.
>
> Good luck.

Thanks for pointing me in the right direction. That worked like a
charm. Very interesting read on EDT.
0
Reply Jerim 12/15/2009 1:24:40 AM

2 Replies
1325 Views

(page loaded in 0.441 seconds)

Similiar Articles:













7/20/2012 7:41:54 PM


Reply: