You should run your code in the event dispatch thread.
As you can read in Oracle documentation: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
You can use Swing classes outside EDT only if they are marked as thread safe in the documentation.