Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jaylimix

Thread: ActionListener

  1. #1
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Thumbs up ActionListener

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class CalculatorApplet extends JApplet {
    
        private JTextField txtFirst= new JTextField();
        private JTextField txtSecond= new JTextField();
        private JButton btnAdd= new JButton("Add");
        private JButton btnMinus= new JButton("Minus");
        private JButton btnReset= new JButton("Reset");
        private JLabel lblAnswer= new JLabel("Enter Integer:", JLabel.CENTER);
    
        public CalculatorApplet() throws HeadlessException {
            
            JPanel x= new JPanel(new GridLayout(1,2));
            x.add(new JLabel("First Integer:"));
            x.add(txtFirst);
            
            JPanel y= new JPanel(new GridLayout(1,2));
            JLabel s= new JLabel("Second Integer");
            y.add(s);
            y.add(txtSecond);
            
            GridLayout g= new GridLayout(1,3);
            JPanel z= new JPanel(g);
            z.add(btnAdd);
            z.add(btnMinus);
            z.add(btnReset);
            
            setLayout(new GridLayout(4,1));
            add(x);
            add(y);
            add(z);
            add(lblAnswer);
            
            btnAdd.addActionListener(new ActionListener(){
                
                public void actionPerformed(ActionEvent e){
                    
                    int f= Integer.parseInt(txtFirst.getText());
                    int s= Integer.parseInt(txtSecond.getText());
                    int sum= f+s;
                    lblAnswer.setText("Answer: "+sum);
                }
            });
            
            btnMinus.addActionListener(new ActionListener(){
                
                public void actionPerformed(ActionEvent e){
                    
                    int f= Integer.parseInt(txtFirst.getText());
                    int s= Integer.parseInt(txtSecond.getText());
                    int sum= f-s;
                    lblAnswer.setText("Answer: "+sum);
                }
            }); 
           
            btnReset.addActionListener(new ActionListener(){
                
                public void actionPerformed(ActionEvent e){
                    
                    txtFirst.setText("");
                    txtSecond.setText("");
                    lblAnswer.setText("Enter Integer:");
                }
            });
        }
    }
    Hello all, good day especially to JosAh

    I'm thinking of a way to separate the codes for addActionListener to become something like this:

    Java Code:
    public class CalculatorApplet extends JApplet implements ActionListener{
    
    btnAdd.addActionListener(this);
    btnMinus.addActionListener(this);
    btnReset.addActionListener(this);
    Problem is the actionPerformed method can only be called once. Any way I can solve this?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: ActionListener

    You approach of using an "annonymouse inner class" is fine. Otherwise you will need to create an "inner class" for each ActionListener. Something like:
    Java Code:
    protected class AddActionListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            //  add your code here
        }
    }
    Then the code in your applet would be:

    Java Code:
    btnAdd.addActionListener( new AddActionListener() );
    Also, that is not the way to create an applet. Code to build the GUI should be done in the init() method. Take a look at How to Make Applets (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for a better approach.

  3. #3
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: ActionListener

    just type
    public void actionPerformed(ActionEvent ae){
    if(ae.getSource()==btnAdd){
    //add
    }
    else if(ae.getSource()==btnMinus){
    //subtract
    }

    }

  4. #4
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default Re: ActionListener

    Thank you to both of you. Cosmos's way is the one I was looking for.
    Cosmos likes this.

  5. #5
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: ActionListener

    Quote Originally Posted by jaylimix View Post
    Thank you to both of you. Cosmos's way is the one I was looking for.
    your welcome...:)

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: ActionListener

    Cosmos's way is the one I was looking for.
    Check out the discussion in this posting: action listeners

Similar Threads

  1. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 07:43 AM
  2. ActionListener - actionPerformed
    By collwill in forum New To Java
    Replies: 2
    Last Post: 04-21-2011, 06:01 PM
  3. problems with my ActionListener
    By xyknight in forum New To Java
    Replies: 3
    Last Post: 04-11-2011, 03:10 PM
  4. ActionListener+KeyListener
    By mandelbrot in forum AWT / Swing
    Replies: 5
    Last Post: 09-10-2010, 12:25 AM
  5. How to access the ActionListener
    By jboy in forum New To Java
    Replies: 3
    Last Post: 10-15-2009, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •