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

Thread: Java GUI Problem, I am having trouble getting my TextArea to resemble console output.

  1. #26
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI Problem, I am having trouble getting my TextArea to resemble console output.

    .

    --- Update ---

    I would like to put a delay in my code but for loop conflicts with my timer.
    timer = new Timer(300, new ActionListener()
            {
                public void actionPerformed(ActionEvent e) 
                {
     
                	//textArea.setText("");     
                     for (int i = 1; i < 60; i++)
                     {               	
                     	String format = "%-10s";
                    	String someline;
                     	someline = String.format(format, new Object[]{"Number" + "      " + (i) + " : " + Growth_numbers1[i] + "      " + "repeats"} );      
                     	textArea.append(someline + "\n");
                     }
     
     
                }
            });
    Last edited by New_Guy25; October 9th, 2014 at 01:05 PM. Reason: duplicate

  2. #27
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java GUI Problem, I am having trouble getting my TextArea to resemble console output.

    Explain the conflict. Code comments would be helpful to others reading your code (and to you) so that they can understand what the code fragment you posted is supposed to do.

  3. #28
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI Problem, I am having trouble getting my TextArea to resemble console output.

    The conflict is between my timer delay and my for loop. When I take this statment --> textArea.append(someline + "\n"); <--- out of the for loop and place it outside the brackets. My delay timer seems to work ok, but then my array does not get printed out the way I wanted it to be printed out. If I leave this statement in the for loop then all my content gets printed out and it keeps looping and looping and there is no delay except a loop delay that loops and loops.

    So the current error I have is this... Everything gets printed out at once and then it loops again.
    Number 1 : 6 repeats
    Number 2 : 10 repeats
    Number 3 : 4 repeats
    Number 4 : 4 repeats
    Number 5 : 7 repeats
    Number 6 : 2 repeats
    Number 7 : 4 repeats
    Number 8 : 6 repeats
    Number 9 : 4 repeats
    Number 10 : 5 repeats
    Number 1 : 6 repeats <===looped again here
    Number 2 : 10 repeats
    Number 3 : 4 repeats
    Number 4 : 4 repeats
    Number 5 : 7 repeats
    Number 6 : 2 repeats
    Number 7 : 4 repeats
    Number 8 : 6 repeats
    Number 9 : 4 repeats
    Number 10 : 5 repeats
    Number 1 : 6 repeats <===looped again here
    Number 2 : 10 repeats
    Number 3 : 4 repeats
    Number 4 : 4 repeats
    Number 5 : 7 repeats
    Number 6 : 2 repeats
    Number 7 : 4 repeats
    Number 8 : 6 repeats
    Number 9 : 4 repeats
    Number 10 : 5 repeats
    <===looping process continues
    -------------------------------------------------------------------------------------------
    This is what I actually want to do with my code and not have it loop:

    Number 1 : 6 repeats
    delay 100 miliseconds
    Number 2 : 10 repeats
    delay 100 miliseconds
    Number 3 : 4 repeats
    delay 100 miliseconds
    Number 4 : 4 repeats
    delay 100 miliseconds
    Number 5 : 7 repeats
    delay 100 miliseconds
    Number 6 : 2 repeats
    delay 100 miliseconds
    Number 7 : 4 repeats
    delay 100 miliseconds
    Number 8 : 6 repeats
    delay 100 miliseconds
    Number 9 : 4 repeats
    delay 100 miliseconds
    Number 10 : 5 repeats
    etc
    etc
    etc

    So the problem lies between my for loop and the timer delay. How can I go about fixing this?

    Thanks

  4. #29
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java GUI Problem, I am having trouble getting my TextArea to resemble console output.

    It's hurting my head. You have a loop inside a timer's ActionListener.actionPerformed() method that will execute every time the timer lapses. Each time the actionPerformed() method executes, the for() loop starts over. That's what you've programmed it to do, but now you're complaining that that's not what you want. I don't understand what you want it to do, but I'm not sure what you want needs a for() loop. It looks like all you need is a counter from 1 to whatever that increments each time the actionPerformed() method executes, and I don't understand where 'x repeats' and the 'delay x ms' comes from, but those should be possible without the for() loop.

  5. #30
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI Problem, I am having trouble getting my TextArea to resemble console output.

            timer = new Timer(300, new ActionListener()
            {
                public void actionPerformed(ActionEvent e) 
                {
     
                	//textArea.setText("");     
                     //for (int i = 1; i < 73; i++)
                       int count = 0;
                       if (count <= 73)
                       {
                    	count = count + 1;
                    	String format = "%-10s";
                    	String someline;
                     	someline = String.format(format, new Object[]{"Number" + "      " + (count) + " : " + Growth_numbers1[count] + "      " + "repeats"} );      
                     	textArea.append(someline + "\n");
                       }
     
     
     
                }
            });


    ok, you were right about the counter, but it only prints the first element in the Array. How can I get it to print out the rest?

  6. #31
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java GUI Problem, I am having trouble getting my TextArea to resemble console output.

    Of course.
    Each time your timer is activated your counter is set to 0. So you will obviously always print the first element.

    You have to keep your counter variable as a field outside of the timer.

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

    GregBrannon (October 9th, 2014)

  8. #32
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java GUI Problem, I am having trouble getting my TextArea to resemble console output.

    You rock, that was awesome.... Thanks

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How to clear Java output console
    By rita khatei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2014, 03:42 PM
  2. Transferring console output to a textArea....how to make this happen?
    By jakhondi21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2013, 04:03 PM
  3. Problem diplaying output infor in GUI
    By jdubicki in forum AWT / Java Swing
    Replies: 3
    Last Post: December 15th, 2011, 08:55 AM
  4. From Console to GUI : problem !!
    By hexwind in forum AWT / Java Swing
    Replies: 33
    Last Post: August 20th, 2011, 10:50 PM
  5. Trouble apending a textArea with the contents of a parameter
    By bluetxxth in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 04:28 PM