Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple way to show quickly-changing values  (Read 370 times)
0 Members and 1 Guest are viewing this topic.
Offline SteveSmith
« Posted 2017-11-13 14:11:06 »

I feel like a bit of a noob asking this; I'm trying to show a few quickly-changing values in a window (just a simple JFrame with a TextArea).  Unfortunately, they change very quickly (i.e. 50 times a second), which cause the text to flicker to the point of being unreadable.  It's only for a bit of debugging, so I was trying to do this simply, but can't seem to find an easy solution.

I've tried directly setting the text in the TextArea, and also using invokeLater(), but neither work well:-

   
1  
2  
3  
4  
5  
SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               textArea.setText(str);
            }
         });


Does anyone have a solution, or a simple way of just showing some quickly-changing debugging output in a window?  Thanks!

Offline h.pernpeintner

JGO Knight


Medals: 62



« Reply #1 - Posted 2017-11-13 14:43:20 »

I'm not too sure about this, but I think you have to use SwingUtilities.invokeAndWait - because otherwise the String str could change be changed by your non-swing thread while the last runnable is still being executed on the EDT.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2017-11-13 15:00:32 »

I'm not too sure about this, but I think you have to use SwingUtilities.invokeAndWait - because otherwise the String str could change be changed by your non-swing thread while the last runnable is still being executed on the EDT.

1  
2  
3  
4  
5  
6  
final String refCopy = str; // just in case 'str' was actually a field, and not a local-var
SwingUtilities.invokeLater(new Runnable() {
   public void run() {
      textArea.setText(refCopy);
   }
});


If you only want to view data, you should use a JLabel, not a JTextArea.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2017-11-13 16:14:58 »

You can use a timestamp and test for elapsed time before updating the JLabel.

If this were more than just a test, I'd consider polling the value from the game loop rather than using every change, unless you need to know each step.

For example, for a 2D area used for a virtual Theremin (y-axis = volume, x-axis = pitch) I poll the mouse position from the game loop rather than allow the mouse movement listener to drive the updates. The mouse is restricted to updating local variables which get checked by the game loop for current value. The synthesizer, upon receiving the new values every 16.6 millis or so (60fps), instead of sporadically and in clusters, employs smoothing based on the known time increment between updates. This works well. But of course it may not fit the requirements of your particular needs.

music and music apps: http://adonax.com
Offline nsigma
« Reply #4 - Posted 2017-11-13 17:33:17 »

One problem with the invokeLater() approach is you really want to coalesce all these values - there's no point in running a Runnable that's setting the text if you already have another Runnable on the queue.

An option might be to pass the strings into a concurrent queue, and drain it within your Runnable, only setting the last available String value (some Runnable will see an empty queue).

Another option, if you know the value is always changing, would be to poll from a Swing Timer.

If you have another thread you want to poll values from that are more complex (or just want to communicate with), it's also worth implementing an equivalent of invokeLater().  You can call with a Runnable that queries values and posts them back to the EDT.  Lock free and asynchronous ftw!

I'd stay away from invokeAndWait() unless you really need it - it's deadlock prone!  Notable that they didn't implement the same in JavaFX.

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 270
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #5 - Posted 2017-11-14 02:18:44 »

Why would using invokeLater() make any difference?

Either set up a function that only changes the text every X times, or change your approach to append the text to the bottom instead of overwriting it completely.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline SteveSmith
« Reply #6 - Posted 2017-11-14 10:51:24 »

Thanks for all the replies.  As suggested by philfrei and KevinWorkMAN, I've decided that since I don't need to see the new values ~50 times a second, I'll just update the text every second or so.  I don't know why i didn't think of that before (as is always the case!).

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (239 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!