pashapm / lanterna

Automatically exported from code.google.com/p/lanterna
GNU Lesser General Public License v3.0
0 stars 0 forks source link

TextArea does not update with invalidate(). #70

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello.

I've got a TextArea that's supposed to update every time a serial communication 
is received (e.g. from a serial cable). So far, I've tried a lot of things. 
I'll try summing up the problem:

* If you update the TextArea with textArea.insertLine(0, "text") from an 
external method implemented in my Window subclass (called signature is 'private 
void appendText(String m)'), the line is inserted (tested with 
System.out.println(textArea.getLine(0))) but not redrawn on the TextArea itself.

* If you then try to update the TextArea by calling getOwner().invalidate() 
nothing happens. I've made some modifications in a custom version of Lanterna, 
making repaint() and update() public methods and tried calling them - nothing. 
All three of these methods have been called as both "out of event thread" and 
in event thread, e.g.:

getOwner().runInEventThread(new Action() {
            @Override
            public void doAction() {
                appendText("Hello");
                getOwner().invalidate();
            }
        });

* Now, this is where it gets weird... If I implement a button in my window and 
associate it with a doAction() method that calls just the appendText("Hello") 
it runs fine and updates accordingly. 

I've done a lot of debugging and I cannot for the life of me figure out why the 
button has the ability to update my TextArea with my method, but other methods 
in my class cannot. I've tried a lot of things, but none of them work.

Am I on the wrong track about the functionality / usage here? Isn't the outcome 
supposed to be the same?

Sorry for the wall of text, I've never opened an issue ticket for a framework 
before.

Original issue reported on code.google.com by tobias.s...@gmail.com on 11 Mar 2013 at 7:26

GoogleCodeExporter commented 9 years ago
Ah, turns out I had the wrong reference to my Window (and this my TextArea). In 
order to fix this issue, I called this:

getOwner().runInEventThread(new Action() {
            @Override
            public void doAction() {
                TextArea act = (TextArea) getOwner().getActiveWindow().getComponentAt(2);  //obviously this index varies, but I just counted it from which order I called addComponent() 
                textArea.insertLine(0, message);
                getOwner().invalidate();
            }
        });

This gave me a correct reference to my TextArea. Anyway, the issue is resolved 
now, please close this and file it under "noobie mistake"! Thanks for the great 
framework :-)

Original comment by tobias.s...@gmail.com on 11 Mar 2013 at 8:33

GoogleCodeExporter commented 9 years ago
Okay... So you called invalidate() on the wrong Window? Or appendText("...") on 
the wrong object?

Original comment by mab...@gmail.com on 20 Mar 2013 at 1:23

GoogleCodeExporter commented 9 years ago
Yeah exactly. My doAction in my button had a reference different from the one I 
got when I called my own method. Obviously since doAction was able to update 
the window and my method wasn't, I figured I somehow had to get the reference 
to the window by other means that directly addressing it (I was using "this." 
to refence to my window). 

Long story short: I checked up on the ID's using the NetBeans debugger and 
checked for consistency errors. And yes, as you said, I was calling 
invalidate() on a wrong window - which to me (at first) didn't make sense, 
because I was using "this.invalidate()". But it's just one of those things that 
eludes new players like myself - reference consistency. 

Sorrry about the wall of text. I'm struggling to understand the issue at hand 
here and because of that, I'm not really sure how much information is relevant 
to the case. 

Thank you!

Original comment by tobias.s...@gmail.com on 20 Mar 2013 at 2:35

GoogleCodeExporter commented 9 years ago
All right, good to hear it worked out in the end.

Original comment by mab...@gmail.com on 20 Apr 2013 at 8:47