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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 33

Thread: Having trouble storing variable from JTextField

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Having trouble storing variable from JTextField

    Hi,
    I want the user to input their name etc into a Jtextfield and it will return their details, but I keep getting an error saying cannot find symbol getText()
    Here is my code
    public void deliveryWindow(){
     
                JFrame newFrame = new JFrame("Delivery Details");
     
                Container pane = getContentPane();
                newFrame.setLayout(new FlowLayout());
                newFrame.setSize(new Dimension(250, 250));
                //newFrame.getContentPane().add(panel);
                newFrame.setLocation(300,300);
                newFrame.setSize(250,250);
                newFrame.setVisible(true);
                JButton submitButton = new JButton("submit");
                newFrame.add(submitButton);
                ButtonEventHandler handler = new ButtonEventHandler();
                submitButton.addActionListener(handler);
     
                JPanel name = new JPanel();
                JLabel nameLabel = new JLabel();
                nameLabel.setText("Name :");
                JTextField nameField = new JTextField(15);
                String deliveryName = nameField.getText();
                name.add(nameLabel);
                newFrame.add(nameLabel);
                newFrame.add(nameField);
     
     
                JPanel address1 = new JPanel();
                JLabel addressLabel = new JLabel();
                addressLabel.setText("House :");
                JTextField address1Field = new JTextField(15);
                String deliveryAddress1 = address1Field.getText();
     
                address1.add(addressLabel);
                address1.add(address1Field);
                newFrame.add(addressLabel);
                newFrame.add(address1Field);
     
                JPanel street = new JPanel();
                JLabel streetLabel = new JLabel();
                streetLabel.setText("Street :");
                JTextField streetField = new JTextField(15);
                String deliveryAddress2 = streetField.getText();
                street.add(streetLabel);
                street.add(streetField);
                newFrame.add(streetLabel);
                newFrame.add(streetField);
     
                JPanel phone = new JPanel();
                JLabel phoneLabel = new JLabel();
                phoneLabel.setText("Phone :");
                JTextField phoneField = new JTextField(15);
                String phoneNumber = streetField.getText();
                phone.add(phoneLabel);
                phone.add(phoneField);
                newFrame.add(phoneLabel);
                newFrame.add(phoneField);
     
     
     
     
     
                }
               	private class ButtonEventHandler implements ActionListener {
               		String nameField;
     
                   public void actionPerformed(ActionEvent e)
    	 	      {
    			String name1;
    			name1 = nameField.getText();
             if (e.getActionCommand().equals("submit"))
    			{
    				JOptionPane.showMessageDialog(null,"Your name is" + name1,"Message",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else
    				JOptionPane.showMessageDialog(null,"Nothing");
    		  }}


  2. #2
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Having trouble storing variable from JTextField

    Can you maybe tell us at what line the error is located?

    * EDIT *

    If the problem is in the line:

    			String name1;
    			name1 = nameField.getText();

    Are you sure the script can access: nameField
    Looking at your code I don't think your script knows what this "nameField" is.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    Thanks for your reply, this is where I get the error
    name1 = nameField.getText();

    How do I go about getting my script to access the nameField?
    Thanks

  4. #4
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Having trouble storing variable from JTextField

    I just adjusted my post. :p

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    I actually had that bit of code in my original post but I still keep getting the same error.

  6. #6
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Having trouble storing variable from JTextField

    Can you post the error message?
    That usually tells everything.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    Sure:
    cannot find symbol method getText()

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    Make sure the variable definition is in scope (within same pair of {}s) where you are using it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Having trouble storing variable from JTextField

    Does your program also gives out those huge error messages?
    Like, error: cannot find symbol method getText()
    and then below that is writen what line etc etc.

    (I will be back later, need to walk to the train now....)

  10. The Following User Says Thank You to Purple01 For This Useful Post:

    sibs (November 30th, 2012)

  11. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    Thanks for the replies. I have changed my code around a bit.
    Inside in pizzaMenu(), I have put
    deliveryName = nameField.getText();
    Inside in public void actionPerformed(ActionEvent e)
    I have put:
    String name1;
    name1 = deliveryName;
    but when I go to enter my name, its displayed as null.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    Please post the full code (in code tags) so we can see where things are defined.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

     public void deliveryWindow(){
     
                JFrame newFrame = new JFrame("Delivery Details");
     
                Container pane = getContentPane();
                newFrame.setLayout(new FlowLayout());
                newFrame.setSize(new Dimension(250, 250));
                //newFrame.getContentPane().add(panel);
                newFrame.setLocation(300,300);
                newFrame.setSize(250,250);
                newFrame.setVisible(true);
                JButton submitButton = new JButton("submit");
                newFrame.add(submitButton);
                ButtonEventHandler handler = new ButtonEventHandler();
                submitButton.addActionListener(handler);
     
                JPanel name = new JPanel();
                JLabel nameLabel = new JLabel();
                nameLabel.setText("Name :");
                JTextField nameField = new JTextField(15);
                String deliveryName = nameField.getText();
                name.add(nameLabel);
                newFrame.add(nameLabel);
                newFrame.add(nameField);
     
     
                deliveryName = nameField.getText();
     
     
     
                }
               	private class ButtonEventHandler implements ActionListener {
     
     
                   public void actionPerformed(ActionEvent e)
    	 	      {
    			String name1;
    			name1=deliveryName;
     
     
     
             if (e.getActionCommand().equals("submit"))
    			{
    				JOptionPane.showMessageDialog(null,"Your name is" + name1  ,"Message",JOptionPane.INFORMATION_MESSAGE);
    			}
    			else
    				JOptionPane.showMessageDialog(null,"Nothing");
    		  }}

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    Where is the code that shows that the value of the variable is null?

    Why use the variable: name1? Why not use deliveryName?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    That appears on the showMessageDialog screen when I enter the name in the nameField and then click submit on the JButton.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    What happens if you remove the variable: name1 and use deliveryName instead?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    Get an error:
    variable deliveryName might not have been initialized

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    Assign it a value (for example: "") when it is defined.
    If you don't understand my answer, don't ignore it, ask a question.

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

    sibs (November 30th, 2012)

  20. #18
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    Thanks for the help, now the output appears blank instead of null.

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    Try debugging the code by adding a println that prints out the value that returned by the getText() method so you can see what the code is doing.
    Is the code getting the value BEFORE the user enters it? If you see the println's print out before the user enters anything, then that is your problem. You need to wait until the user has entered something BEFORE calling getText().
    If you don't understand my answer, don't ignore it, ask a question.

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

    sibs (November 30th, 2012)

  23. #20
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    Sorry but java is not my strongest area, how would I go about doing that? Where would I place the System.out.println()?

  24. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    Where would I place the System.out.println()?
    Immediately after the variable gets a value assigned to it.

    Assign variable a value
    print value of the variable
    If you don't understand my answer, don't ignore it, ask a question.

  25. #22
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    Still getting the same problem, no value is being returned.

  26. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    Are you printing the value AFTER the user has entered some data?
    Can you see the printed output before the user enters anything?
    If you don't understand my answer, don't ignore it, ask a question.

  27. #24
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble storing variable from JTextField

    Yeah, I've tried it a few different ways and it still wont return any values.

  28. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Having trouble storing variable from JTextField

    Please answer this question:
    Can you see the printed output before the user enters anything?
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Reading XML content and storing in variable
    By learn_java in forum Java SE APIs
    Replies: 1
    Last Post: November 16th, 2012, 04:23 AM
  2. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  3. [SOLVED] Trouble clearing JTextField, getting NullPointerException
    By Lanst83 in forum AWT / Java Swing
    Replies: 11
    Last Post: September 22nd, 2012, 06:37 PM
  4. Replies: 5
    Last Post: November 16th, 2011, 10:22 AM
  5. Assigning an 'int' from a JTextField to a variable...
    By RiskyShenanigan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 26th, 2011, 04:00 PM