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 2 of 2 FirstFirst 12
Results 26 to 33 of 33

Thread: Having trouble storing variable from JTextField

  1. #26
    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, say if it was .println("ff"+deliveryName); it would show "ff"

  2. #27
    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: Having trouble storing variable from JTextField

    Does it print BEFORE the user enters anything in the text field?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Member vigneshwaran's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    35
    My Mood
    Cheerful
    Thanks
    7
    Thanked 1 Time in 1 Post

    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);
     
    name.add(nameLabel);
    newFrame.add(nameLabel);
    newFrame.add(nameField);
     
     
     
     
     
     
    }
    private class ButtonEventHandler implements ActionListener {
     
     
    public void actionPerformed(ActionEvent e)
    {
     
    name1=nameField.getText();
     
     
     
    if (e.getActionCommand().equals("submit"))
    {
    JOptionPane.showMessageDialog(null,"Your name is" + name1 ,"Message",JOptionPane.INFORMATION_MESSAGE);
    }
    else
    JOptionPane.showMessageDialog(null,"Nothing");
    }}


    Now Run this Code.. The Mistake you did is the place where u assigned the value of Text to the String deliveryName.. You should place it in the ActionPerformed Method..

  4. #29
    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: Having trouble storing variable from JTextField

    @vigneshwaran

    Which do you think is a good way to help a beginner learn to solve programming problemns?

    Find his problem and give him the solution. AKA spoonfeeding
    or
    Help the OP find what is causing the problem and work with to help him find the solution

    If the OP is given the solution and not taught how to debug his code, how will he become a better programmer?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Member vigneshwaran's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    35
    My Mood
    Cheerful
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Having trouble storing variable from JTextField

    @ Norm : What's the Mistake i did?

    --- Update ---

    I just corrected the mistake he did. Not Spoonfeeded.

  6. #31
    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: Having trouble storing variable from JTextField

    Sorry, you gave him the answer without helping him debug the code and find what the problem was = spoon feeding.
    If you don't understand my answer, don't ignore it, ask a question.

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

    vigneshwaran (December 1st, 2012)

  8. #32
    Member vigneshwaran's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    35
    My Mood
    Cheerful
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Having trouble storing variable from JTextField

    Oh ok ok.. I'm new to here. I should let him solve. Anyway. Thanks 4 ur reply

  9. #33
    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: Having trouble storing variable from JTextField

    I should let him solve
    What I try to do is to help the OP find the problem and solve it. That takes a few more steps and time.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

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, 05: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, 11: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