2

I was running some code that did a printout in a swingworker. I was not getting printouts so I used SwingUtilities.invokeLater and now it works. I did not expect this result, how did this happen? I would have thought System.out.println could run outside of the EDT.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
  • 1
    Something else is wrong. System.out.println(...) does not care what thread it's called in and only *Swing* method calls need be called on the Swing event thread. – Hovercraft Full Of Eels Jun 28 '12 at 20:48
  • 2
    nooo System.out.println... that is the _root of all evil_ and also ugly as hell. You can choose between at least 3 easy to setup, lightweight logging frameworks in java. (slf4j, log4j, logback, etc) – Gergely Szilagyi Jun 28 '12 at 20:52
  • 1
    You won't get any better help without posting code. – Marko Topolnik Jun 28 '12 at 21:12

4 Answers4

2

That would have been pretty easy to test (not to say, even typing all the code to test this is less work then posting it here):

import java.awt.EventQueue;
public class HelloWorld {
  public static void main( String[] args ) {
    System.out.println("Hello world");
    System.out.println( EventQueue.isDispatchThread());
  }
}

results in

Hello world
false

on the console.

So yes, System.out.println can be used outside the EDT

Robin
  • 36,233
  • 5
  • 47
  • 99
1

I would have thoughtsystem.out.println could run outside of the edt.

That is true. To test this, create a thread where you put a loop and a printout and see for yourself :)

GETah
  • 20,922
  • 7
  • 61
  • 103
0

System.out.println does run outside the edt. You made it run inside when you ran it using a swingworker. In theory you should always be able to just print your results right from your code block. I suggest:

Runnable runnable = new Runnable() {

        public void run() {

        }
    };
SwingUtilities.invokeLater(runnable);
Mitch Connor
  • 766
  • 10
  • 19
0

Although it can, it has very few applications outside of debugging.

an example for an alternate output would be JOptionPane:

JOptionPane.showMessageDialog(frame/* sets up the message, can also be replaced with null to remove formatting*/,
"Eggs are not supposed to be green."/* this is your main message*/,
"A plain message"/* this is what shows in the title spot (first parameter must not be null)*/,
JOptionPane.PLAIN_MESSAGE/*shows no icon, also replacable with WARNING_MESSAGE, ERROR_MESSAGE, INFORMATION_MESSAGE*/);

all that would be on one line, but I broke it up for formatting here one line version:

JOptionPane.showMessageDialog(frame/* sets up the message, can also be replaced with null to remove formatting*/, "Eggs are not supposed to be green."/* this is your main message*/, "A plain message"/* this is what shows in the title spot (first parameter must not be null)*/, JOptionPane.PLAIN_MESSAGE/*shows no icon, also replacable with WARNING_MESSAGE, ERROR_MESSAGE, INFORMATION_MESSAGE*/);

without comments:

JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.", "A plain message", JOptionPane.PLAIN_MESSAGE);

It's fun to troll your teachers with Dr. Seuss quotes in your programs.

Azulflame
  • 1,534
  • 2
  • 15
  • 30