Results 1 to 10 of 10
  1. #1
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default Cant refresh JFrame, jTextArea1

    Hello experts

    I created a java program using netbeans (palette), the output is displayed in JTextArea,

    Part of the code

    Java Code:
    for ( i = 0; i < cArray.length; i++) {
                if (cArray[i].contains("96279")) {
                    Command = CommandPart1 + cArray[i] + CommandPart2;
                    String run = thr.run(Command);
                    System.out.println(cArray[i] + ": " + run);
                     jTextArea1.append(cArray[i] +": "+ run + "\n");
    
                    jTextArea1.revalidate();
                    this.repaint() ;
                              }
            }
    The output in jTextArea1 is displayed after the code is excuted and not as per the code in which it appends the data at each line of the for loop
    , in the System.out, it's displayed line by line with no problem ?

    I tried to use repaint, validate, revalidate

    Can any body help

  2. #2
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    after looking around i found this line of code

    jTextArea.update(jTextArea.getGraphics());

    that seemed to fix similar problems for some. If that doesn't work i read that multi-threading may be needed. Do a search fro refresh jtextarea. Hope this helps

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    If you want help, you'll have to provide an SSCCE that demonstrates what you're actually doing. For example, is this code running on the EDT? In another Thread?

    I would bet you're doing this on the EDT, which is causing all of the repaint() calls to be pushed to the end. You might want to look at the paintImmediately() method, or you might want to move logic that takes a long time off of the EDT.

    Edit- And no, using the update method is probably not the way to go. If you're calling the getGraphics() method of a Component, you're doing something very wrong.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default

    I have a suspicion that this is a Swing threading issue, and that you have a Thread.sleep or while (true) lurking in code not shown us. If so, use a Swing Timer instead. If not, then as Kevin wisely suggests, create and show us an SSCCE. I also agree that you should ignore Debugger's advice. While he means well, it's off the mark.

  5. #5
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default

    Thanks a lot debugger and all..

    it's working fine now.. I just added jTextArea.update(jTextArea.getGraphics());

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

    Default

    Quote Originally Posted by yahyaaa View Post
    Thanks a lot debugger and all..

    it's working fine now.. I just added jTextArea.update(jTextArea.getGraphics());
    Yep. That answer is concise, pretty, and just plain wrong. I strongly urge you not to do this.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Quote Originally Posted by Fubarable View Post
    Yep. That answer is concise, pretty, and just plain wrong. I strongly urge you not to do this.
    Don't worry, we'll see him in a couple of weeks when he's confused about why all his Components "aren't painting right".
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Was not meant to confuse him, just a though of what may fix it, if he/she chooses to look into it more is up to them, which is why i also mentioned the threading issue, which has been the case for me before.

    As i said before simply trying to help, but i'm obviously not as advanced as some so i would look into what they are saying,

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Quote Originally Posted by Debugger View Post
    Was not meant to confuse him, just a though of what may fix it, if he/she chooses to look into it more is up to them, which is why i also mentioned the threading issue, which has been the case for me before.

    As i said before simply trying to help, but i'm obviously not as advanced as some so i would look into what they are saying,
    We (I) don't mind less experienced users trying to help - heck, I don't really consider myself as "advanced" as most of the veterans here either. But coding Swing correctly takes practice, and it takes time to understand some of the more common problems- two of which you had in your reply.

    Using getGraphics() on a Component is almost never ever a good idea. It might seem to fix one problem, but I guarantee it's going to cause other, less fixable problems down the road.

    And telling the OP to use multi-threading is opening up a dangerous can of worms and is probably more misleading than it is informative. Sure, I can see one way of using it to fix this particular problem, especially because the append method is one of the few cases of a thread-safe method in Swing, but that doesn't mean that it's the best way to go. I'd say using a Swing Timer is probably more what the OP is looking for. Until you understand the EDT and all of its magic, you almost definitely should not be mixing threading and Swing.

    I'm not trying to be mean or discourage you from helping, but this is a good example of when the first thing to pop into your head isn't actually the correct way to go. But the OP will probably continue to follow your advice, and it will eventually give him bigger problems.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    well then i guess he will learn his lesson and not listen to me =p i understand what your saying though which is why im open to criticism. I knew what i meant in my head, but did not portray it correctly to them, should have been more specific.

    If they were a good programmer, they would read more into than just my post, and wish to understand WHY that fixes it, and would like i now do realize that it fixes that issue but will result in more.

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  3. JFrame refresh problem
    By elliot in forum AWT / Swing
    Replies: 5
    Last Post: 02-03-2010, 11:18 PM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 PM

Posting Permissions

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