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

Thread: How to synchronize JTextField with JFrame and other methods

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to synchronize JTextField with JFrame and other methods

    I want to creat a GUI with buttons so that each of them will activate a textfield. and in the textfield I want to type info and press enter so that the program will receive it and the textfield will keep staying so that I would be able to type more and press enters to input more info to the program. And as soon as I'm done I would type something like "done" and then the program will continue to execute the next lines

    this is an example for one button so far.

    This is my code:

    public class runtime extends JFrame
    {
        setLayout(new FlowLayout());
        button1 = new JButton("press me");
        add(button1);
        ActionHandle userClick = new ActionHandle();//create object from the action handling class
        button1.addActionListener(userClick);
    }
    .

    private class ActionHandle implements ActionListener
    {
        if(click.getSource() == button1)
        {
                method1();
        }
        else     //if haven't pressed any button then must've entered something in the text field
        {
            String input = new String();
            input = textField1.getText();
            System.out.println(input);
        }
    }
    .

    private void method1()
    {
        JTextField text1 = new JTextField(20);          //{
        add(text1);                                     
        ActionHandle userClick = new ActionHandle();
        text1.addActionListener(userClick);             //  create the textfield line
        setLayout(new FlowLayout());                   
        this.setVisible(true);                         // }
     
        String userInput= "";     //here I will put all the info received from user
     
        while(!userInput.equals("done"))   //as long as user didn't type "done"
        {
            userInput = userInput + _________    // I don't know what to write here
        }
    }
    The whole code here is in one class, and I activate it using a different class with main.

    My problem is that the textfield doesn't wait for me to write anything it just continue to execute the rest of the code (it goes to the while loop).

    Please tell me what I need to do in order to receive such info, then use it, and continue receiving info until I type "done". Just remember that I need to do the same for at least 13 other buttons. I don't want to use normal JOptionPane I need the JTextField.

    If you edit my code just delete all my commets from it and add your own on your edit. Thank you guys
    Last edited by Davis8988; April 23rd, 2014 at 09:39 AM.


  2. #2
    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: How to synchronize JTextField with JFrame and other methods

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    GUI programming is event driven. The use of a while() loop to control a GUI's execution is almost always incorrect. Refer to the tutorial "How to Write an Action Listener." Study that and attempt to add that technique to your code, and come back if you need help or more questions.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to synchronize JTextField with JFrame and other methods

    Thank you,
    I know I'm new and it is very difficult but that's why I asked how to do it with a loop. I really want to know the correct way to do it using a loop.

  4. #4
    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: How to synchronize JTextField with JFrame and other methods

    No, Java GUI programming is not very difficult to do correctly. I suggest you learn how to do it correctly by following the Java Swing Tutorials. Ignore Oracle's bias to use the Netbeans GUI Builder (Netbeans by itself is fine).

    There is no correct way to do what you've tried to do with a loop. In fact, when you learn more about Swing and how it works, you'll see that the architecture provides a loop - in a way - that is waiting for the user to provide an input or generate an event to which the application will respond. What you're trying to do with a loop is already there. Your way will inhibit the proper operation of the event handling system already provided by Swing.

Similar Threads

  1. JFrame + JTextField Question
    By A b in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 2nd, 2014, 06:22 AM
  2. JFrame + JTextField and Random Number Generator
    By Richard_Harrow in forum AWT / Java Swing
    Replies: 7
    Last Post: December 8th, 2013, 05:30 PM
  3. How to repaint() a class in a JFrame, with a JTextfield user input
    By DANGEROUSSCION in forum Object Oriented Programming
    Replies: 1
    Last Post: March 23rd, 2013, 03:06 PM
  4. Parallel vectors synchronize
    By speaker in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 18th, 2012, 03:27 PM
  5. When to Synchronize Code
    By Bijaysadhok in forum Threads
    Replies: 12
    Last Post: March 16th, 2012, 09:28 AM

Tags for this Thread