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: Actionlistener issue

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

    Default Actionlistener issue

    This is what I am trying to accomplish: Write an application for Lambertís Vacation Rentals. Use separate ButtonGroups to allow a client to select one of three locations, the number of bedrooms, and whether meals are included in the rental. Assume that the locations are parkside for $600 per week, poolside for $750 per week, or lakeside for $825 per week. Assume that the rentals have one, two or three bedrooms and that each bedroom over one adds %75 to the base prices. Assume that if meals are added, the price is $200 more per rental.

    And so far this is what I have but I can't for the life of me, figure out where i am going wrong with the actionlistener stuff. I have tried a dozen different ways to do this. I just need a pointer in the correct direction to get this code moving again.

    package swing;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Swing extends JFrame implements ActionListener {
     
        public static void main(String[] args) { 
     
       final int parkside = 600, poolside = 750, lakeside = 825;
       final int plusmeals = 200, onebed = 0, twobed = 75, threebed = 150; 
       int total; 
      JFrame frame = new JFrame ("Lamberts Vacation Rentals");
      frame.setVisible (true);
      frame.setSize(400,400);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel panel = new JPanel();
      frame.add(panel);
      JLabel vacation = new JLabel("Lamberts Vacation Rental");
      JLabel price = new JLabel("The price for your vacation is: ");
      JRadioButton loc1, loc2, loc3, room1, room2, room3;
      JCheckBox meals, none;
      JTextField finaltotal = new JTextField(8);
     
      loc1 = new JRadioButton("Parkside $600");
      loc2 = new JRadioButton("Poolside $750");
      loc3 = new JRadioButton("Lakeside $825");
      meals = new JCheckBox ("Include meals for $200");
      none = new JCheckBox ("Do NOT include meals");
      room1 = new JRadioButton("One Bedroom");
      room2 = new JRadioButton("Two bedrooms (add $75)");
      room3 = new JRadioButton("Three bedrooms (add $150)");
     
      ButtonGroup location = new ButtonGroup();
      location.add(loc1);
      location.add(loc2);
      location.add(loc3);
      panel.add(vacation);
      panel.add(loc1);
      panel.add(loc2);
      panel.add(loc3);
      ButtonGroup addmeals = new ButtonGroup();
      addmeals.add(meals);
      addmeals.add(none);
      panel.add(meals);
      panel.add(none);
      ButtonGroup bedrooms = new ButtonGroup();
      bedrooms.add(room1);
      bedrooms.add(room2);
      bedrooms.add(room3);
      panel.add(room1);
      panel.add(room2);
      panel.add(room3);
      panel.add(price);
      panel.add(finaltotal);
     
      finaltotal.setText("$" + finaltotal);
      public Swing ();
      loc1.addActionListener(new Action());
      loc2.addActionListener(new Action());
      loc3.addActionListener(new Action());
      meals.addActionListener(new Action());
      none.addActionListener(new Action());
      room1.addActionListener(new Action());
      room2.addActionListener(new Action());
      room3.addActionListener(new Action());
          }
     
        public void actionPerformed (ActionEvent e) {
      Object source = e.getSource();
     
     if(source == JRadioButton){
         if (select == e.SELECTED);
     
     
     
     
     }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Actionlistener issue

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Are you getting errors? If so, please post them.

    What are you trying to do with this block of code?:
            public Swing ();
            loc1.addActionListener(new Action());
            loc2.addActionListener(new Action());
            loc3.addActionListener(new Action());
            meals.addActionListener(new Action());
            none.addActionListener(new Action());
            room1.addActionListener(new Action());
            room2.addActionListener(new Action());
            room3.addActionListener(new Action());
    Oh! I get it. public Swing() (the first line) is supposed to be a constructor. Then the lines above should look like:
            public Swing ()
            {
                loc1.addActionListener(new Action());
                loc2.addActionListener(new Action());
                loc3.addActionListener(new Action());
                meals.addActionListener(new Action());
                none.addActionListener(new Action());
                room1.addActionListener(new Action());
                room2.addActionListener(new Action());
                room3.addActionListener(new Action());
            }
    I'm not a fan of the 'this' action listener, but it's probably what you've learned based on the rest of your code. In that theme, the 'add' lines above can be 'fixed' by changing to:

    loc1.addActionListener( this );

    AND by moving the variable declarations outside the main() method so that they can be instance variables.

    Lots to do. Make those changes and come back when you're ready for more help. Post your updated code when you return.

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

    Wink Re: Actionlistener issue

    I have actually worked through my problem! Then I checked this and you were correct by using the (this) instead of the action part. I started by trying to use our book and then that was explaining enough so I ventured to youtube and well..that made me a lot more confused.

    Now i have this, which actually works ^.^
    Sometimes you just have to deleted everything and start all over.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Swing extends JFrame implements ActionListener {
     
       JRadioButton loc1;
       JRadioButton loc2;
       JRadioButton loc3;
       JCheckBox meals;
       JCheckBox none;
       JRadioButton room1;
       JRadioButton room2;
       JRadioButton room3;
       JTextField finaltotal = new JTextField(8);
       final int parkside = 600, poolside = 750, lakeside = 825;
       final int plusmeals = 200, onebed = 0, twobed = 75, threebed = 150;
       int total = 0;
     
       public static void main(String[] args) {
           new Swing();
       }
     
       public Swing() {
           JFrame frame = new JFrame("Lamberts Vacation Rentals");
     
           JPanel panel = new JPanel();
           frame.add(panel);
           JLabel vacation = new JLabel("Lamberts Vacation Rental");
           JLabel price = new JLabel("The price for your vacation is: ");
     
           loc1 = new JRadioButton("Parkside $600");
           loc2 = new JRadioButton("Poolside $750");
           loc3 = new JRadioButton("Lakeside $825");
           meals = new JCheckBox("Include meals for $200");
           none = new JCheckBox("Do NOT include meals");
           room1 = new JRadioButton("One Bedroom");
           room2 = new JRadioButton("Two bedrooms (add $75)");
           room3 = new JRadioButton("Three bedrooms (add $150)");
     
           ButtonGroup location = new ButtonGroup();
           location.add(loc1);
           location.add(loc2);
           location.add(loc3);
           panel.add(vacation);
           panel.add(loc1);
           panel.add(loc2);
           panel.add(loc3);
           ButtonGroup addmeals = new ButtonGroup();
           addmeals.add(meals);
           addmeals.add(none);
           panel.add(meals);
           panel.add(none);
           ButtonGroup bedrooms = new ButtonGroup();
           bedrooms.add(room1);
           bedrooms.add(room2);
           bedrooms.add(room3);
           panel.add(room1);
           panel.add(room2);
           panel.add(room3);
           panel.add(price);
           panel.add(finaltotal);
           frame.setVisible(true);
           frame.setSize(400, 400);
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           loc1.addActionListener(this);
           loc2.addActionListener(this);
           loc3.addActionListener(this);
           meals.addActionListener(this);
           none.addActionListener(this);
           room1.addActionListener(this);
           room2.addActionListener(this);
           room3.addActionListener(this);
       }
     
       public void actionPerformed(ActionEvent e) {
     
           if(loc1.isSelected()) {
               total = parkside;
           }
           else if(loc2.isSelected()) {
               total = poolside;
           }
           if(loc3.isSelected()) {
               total = lakeside;
           }
           if(room1.isSelected()) {
               total += onebed;
           }
           else if(room2.isSelected()) {
               total += twobed;
           }
           if(room3.isSelected()) {
               total += threebed;
           }
           if(meals.isSelected()) {
               total += plusmeals;
           }
           else if(none.isSelected()) {
               total -= 0;
           }
     
           finaltotal.setText("$" + total);
       }
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Actionlistener issue

    Good job working through it. You're right, sometimes falling back and regrouping with a clean sheet of paper can be the best approach, but many find that too hard to do, continuing to polish that which won't ever shine.

Similar Threads

  1. Need help with ActionListener if-else
    By texasPI in forum Loops & Control Statements
    Replies: 4
    Last Post: September 6th, 2013, 07:18 PM
  2. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  3. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  4. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 11:25 AM
  5. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM

Tags for this Thread