3 Replies Latest reply: Jun 20, 2013 3:10 AM by mKorbel RSS

    swing components on EDT

    jan_v
      I have to create a bunch of rather heavy (in processing time) JPanels. In order not to block the EDT during the construction of these panels I was wondering what part of the construction/activation/usage HAS to be done on the EDT and what can be prepared in advance on a side thread?

      For me construction means, the setLayout, setBorder- kind of stuff.
      For me activation means the actual adding to the GUI (f.i. getContentPane.add).
      For me usage means the actual filling with dynamic data (f.i. setText).

      Thank you in advance.
        • 1. Re: swing components on EDT
          mKorbel
          - all updates to already visible AWT/Swing GUI must be on EDT

          - you can to prepare whole GUI out-off EDT, but setVisible and apply LayoutManager must be on EDT, wraped in invokaLater
          • 2. Re: swing components on EDT
            Kleopatra

            Everything must be done on the EDT, see the concurrency chapter in the tutorial:

             

            almost all code that creates or interacts with Swing components must run on the event dispatch thread. [..] Some Swing component methods are labelled "thread safe" in the API specification; these can be safely invoked from any thread. All other Swing component methods must be invoked from the event dispatch thread.

             

            So why exactly is the creation "heavy"? Try to profile and then separate out non-ui stuff (which I wildly assume will turn out as the bottleneck, haven't seen a view with perciptible performance impact in years)

             

            @mKorbel: you know that this "safe-before-realized" is an outdated myth, why do you repeat it?

            • 3. Re: swing components on EDT
              mKorbel

              @: hehehe why, I think than not out_dated, everything is about to simulating CardLayout and wrong way

               

              EDIT:

               

              in Java7 aren't thread safe JComponents or its methods, everything changed, JTextComponents too