Cant get Action listener to work.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Cant get Action listener to work.

  1. #1
    Join Date
    Mar 2005
    Posts
    3

    Cant get Action listener to work.

    Hi Guys!! im having some trouble with my college project. basically i want create a password and username facility. when im clicking on the login button, the action listener appended to the Button isnt listening or taking in the arguments fo rteh user name and password.

    i have posted the code up here and hopefully some one can help me out. i recon the syntax is correct. i have checked my braced just to make sure your listener class is OUTSIDE init() but INSIDE my applet class. my listeners are also appened. plz help me thanx DrunkinP

    /**
    * Title: project
    * Description:
    * Copyright: Copyright (c) 2005
    * Company: college practicals
    * @author trish
    * @version 1.0
    */

    import javax.swing.JOptionPane;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.*;

    public class project extends Applet
    {
    Label label1;
    Button buttonEnterDetails;
    Button buttonUpdateDetails;
    Button buttonContactAdmin;
    Button buttonSearchForBook;
    Button buttonLogin;
    Button button1 = new Button();
    TextArea textUserName;
    TextArea textPassWord;

    public void init()
    {
    setLayout(null);
    setBackground(Color.white);
    setSize(800, 800); //size of applet in web page

    label1 = new Label("Welcome to Books & Buy", Label.CENTER);
    label1.setFont(new Font("Serif", Font.BOLD, 20));
    label1.setBounds(70,8,280,40);
    label1.setBackground(Color.white);
    label1.setForeground(Color.blue);
    add(label1); // add label1 to web page

    Label labelUserName = new Label("User Name");
    labelUserName.setFont(new Font("Serif", Font.BOLD, 15));
    labelUserName.setBounds(8,100,80,40);
    labelUserName.setBackground(Color.white);
    labelUserName.setForeground(Color.blue);
    add(labelUserName); // add label1 to web page

    TextField textUserName = new TextField();
    textUserName.setBounds(100,100,80,30);
    add(textUserName);

    Label labelPassWord = new Label("Password");
    labelPassWord.setFont(new Font("Serif", Font.BOLD, 15));
    labelPassWord.setBounds(200,100,80,40);
    labelPassWord.setBackground(Color.white);
    labelPassWord.setForeground(Color.blue);
    add(labelPassWord); // add label1 to web page


    TextField textPassWord = new TextField();
    textPassWord.setBounds(300,100,80,30);
    textPassWord.setEchoChar('*');
    add(textPassWord);

    Button buttonLogin = new Button();
    buttonLogin.setLabel("Login");
    buttonLogin.setBounds(390,100,60,30);
    buttonLogin.setBackground(Color.white);
    buttonLogin.setForeground(Color.blue);
    add(buttonLogin);

    buttonEnterDetails = new Button();
    buttonEnterDetails.setLabel("Enter Details");
    buttonEnterDetails.setBounds(8,152,86,32);
    buttonEnterDetails.setBackground(Color.white);
    buttonEnterDetails.setForeground(Color.blue);

    add(buttonEnterDetails);

    buttonUpdateDetails = new Button();
    buttonUpdateDetails.setLabel("Update Details");
    buttonUpdateDetails.setBounds(104,152,86,32);
    buttonUpdateDetails.setBackground(Color.white);
    buttonUpdateDetails.setForeground(Color.blue);

    add(buttonUpdateDetails);

    buttonContactAdmin = new Button();
    buttonContactAdmin.setLabel("ContactAdmin");
    buttonContactAdmin.setBounds(200,152,86,32);
    buttonContactAdmin.setBackground(Color.white);
    buttonContactAdmin.setForeground(Color.blue);

    add(buttonContactAdmin);

    buttonSearchForBook = new Button();
    buttonSearchForBook.setLabel("Search");
    buttonSearchForBook.setBounds(300,152,86,32);
    buttonSearchForBook.setBackground(Color.white);
    buttonSearchForBook.setForeground(Color.blue);

    add(buttonSearchForBook);

    ButtonListener ourButtonListener = new ButtonListener();
    buttonEnterDetails.addActionListener(ourButtonListener);
    buttonUpdateDetails.addActionListener(ourButtonListener);
    buttonContactAdmin.addActionListener(ourButtonListener);
    buttonSearchForBook.addActionListener(ourButtonListener);
    buttonLogin.addActionListener(ourButtonListener);

    }

    class ButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent event)
    {
    Object press = event.getSource();
    if(press == buttonLogin)

    buttonLogin_ActionPerformed(event);

    }
    }

    void buttonLogin_ActionPerformed(ActionEvent event)
    {
    if(textUserName.getText().equalsIgnoreCase("phillipa") &&
    textPassWord.getText().equalsIgnoreCase("flynn"))
    {
    JOptionPane.showMessageDialog(null,"Welcome to Babes in the Books!!");

    }

    else if(!(textUserName.getText().equalsIgnoreCase("phillipa") &&
    textPassWord.getText().equalsIgnoreCase("flynn")))
    {
    JOptionPane.showMessageDialog(null,"Invalid Username and/or Password");

    }
    }

    }

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174

    Talking

    Hi DrunkinP

    There a quite a few problems with this code - all to do with local and class variable scope.

    If you define an item within a method for instance

    Code:
    public void aMethod()
    {
           ButtonListener ourButtonListener = new ButtonListener();
    }
    As soon as aMethod is finished, the object ourButtonListener will go out of scope and disappear ready for garbage collection, the same is the case in this code for the TextFields.

    The code below seems to work ok.

    Code:
    /**
     * Title: project
     * Description:
     * Copyright: Copyright (c) 2005
     * Company: college practicals
     * @author trish
     * @version 1.0
     */
    
    import javax.swing.JOptionPane;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.*;
    
    public class project extends Applet
    {
        Label label1;
        Button buttonEnterDetails;
        Button buttonUpdateDetails;
        Button buttonContactAdmin;
        Button buttonSearchForBook;
        Button buttonLogin;
        Button button1 = new Button();
        TextField textUserName;
        TextField textPassWord;
        ButtonListener ourButtonListener;
        
        public void init()
        {
            setLayout(null);
            setBackground(Color.white);
            setSize(800, 800); //size of applet in web page
            
            label1 = new Label("Welcome to Books & Buy", Label.CENTER);
            label1.setFont(new Font("Serif", Font.BOLD, 20));
            label1.setBounds(70,8,280,40);
            label1.setBackground(Color.white);
            label1.setForeground(Color.blue);
            add(label1); // add label1 to web page
            
            Label labelUserName = new Label("User Name");
            labelUserName.setFont(new Font("Serif", Font.BOLD, 15));
            labelUserName.setBounds(8,100,80,40);
            labelUserName.setBackground(Color.white);
            labelUserName.setForeground(Color.blue);
            add(labelUserName); // add label1 to web page
            
            textUserName = new TextField();
            textUserName.setBounds(100,100,80,30);
            add(textUserName);
            
            Label labelPassWord = new Label("Password");
            labelPassWord.setFont(new Font("Serif", Font.BOLD, 15));
            labelPassWord.setBounds(200,100,80,40);
            labelPassWord.setBackground(Color.white);
            labelPassWord.setForeground(Color.blue);
            add(labelPassWord); // add label1 to web page
            
            
            textPassWord = new TextField();
            textPassWord.setBounds(300,100,80,30);
            textPassWord.setEchoChar('*');
            add(textPassWord);
            
            buttonLogin = new Button();
            buttonLogin.setLabel("Login");
            buttonLogin.setBounds(390,100,60,30);
            buttonLogin.setBackground(Color.white);
            buttonLogin.setForeground(Color.blue);
            add(buttonLogin);
            
            buttonEnterDetails = new Button();
            buttonEnterDetails.setLabel("Enter Details");
            buttonEnterDetails.setBounds(8,152,86,32);
            buttonEnterDetails.setBackground(Color.white);
            buttonEnterDetails.setForeground(Color.blue);
            
            add(buttonEnterDetails);
            
            buttonUpdateDetails = new Button();
            buttonUpdateDetails.setLabel("Update Details");
            buttonUpdateDetails.setBounds(104,152,86,32);
            buttonUpdateDetails.setBackground(Color.white);
            buttonUpdateDetails.setForeground(Color.blue);
            
            add(buttonUpdateDetails);
            
            buttonContactAdmin = new Button();
            buttonContactAdmin.setLabel("ContactAdmin");
            buttonContactAdmin.setBounds(200,152,86,32);
            buttonContactAdmin.setBackground(Color.white);
            buttonContactAdmin.setForeground(Color.blue);
            
            add(buttonContactAdmin);
            
            buttonSearchForBook = new Button();
            buttonSearchForBook.setLabel("Search");
            buttonSearchForBook.setBounds(300,152,86,32);
            buttonSearchForBook.setBackground(Color.white);
            buttonSearchForBook.setForeground(Color.blue);
            
            add(buttonSearchForBook);
            
            ourButtonListener = new ButtonListener();
            buttonEnterDetails.addActionListener(ourButtonListener);
            buttonUpdateDetails.addActionListener(ourButtonListener);
            buttonContactAdmin.addActionListener(ourButtonListener);
            buttonSearchForBook.addActionListener(ourButtonListener);
            buttonLogin.addActionListener(ourButtonListener);
            
        }
        
        class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                Object press = event.getSource();
                if(press == buttonLogin)
                {
                    buttonLogin_ActionPerformed(event);
                }
            }
        }
        
        void buttonLogin_ActionPerformed(ActionEvent event)
        {
            if(textUserName.getText().equalsIgnoreCase("phillipa") &&
                    textPassWord.getText().equalsIgnoreCase("flynn"))
            {
                JOptionPane.showMessageDialog(null,"Welcome to Babes in the Books!!");
                
            }
            
            else if(!(textUserName.getText().equalsIgnoreCase("phillipa") &&
                    textPassWord.getText().equalsIgnoreCase("flynn")))
            {
                JOptionPane.showMessageDialog(null,"Invalid Username and/or Password");
                
            }
        }
     }
    Enjoy.
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center