Results 1 to 6 of 6
  1. #1
    mpankhurst is offline Member
    Join Date
    Dec 2011
    Location
    Southern Wisconsin
    Posts
    8
    Rep Power
    0

    Default Changing the text in a JTextField

    I have a calculator problem I'm working on and am getting an error. I will post the classes I have and the error I'm getting:

    First Class:
    Java Code:
    public class awesomeCalc
    {
    	public static void main(String[] args)
    	{
    		new buildCalc();
    	}
    }

    Second Class:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class buildCalc extends JFrame
    {
    	private final int WINDOW_WIDTH = 400;
    	private final int WINDOW_HEIGHT = 200;
    	public buildLabel topDisplay;
    	private buildButtons buttonDisplay;
    	public buildCalc()
    	{
    		topDisplay = new buildLabel();
    		buttonDisplay = new buildButtons();
    		setTitle("Awesome Calculator!");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new BorderLayout());
    		add(topDisplay, BorderLayout.NORTH);
    		add(buttonDisplay, BorderLayout.CENTER);
    		
    		pack();
    		setVisible(true);
    	}
    }
    Third Class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class buildLabel extends JPanel
    {
    	public JTextField display;
    	private String currentLabel = "0";
    	public buildLabel()
    	{
    		JTextField display = new JTextField("0");
    		add(display);
    		setVisible(true);
    		
    	}
    	public void changeDisplay()
    	{
    		display.setText("1");
    	}
    }
    Fourth Class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class buildButtons extends JPanel
    {
    	private JButton num1;
    	private JButton num2;
    	private JButton num3;
    	private JButton num4;
    	private JButton num5;
    	private JButton num6;
    	private JButton num7;
    	private JButton num8;
    	private JButton num9;
    	private JButton num0;
    	private JButton clear;
    	private JButton numDec;
    	private JButton add;
    	private JButton subtract;
    	private JButton multiply;
    	private JButton divide;
    	
    	public buildButtons()
    	{
    		setLayout(new GridLayout(4,4));
    		JButton num1 = new JButton("1");
    		JButton num2 = new JButton("2");
    		JButton num3 = new JButton("3");
    		JButton num4 = new JButton("4");
    		JButton num5 = new JButton("5");
    		JButton num6 = new JButton("6");
    		JButton num7 = new JButton("7");
    		JButton num8 = new JButton("8");
    		JButton num9 = new JButton("9");
    		JButton num0 = new JButton("0");
    		JButton numDec = new JButton(".");
    		JButton clear = new JButton("Clear");
    		JButton add = new JButton("+");
    		JButton subtract = new JButton("-");
    		JButton multiply = new JButton("*");
    		JButton divide = new JButton("/");
    		
    		num1.addActionListener(new num1Listener());
    		
    		add(num1);
    		add(num2);
    		add(num3);
    		add(divide);
    		add(num4);
    		add(num5);
    		add(num6);
    		add(multiply);
    		add(num7);
    		add(num8);
    		add(num9);
    		add(subtract);
    		add(clear);
    		add(num0);
    		add(numDec);
    		add(add);
    		
    		setVisible(true);
    		
    		
    	}
    buildLabel changes = new buildLabel();
    private class num1Listener implements ActionListener
    	{
    		
    		public void actionPerformed(ActionEvent e)
    		{
    			changes.changeDisplay();
    		}
    	}
    }
    Changing the text in a JTextField-errormessage.png


    The problem occurs when I press the num1 button. I am trying to set the textfield to the number 1 and failing miserably. Any help would be appreciated.

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    3

    Default Re: Changing the text in a JTextField

    The error is in your buildLabel class. Try removing the JTextField from the constructor:

    Like this:
    Java Code:
        public buildLabel()
        {
            display = new JTextField("0");
            add(display);
            setVisible(true);
             
        }

  3. #3
    mpankhurst is offline Member
    Join Date
    Dec 2011
    Location
    Southern Wisconsin
    Posts
    8
    Rep Power
    0

    Default Re: Changing the text in a JTextField

    I put in your fix and I'm no longer getting the error but it's not actually changing the text when I call the method from the buildButtons class. The following is what I changed the buildLabel class to:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class buildLabel extends JPanel
    {
    	public JTextField display;
    	private String currentLabel = "0";
    	public buildLabel()
    	{
    		display = new JTextField("0");
    		add(display);
    		setVisible(true);
    		
    	}
    	public void changeDisplay()
    	{
    		display.setText("1");
    	}
    }
    any further help or resources you can point me at would be appreciated

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Changing the text in a JTextField

    You're changing a JTextField, just not the one that is being displayed. Just look at your code and you'll see that you display the buildLabel that's been placed in the variable called topDisplay, and then you change the JLabel in ta buildLabel object that is held by the in a variable called changes. Again, they're two different objects. If you want to see a change in the state of a visualized object, you must call methods on that very same object.

  5. #5
    Sebasnr is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: Changing the text in a JTextField

    As Fubarable said, you're creating a new instance of "buildLabel" in your "buildButtons" class, and in consequence, you're editing the JTextField of "changes" (which isn't visible) instead the topDisplay's JTextField. Try this out:


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class buildCalc extends JFrame
    {
        private final int WINDOW_WIDTH = 400;
        private final int WINDOW_HEIGHT = 200;
        public buildLabel topDisplay;
        private buildButtons buttonDisplay;
        public buildCalc()
        {
            topDisplay = new buildLabel();
            buttonDisplay = new buildButtons(topDisplay);
            setTitle("Awesome Calculator!");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
            add(topDisplay, BorderLayout.NORTH);
            add(buttonDisplay, BorderLayout.CENTER);
             
            pack();
            setVisible(true);
        }
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class buildButtons extends JPanel
    {
        private JButton num1;
        private JButton num2;
        private JButton num3;
        private JButton num4;
        private JButton num5;
        private JButton num6;
        private JButton num7;
        private JButton num8;
        private JButton num9;
        private JButton num0;
        private JButton clear;
        private JButton numDec;
        private JButton add;
        private JButton subtract;
        private JButton multiply;
        private JButton divide;
        private buildLabel changes;
         
        public buildButtons(buildLabel changes)
        {
            this.changes = changes;
            setLayout(new GridLayout(4,4));
            JButton num1 = new JButton("1");
            JButton num2 = new JButton("2");
            JButton num3 = new JButton("3");
            JButton num4 = new JButton("4");
            JButton num5 = new JButton("5");
            JButton num6 = new JButton("6");
            JButton num7 = new JButton("7");
            JButton num8 = new JButton("8");
            JButton num9 = new JButton("9");
            JButton num0 = new JButton("0");
            JButton numDec = new JButton(".");
            JButton clear = new JButton("Clear");
            JButton add = new JButton("+");
            JButton subtract = new JButton("-");
            JButton multiply = new JButton("*");
            JButton divide = new JButton("/");
             
            num1.addActionListener(new num1Listener());
             
            add(num1);
            add(num2);
            add(num3);
            add(divide);
            add(num4);
            add(num5);
            add(num6);
            add(multiply);
            add(num7);
            add(num8);
            add(num9);
            add(subtract);
            add(clear);
            add(num0);
            add(numDec);
            add(add);
             
            setVisible(true);
             
             
        }
    
    private class num1Listener implements ActionListener
        {
             
            public void actionPerformed(ActionEvent e)
            {
                changes.changeDisplay();
            }
        }
    }

  6. #6
    mpankhurst is offline Member
    Join Date
    Dec 2011
    Location
    Southern Wisconsin
    Posts
    8
    Rep Power
    0

    Default Re: Changing the text in a JTextField

    Thanks guys the solution you guys gave works better than what I came up with. I was sending a function from the buildButtons to the buildCalc and having the buildCalc then call the buildLabel and change the text. Passing the object works way better thanks again :)

Similar Threads

  1. Remember the text in JTextField
    By seredi in forum New To Java
    Replies: 11
    Last Post: 09-21-2011, 06:55 PM
  2. Replies: 4
    Last Post: 04-19-2011, 05:09 AM
  3. Changing text color in SWT
    By ourimaler in forum SWT / JFace
    Replies: 1
    Last Post: 06-02-2010, 01:08 PM
  4. Reaplacing text in JTextField
    By PhQ in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 12:25 AM
  5. Changing the color of text
    By Lang in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 09:51 AM

Posting Permissions

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