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 jButton

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with jButton

    I've created a calculator. Two .java files, one the calculator, one the GUI.

    I'm stuck on how to link up the jButton ActionListeners.... wondering if anyone can help...

    import javax.swing.*;
    import java.awt.event.*;
     
    public class ButtonDemo extends Calculator{
     
        private JButton btnAdd,  btnMinus,  btnMultiply,  btnDivide,  btnPercent,  btnSqrRoot,  btnTotal,  btnExit;
        private JLabel lblTotal,  lblNumber;
        private JTextField txtTotal,  txtNumber;
        private JPanel panel;
        private JFrame frame;
     
        public ButtonDemo() {
            frame = new JFrame();
            frame.setTitle("Calculator");
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
     
            panel = new JPanel();
            panel.setLayout(null);
     
            lblTotal = new JLabel("Total");
            lblTotal.setBounds(10, 50, 100, 20);
            panel.add(lblTotal);
     
            txtTotal = new JTextField();
            txtTotal.setBounds(110, 50, 150, 20);
    		txtTotal.addActionListener(new TotalHandler());
            panel.add(txtTotal);
     
            lblNumber = new JLabel("Number");
            lblNumber.setBounds(10, 70, 100, 20);
            panel.add(lblNumber);
     
            txtNumber = new JTextField("");
            txtNumber.setBounds(110, 70, 150, 20);
            panel.add(txtNumber);
     
            btnAdd = new JButton("Add");
            btnAdd.setBounds(50, 110, 100, 20);
            btnAdd.addActionListener(new AddHandler());
            panel.add(btnAdd);
     
            btnMinus = new JButton("Minus");
            btnMinus.setBounds(150, 110, 100, 20);
            btnMinus.addActionListener(new MinusHandler());
            panel.add(btnMinus);
     
            btnMultiply = new JButton("Multiply");
            btnMultiply.setBounds(50, 130, 100, 20);
            btnMultiply.addActionListener(new MultiplyHandler());
            panel.add(btnMultiply);
     
            btnDivide = new JButton("Divide");
            btnDivide.setBounds(150, 130, 100, 20);
            btnDivide.addActionListener(new DivideHandler());
            panel.add(btnDivide);
     
            btnPercent = new JButton("Percent");
            btnPercent.setBounds(50, 150, 100, 20);
            btnPercent.addActionListener(new PercentHandler());
            panel.add(btnPercent);
     
            btnSqrRoot = new JButton("SqrRoot");
            btnSqrRoot.setBounds(150, 150, 100, 20);
            btnSqrRoot.addActionListener(new SqrRootHandler());
            panel.add(btnSqrRoot);
     
            btnTotal = new JButton("Total");
            btnTotal.setBounds(50, 170, 100, 20);
            btnTotal.addActionListener(new TotalHandler());
            panel.add(btnTotal);
     
            btnExit = new JButton("Exit");
            btnExit.setBounds(150, 170, 100, 20);
            btnExit.addActionListener(new ExitHandler());
            panel.add(btnExit);
     
            frame.getContentPane().add(panel);
            frame.setVisible(true);
        }
     
        class TotalHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			txtTotal.setText(total +  "");
            }
        }
     
        class AddHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			AddTotal totals = new AddTotal();
     
            }
        }
     
        class MinusHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
        class MultiplyHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
        class DivideHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
        class PercentHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
        class SqrRootHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
         class ExitHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        }
     
     
        public static void main(String[] args) {
           new ButtonDemo();
        }
    }

    import javax.swing.*;
    import java.awt.event.*;
     
    public class ButtonDemo extends Calculator{
     
        private JButton btnAdd,  btnMinus,  btnMultiply,  btnDivide,  btnPercent,  btnSqrRoot,  btnExit;
        private JLabel lblTotal,  lblNumber;
        private JTextField txtTotal,  txtNumber;
        private JPanel panel;
        private JFrame frame;
     
        public ButtonDemo() {
            frame = new JFrame();
            frame.setTitle("Calculator");
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
     
            panel = new JPanel();
            panel.setLayout(null);
     
            lblTotal = new JLabel("Total");
            lblTotal.setBounds(10, 50, 100, 20);
            panel.add(lblTotal);
     
            txtTotal = new JTextField();
            txtTotal.setBounds(110, 50, 150, 20);
    		txtTotal.addActionListener(new TotalHandler());
            panel.add(txtTotal);
     
            lblNumber = new JLabel("Number");
            lblNumber.setBounds(10, 70, 100, 20);
            panel.add(lblNumber);
     
            txtNumber = new JTextField("");
            txtNumber.setBounds(110, 70, 150, 20);
            panel.add(txtNumber);
     
            btnAdd = new JButton();
            btnAdd.setBounds(50, 110, 100, 20);
            btnAdd.addActionListener(new AddHandler());
            panel.add(btnAdd);
     
            btnMinus = new JButton("Minus");
            btnMinus.setBounds(150, 110, 100, 20);
            btnMinus.addActionListener(new MinusHandler());
            panel.add(btnMinus);
     
            btnMultiply = new JButton("Multiply");
            btnMultiply.setBounds(50, 130, 100, 20);
            btnMultiply.addActionListener(new MultiplyHandler());
            panel.add(btnMultiply);
     
            btnDivide = new JButton("Divide");
            btnDivide.setBounds(150, 130, 100, 20);
            btnDivide.addActionListener(new DivideHandler());
            panel.add(btnDivide);
     
            btnPercent = new JButton("Percent");
            btnPercent.setBounds(50, 150, 100, 20);
            btnPercent.addActionListener(new PercentHandler());
            panel.add(btnPercent);
     
            btnSqrRoot = new JButton("SqrRoot");
            btnSqrRoot.setBounds(150, 150, 100, 20);
            btnSqrRoot.addActionListener(new SqrRootHandler());
            panel.add(btnSqrRoot);
     
            btnExit = new JButton("Exit");
            btnExit.setBounds(100, 170, 100, 20);
            btnExit.addActionListener(new ExitHandler());
            panel.add(btnExit);
     
            frame.getContentPane().add(panel);
            frame.setVisible(true);
        }
     
        class TotalHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			txtTotal.setText(total +  "");
            }
        }
     
        class AddHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			!!!!!!STUCK HERE!!!!!! Need to call the 'add()' method from calculator......!!!!
            }
        }
     
        class MinusHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
        class MultiplyHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
        class DivideHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
        class PercentHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
        class SqrRootHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
     
            }
        }
     
         class ExitHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        }
     
     
        public static void main(String[] args) {
           new ButtonDemo();
        }
    }
    Last edited by ls7897; March 13th, 2011 at 12:20 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with jButton

    What do you mean by 'link up'? Do you mean call the methods of Calculator from the action listeners? As ButtonDemo extends Calculator, you can just directly call the method(s), passing entered value from the JTextField (which must first be converted to a primitive - see the API's for the primitive wrappers, for example Double parseDouble() function)

  3. The Following User Says Thank You to copeg For This Useful Post:

    ls7897 (March 13th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with jButton

    Quote Originally Posted by copeg View Post
    What do you mean by 'link up'? Do you mean call the methods of Calculator from the action listeners? As ButtonDemo extends Calculator, you can just directly call the method(s), passing entered value from the JTextField (which must first be converted to a primitive - see the API's for the primitive wrappers, for example Double parseDouble() function)
    This is exactly what I mean, but I just can't work out how to do it correctly. I'm getting errors everytime including 'cannot put add(Double) into jButton....

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with jButton

    What have you tried? What exactly are the errors?

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with jButton

        class AddHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			AddTotal totals = new AddTotal();
    			totals.add();
            }
        }

        class AddHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			txtTotal.add(total);
            }
        }

        class AddHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
    			return add();
            }
        }

    I'm really lost!!!!

  7. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help with jButton

    Moved thread to - AWT / Java Swing
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with jButton

    Break the problem down into smaller steps 1) get the user entered value - you can use the getText() method to retrieve this from a JTextField 2) Convert that value to something the method you wish to use understands - if you have a String and need to pass a double, you can use Double.parseDouble 3) pass that value to your method(s) - call your method with the value obtained in 2. In the form of the action listener, these steps are written in pseudo-code below.
    public void actionPerformed(ActionEvent e){
      //1
      //2
      //3
    }

Similar Threads

  1. How do i have a jButton Open a URL?
    By dragon40226 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 7th, 2011, 04:21 AM
  2. JButton.... actionListener.... help!!!!....
    By eiramae in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 05:54 AM
  3. JButton help
    By tabutcher in forum Java Applets
    Replies: 4
    Last Post: March 9th, 2010, 07:04 PM
  4. JButton Help
    By ravjot28 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2010, 11:38 AM
  5. JButton...
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2009, 11:39 AM