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

Thread: help with actionlistener for my calculator; button are created within a for loop for number 1 to9

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with actionlistener for my calculator; button are created within a for loop for number 1 to9

    i help some help with coding the action listener for my button (btBody) which create button displaying 1 to 9 in a nested for loop; the button should allow the user to click on them and to display the number clicked on in a JTextField;

    my code;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class EX_7_2 extends JFrame 
        {
            public EX_7_2()
            {
                setLayout(new BorderLayout(5, 10));
     
                // create p1
                JPanel p1 = new JPanel();
                p1.setLayout(new BorderLayout(5, 10));
     
              p1.add(new JTextField(), BorderLayout.NORTH);
     
              JPanel p3 = new JPanel();
     
             p3.setLayout(new GridLayout(1, 3, 5, 5));
             JButton btBackspace = new JButton ("Backspace");
             btBackspace.setBackground(Color.WHITE);
             btBackspace.setForeground(Color.RED);
     
             JButton btCE = new JButton ("CE");
             btCE.setBackground(Color.WHITE);
             btCE.setForeground(Color.RED);
     
             JButton btC = new JButton ("C");
             btC.setBackground(Color.WHITE);
             btC.setForeground(Color.RED);
     
              p3.add(btBackspace);
              p3.add(btCE);
              p3.add(btC);
     
              p1.add(p3, BorderLayout.CENTER);
     
     
     
              // create p4
     
              JPanel p2 = new JPanel();
              p2.setLayout(new GridLayout(4, 3, 5, 5));  
     
              int num;
              for (int iRow = 3; iRow>0; iRow--)
              {
                  for (int iCol = 1; iCol<4; iCol++)
                  {
                      num = 3*(iRow-1) + iCol;
     
                      JButton btBody = new JButton("" + num);
                      btBody.setBackground(Color.WHITE);
                      btBody.setForeground(Color.BLUE);
                      p2.add(btBody);
     
                       btBody.addActionListener(new ActionListener() 
                       {
                        @Override /**HandleItemEvent*/
                        public void actionPerformed(ActionEvent e) 
                            {
     
     
                            }                     
     
     
                      });
                  }           
              }
     
             JButton btZero = new JButton ("0");
             btZero.setBackground(Color.WHITE);
             btZero.setForeground(Color.BLUE);
     
             JButton btPlusMinus = new JButton ("+/-");
             btPlusMinus.setBackground(Color.WHITE);
             btPlusMinus.setForeground(Color.BLUE);
     
             JButton btDot = new JButton (".");
             btDot.setBackground(Color.WHITE);
             btDot.setForeground(Color.BLUE);
     
             p2.add(btZero);
              p2.add(btPlusMinus);
              p2.add(btDot);
     
     
             JPanel p5 = new JPanel();
             p5.setLayout(new GridLayout(4, 2, 5, 5));
     
             JButton btBack = new JButton ("/");
             btBack.setBackground(Color.WHITE);
             btBack.setForeground(Color.RED);
     
             JButton btsprt = new JButton ("sprt");
             btsprt.setBackground(Color.WHITE);
             btsprt.setForeground(Color.BLUE);
     
             JButton btStar = new JButton ("*");
             btStar.setBackground(Color.WHITE);
             btStar.setForeground(Color.RED);
     
             JButton btPercentage = new JButton ("%");
             btPercentage.setBackground(Color.WHITE);
             btPercentage.setForeground(Color.BLUE);
     
             JButton btMinus = new JButton ("-");
             btMinus.setBackground(Color.WHITE);
             btMinus.setForeground(Color.RED);
     
             JButton btOnex = new JButton ("1/x");
             btOnex.setBackground(Color.WHITE);
             btOnex.setForeground(Color.BLUE);
     
             JButton btPlus = new JButton ("+");
             btPlus.setBackground(Color.WHITE);
             btPlus.setForeground(Color.RED);
     
             JButton btEquals = new JButton ("=");
             btEquals.setBackground(Color.WHITE);
             btEquals.setForeground(Color.RED);
     
             p5.add(btBack);
               p5.add(btsprt);
                p5.add(btStar);
                 p5.add(btPercentage);
                  p5.add(btMinus);
                   p5.add(btOnex);
                    p5.add(btPlus);
                     p5.add(btEquals);
     
                     JPanel p4 = new JPanel();
                     p4.setLayout(new BorderLayout(5, 10));
                     p4.add(p2, BorderLayout.CENTER);                
          p4.add(p5, BorderLayout.EAST);          
     
          add(p1, BorderLayout.NORTH );
          add(p4, BorderLayout.CENTER);
     
     
     
        }             
     
     
    public static void main(String[] args) {
     
            EX_7_2 frame = new EX_7_2();
            frame.setTitle("Calculator");
            frame.setSize(350, 375);
                  frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
    }
    }


  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: help with actionlistener for my calculator; button are created within a for loop for number 1 to9

    Okay. I'm not sure why you insist. Continue in this thread and I'll close the other.

    Good luck.

  3. #3
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: help with actionlistener for my calculator; button are created within a for loop for number 1 to9

    Look into getActionCommand():
    ActionEvent (Java Platform SE 7 )
    That has a little description for it. Your code doesn't quite conform to what is usually seen but in terms of being able to work...that should be doable. Anyway do research on the above mentioned method.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with actionlistener for my calculator; button are created within a for loop for number 1 to9

    Also do a Search on this forum for examples of code that use the getSource() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with actionlistener for my calculator; button are created within a for loop for number 1 to9

    i have and tried but cant to seem to get it working, could you give the the correct code to try

  6. #6
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: help with actionlistener for my calculator; button are created within a for loop for number 1 to9

    I won't just give you code and Norm certainly won't, take some time (more than a few minutes) to look for information on getActionCommand and getSource. To get you started google "<insert method> examples". You may have done this already but go over it again and you may see something you missed before.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with actionlistener for my calculator; button are created within a for loop for number 1 to9

    i have and tried but cant to seem to get it working
    Post the code that shows what you are trying to do so we can help you understand what is wrong and direct you to ways to do it.

    --- Update ---

    Also posted at: Adding ActionListener to 9 JButtons (Swing / AWT / SWT forum at JavaRanch)
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    KucerakJM (March 8th, 2014)

Similar Threads

  1. Replies: 1
    Last Post: June 19th, 2013, 03:44 AM
  2. ActionListener not working when button is clicked.
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 19
    Last Post: April 10th, 2012, 04:23 PM
  3. Help with button actionlistener
    By umerahmad in forum Java Theory & Questions
    Replies: 9
    Last Post: August 25th, 2011, 07:21 AM
  4. GUI Calculator: Adding Answer Button? Please Help
    By Staticity in forum What's Wrong With My Code?
    Replies: 29
    Last Post: July 27th, 2011, 03:18 PM
  5. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM