Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem involving action listener and if statements for multiple conditions

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem involving action listener and if statements for multiple conditions

    I'm writing a program for exemplary reasons that is basically two text fields, one for a username, and one for a password. the way its written is asking testing for what is inputed into the text fields if its equal to a "valid" username and password. if it is, a variable representing that is set equal to 1. there is another if statement asking if the actionevent.getsource is equal to the name of the textfield. the issue I had was that when I ran the program, it seemed to only test for the first condition, so I got the same result from the program regardless of what I input into the text field.

    The Code



    Main class:
    import javax.swing.JFrame;
    	public class Alpha {
            public static void main(String args[]){
                    log l = new log();
     
    		        l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		        l.setSize(360, 240);
    		        l.setVisible(true);
            }
    }

    Secondary or sub class:
    import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
     
    import javax.swing.JFrame; import javax.swing.JOptionPane;
    import javax.swing.JTextField; 
    public class log extends JFrame{
    	JTextField mess1;
    	JTextField un;
     
    	public log(){
    	super("Sign In");
    	setLayout(new FlowLayout());
    			mess1 = new JTextField("Enter your name here:");
    		mess1.setEditable(false);
    	add(mess1);
     
    		un = new JTextField("              ");
    	add(un);
     
    	thehandler asdf = new thehandler();
    		mess1.addActionListener(asdf);
    		un.addActionListener(asdf);
    	}
    	class thehandler implements ActionListener{
    		String s1;
    		int val = 0;
    	public void actionPerformed(ActionEvent eve){
    			if(eve.getSource().equals(un) && val == 1){
    				s1=String.format("Logging in, please wait...", eve.getActionCommand());
    				JOptionPane.showMessageDialog(null, s1);
    			}
    				else if(eve.getSource().equals(un) && val != 1)
    				s1=String.format("Your username or password is incorrect.", s1);
    			}
    		}
    	}
    *the code originaly was written "eve.getSource() ==" but I changed that to see if it would resolve the issue with no avail. With it written this way, the program does nothing upon entering the text and pressing enter. Currently, there is nothing written to change the variable "val" to 1 to show that the username is valid. I did this because I can't figure out how I would go about doing this.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Problem involving action listener and if statements for multiple conditions

    You gave no hint as to what the problem is, so don't forget to include that when you return. The common mistake seen in what you describe is using '==' to compare Strings rather than the equals() method, but I've no idea if that has anything to do with your problem.

    Please read this topic before posting your code so that you post it correctly.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem involving action listener and if statements for multiple conditions

    Quote Originally Posted by GregBrannon View Post
    You gave no hint as to what the problem is, so don't forget to include that when you return. The common mistake seen in what you describe is using '==' to compare Strings rather than the equals() method, but I've no idea if that has anything to do with your problem.

    Please read this topic before posting your code so that you post it correctly.
    I apologize for forgetting to state the problem in the post, I have edited it to more clearly state what is wrong. Also, thanks for the link to the topic on how to properly show code in a post.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Problem involving action listener and if statements for multiple conditions

    Spend some time in the API each time you use a new Class, even if you're following a tutorial and figure the author knows what they're talking about.

    The 'if' logic is a bit screwy, but you knew that. To state what you already know, val is set to 0 each time the ActionListener is entered, and I don't see where it can change to 1 while in the listener. Think through your logic, write down the steps on paper, AND THEN try to program it when you think it makes sense. Imagine a conversation between the computer and a user and how that would happen. Write a script with direction to describe what happens after each response. I'm hoping you'll find that everything won't happen inside the same clause, certainly not all within the ActionListener.

    As you program what you think makes sense, you might try adding simple print statements to follow what's happening. If you still need help, post the part you've written down on paper and we'll see if we agree that it makes sense.

Similar Threads

  1. add action listener problem
    By mdhmdh100 in forum AWT / Java Swing
    Replies: 5
    Last Post: February 5th, 2012, 01:53 AM
  2. Action Listener?user input problem
    By gpelefty90 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 2nd, 2011, 05:17 AM
  3. Problem with JText Fields and using action listener
    By toble in forum AWT / Java Swing
    Replies: 2
    Last Post: October 27th, 2010, 04:44 PM
  4. converter program, problem with action listener
    By robertson.basil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:44 AM
  5. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM