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 5 of 5

Thread: Need help with ActionListener if-else

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with ActionListener if-else

    New member here and new to Java as well. I'm taking a Java class this semester and I'm having trouble with an assignment.

    I need to create a program that produces a message based on an age within an age range I define. That's where I'm having trouble.
    I've got a JButton setup with an ActionListener but I can't figure out how to do the ActionEvent.

    I want to do an age range of 8-12 years old, 20-30 years old and 40-50 years old and when the JButton is pressed, I want it to display a different message depending on the age.

    Can anyone point me in the right direction?

    Here's what I have so far:
    displayJButton = new JButton();
            displayJButton.setText( "Display Wisdom" );
            displayJButton.setBounds( 100, 42, 280, 21 );
            displayJButton.addActionListener(
                  new ActionListener()
                  {
                      public void actionPerformed(ActionEvent event)
                      {
                          displayJButtonActionPerformed(event);
                      }
                  }
               );   
          contentPane.add( displayJButton );

    And...

    public void displayJButtonActionPerformed(ActionEvent click)
    { 
       String age=ageJTextField.getText();
       if ((age.equals "9"))
     
       displayJLabel.setText("yeah");
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with ActionListener if-else

    It seems you have the button/actionListener portion figured out. Is it that you don't know how to write the code to respond according to the age range? If so, expand the displayJButtonActionPerformed() method to something like:
    int ageNumber = Integer.parseInt( age );
    if ( ageNumber >= 8 && ageNumber < 13 )
    {
        // do the 8  - 12 range thing
    }
    else if ( . . . ) // the next age range
    {
        // and similarly
    }
    else if ( . . . )

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with ActionListener if-else

    Yes, my issue is with JButtonActionPerformed. My textbook is very sparse in this area and I tried several examples I found online but I keep getting errors. I'll try your example. Thank you, really. I've spent all day trying to get this to work.

    Erik

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with ActionListener if-else

    Let us know if you have any problems coding that up and getting the results you need.

    Edit: Start simple, code one of the 'if' clauses and test it to make sure you're on the right track. Since I'm not sure exactly what your problem is, I don't want you to go too far down the wrong path.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with ActionListener if-else

    Again, many thanks! I should have sought help this morning before wasting all day stumbling around in the dark.

    Just for my future reference here's what ended up working for me:

    public void displayJButtonActionPerformed(ActionEvent click)
    { 
       String age=ageJTextField.getText();
     
       int ageNumber = Integer.parseInt( age );
     
       if ( ageNumber >= 8 && ageNumber < 13 )
    {
        displayJLabel.setText("Goosebumps books are the best!");
    }
       else if (ageNumber >= 13 && ageNumber < 18)
       {
           displayJLabel.setText ("No Dating allowed!");
       }
       else if (ageNumber >= 18 && ageNumber <30)
       {
           displayJLabel.setText ("Best years of my life!");
       }
    }

Similar Threads

  1. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  2. Help with ActionListener please
    By knightmetal in forum AWT / Java Swing
    Replies: 3
    Last Post: August 23rd, 2011, 05:41 PM
  3. ActionListener help
    By hello_world in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2011, 11:45 PM
  4. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM
  5. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM