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

Thread: Help With ActionListener

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help With ActionListener

    Hello. I am trying to make a calculator using Java GUI. I've managed to make an ActionListener and add it to a button, but I've made an error in my code that I'm unsure of how to solve. Because of how I've written the code, only one number can be placed in the text field. For example, the an ActionListener for the three button on the calculator was added to the button, but no matter how many times the user presses the button, only one 3 will appear in the text field. The code is below:

    import javax.swing.*;//import the packages needed for gui
    import java.awt.*;
    import java.awt.event.*;
    public class Calculator {
        public static void main(String[] args) {
    	JFrame window = new JFrame("Window");//makes a JFrame
    	    window.setSize(300,350);
    	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	JPanel panel = new JPanel (new FlowLayout());//makes the panel, textfield and buttons
        final JTextField textField = new JTextField(20);
    	JButton openbracket = new JButton("(");
    	JButton closebracket = new JButton(")");
    	JButton clearbutton = new JButton("C");
    	JButton arcsin = new JButton("arcsin");
    	JButton arccos = new JButton("arccos");
    	JButton arctan = new JButton("arctan");
    	JButton sin = new JButton("sin");
    	JButton cos = new JButton("cos");
    	JButton tan = new JButton("tan");
    	JButton log = new JButton("log");
    	JButton seven = new JButton("7");
    	JButton eight = new JButton("8");
    	JButton nine = new JButton("9");
    	JButton four = new JButton("4");
    	    JButton five = new JButton("5");
    	    JButton six = new JButton("6");
    	    JButton one = new JButton("1");
    	    JButton two = new JButton("2");
    	    JButton three = new JButton("3");
    	    JButton zero = new JButton("0");
    	    JButton radixpoint = new JButton(".");
    	    JButton equal = new JButton("=");
    	    final String values = " ";
    	     class Listener implements ActionListener {	
    	     public void actionPerformed(ActionEvent e) {   		  
    		    String output = values + "3";
    		    textField.setText(output);
    		}
    	}
    	Listener listener = new Listener();
     panel.add(textField);//adding all the things
    	    window.add(panel);
    	panel.add(openbracket);
    	panel.add(closebracket);
    	panel.add(clearbutton);
    	panel.add(arcsin);
    	panel.add(arccos);
    	panel.add(arctan);
    	panel.add(sin);
    	panel.add(cos);
    	panel.add(tan);
    	panel.add(log);
    	panel.add(nine);
    	panel.add(eight);
    	panel.add(seven);
    	panel.add(six);
    	panel.add(five);
    	panel.add(four);
    	three.addActionListener(listener);
    	panel.add(three);
    	panel.add(two);
    	panel.add(one);
    	panel.add(zero);
    	panel.add(radixpoint);
    	panel.add(equal);
    	window.setVisible(true);
    }
    }

    As you can see, because the compiler forces the String variable to be final, so when the user presses the button, the code simply shows how a space character and three character would look like, because the String variable can't change. How do I write my code so that every time the user presses the button, a character is added to the text field?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help With ActionListener

    The String only has to be final because you're trying to do everything from the main method. Split this up into a more reasonable organization of your data: for example, you could create an instance of Calculator and move most of this logic into the constructor. Then the String would be a class-level variable of the Calculator class, and you could either access it from an anonymous inner class or set it via a setter function.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    GregBrannon (July 29th, 2014)

  4. #3
    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

    You should be comfortable with OOP design and experienced with OOP program organization before writing a GUI program in Java. Your current design suggests that you're neither comfortable nor experienced with OOP. You might back up a bit and start with basic OOP exercises and tutorials, writing many programs (studies) that give you the experience needed.

Similar Threads

  1. ActionListener
    By ericgomez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 22nd, 2013, 12:25 PM
  2. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  3. Help with ActionListener please
    By knightmetal in forum AWT / Java Swing
    Replies: 3
    Last Post: August 23rd, 2011, 05:41 PM
  4. ActionListener help
    By hello_world in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2011, 11:45 PM
  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