0

This is my main method where I start my application. The JFrame loads successfully. When I add the WHILE-Loop part to do some background work where I work with some data to show on my JFrame my JFrame doesn't load correctly (see image below).

public static void main(String[] args) throws IOException  {

        if (Config.checkIfConfigExists() == true) {

            /*
             * Starten der Anwendung
             */
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {

                        Main window = new Main();

                        window.frmServicenowHelper.invalidate();
                        window.frmServicenowHelper.validate();
                        window.frmServicenowHelper.repaint();

                        window.frmServicenowHelper.setVisible(true);

                        while (true) {

                            // the part that makes it error

                        }



                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        } else {

            Notifications.alertMSGConfig("Config not found. Create one?");

        }   
    }

As you can see the JFrame freezes and shows its background.

JFrame loading error

I found out it has something to do with Threads and correct processing (I think I am using something at the wrong point) but I am unable to fix it myself.

Background knowledge:

I want to get a JSON-String from a URL (the methods for that are working - I want to call & show the results on the frame) every 5 minutes (therefore the while-loop).

EDIT:

I tried this which loads the frame correctly but makes the loop (which I need) useless:

while (true) {

                            Main window = new Main();

                            window.frmServicenowHelper.invalidate();
                            window.frmServicenowHelper.validate();
                            window.frmServicenowHelper.repaint();

                            window.frmServicenowHelper.setVisible(true);

                            break;


                        }
piguy
  • 516
  • 3
  • 10
  • 30
  • Are you sleeping for some time before repainting? Are you sure that more than one thread is not operating on JFrame? – Alex Baranowski Feb 27 '17 at 11:20
  • @AlexBaranowski I am not sleeping at any point - I am quite sure its only one Thread. Adding **break;** makes it work but makes the loop useless which I need – piguy Feb 27 '17 at 11:27

1 Answers1

0

I found the solution:

I simply created a new Thread (background processing) using:

public static void main(String[] args) {

     Runnable r = new Runnable() {
         public void run() {
             runYourBackgroundTaskHere();
         }
     };

     new Thread(r).start();
     //this line will execute immediately, not waiting for your task to complete
}

Source: Create threads in java to run in background

Community
  • 1
  • 1
piguy
  • 516
  • 3
  • 10
  • 30