-3

First of all, I know there are multiple questions out there on this topic, but none of them answers what I want to know.

During going through some questions, I came across a advice that we should always use EDT to update our GUI.

My questions are,

  1. When should I use it? One example please.
  2. What will happen if I don't use it? One example please. Code not required.
  • 3
    **(1)** It's the thread responsible for handling all state related to the UI and handling user generated events (e.g. mouse clicks, key presses, etc.). **(2)** It does as described in point one. **(3)** You should use it whenever you're manipulating the UI. **(4)** An exception might be thrown or you can run into undefined behavior (typical improperly-synchronized-multithreaded-code issues). **(5)** If you're on a background thread you can use `SwingUtilities.invokeLater` to schedule an action on the EDT. If you're in an event handler you should already be on the EDT. – Slaw Jul 24 '20 at 02:36
  • 3
    Read the tutorial on [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) which should answer all your questions. – camickr Jul 24 '20 at 02:36
  • I have created many GUI programs in which I remove and add components from the main panel. Is this the part where I am supposed to use EDT? Because, I have not used it and I have not encountered any problem either. – Anrew Fred Jul 24 '20 at 02:40
  • You seem to need a [Event Dispatching Thread tutorial](https://www.google.com/search?safe=off&client=opera&hs=aBr&sxsrf=ALeKk019SwXr_DYtGiNHtA65cxWdqxtYNg%3A1595558897481&ei=8UsaX8qLHfbG4-EPyv2k-AQ&q=Event+Dispatching+Thread+tutorial&oq=Event+Dispatching+Thread+tutorial&gs_lcp=CgZwc3ktYWIQAzIICCEQFhAdEB46BAgAEEc6AggAOgYIABAWEB46BAghEApQhxVY5Spg6DBoAHABeACAAbECiAH_D5IBBzAuMi42LjGYAQCgAQGqAQdnd3Mtd2l6wAEB&sclient=psy-ab&ved=0ahUKEwiKyLWK8OTqAhV24zgGHco-CU8Q4dUDCAs&uact=5). SO is a Q&A site, not a place to solicit (personalized) tutorials. Voted to close as 'needs focus'. – Andrew Thompson Jul 24 '20 at 02:49
  • @AndrewThompson I am not explicitly asking for a tutorial. I am just trying to reach people who are already informed on this topic and get a simple explanation to start from. The official motto of stackoverflow is **_Where Developers Learn, Share, & Build Careers_**. So I suppose I am at the right place to ask this question. – Anrew Fred Jul 24 '20 at 02:59
  • *I am not explicitly asking for a tutorial.* - but you have already been given a link to the standard Swing tutorial. Read the tutorial!!! Then if you have something specific about the tutorial you ask a specific question stating the reference in the tutorial you don't understand. Why to you think a one or two sentence answer in these comments can provide more information that a tutorial? – camickr Jul 24 '20 at 03:02
  • @camickr That's what I wanted. Something to start from or any additional help if anyone is **willing** to provide (just like Slaw and a.deshpande012 did). – Anrew Fred Jul 24 '20 at 03:04
  • *That's what I wanted.* - yes, but you haven't read it and you keep asking questions that are covered in the tutorial. The idea of a tutorial is to read first and ask later. – camickr Jul 24 '20 at 03:08
  • 2
    @camickr In case you presumed that I didn't read it, **I did read it**. But, I couldn't understand it totally. That's why I asked this particular question, **"Does updating any modification done to the GUI under EDT enough to prevent any mis-behaviour?"** – Anrew Fred Jul 24 '20 at 03:11
  • I did help I gave you a link to the tutorial. Not only that the Swing tutorial covers all Swing basics, so you have a reference to read before asking further questions. Not once in your question did you reference any information from the tutorial indicating that you read anything from the tutorial. If we know the section your find confusing then we can attempt to clarify the wording from the tutorial rather than just repeat the wording in the tutorial. – camickr Jul 24 '20 at 03:24
  • @camickr I don't suppose anyone need any reference to answer this question, **_"Does updating any modification done to the GUI under EDT enough to prevent any mis-behaviour?"_**. Simple proof of that is, Slaw already helped me by answering this question without needing any reference. – Anrew Fred Jul 24 '20 at 03:26
  • These are not five separate questions but two. The answers to 1-2 are the same, as are the answers to 3-5. – user207421 Jul 24 '20 at 04:14

1 Answers1

1
  1. The Event Dispatching Thread is the thread where all Swing-related work should happen. As you've seen, you can sometimes get away with not explicitly using it to update your UI, although this is considered bad form.

  2. The EDT is responsible for your Swing UI. It handles mouse clicks, events, callbacks, window drawing, etc. Every time an ActionListener is called, it's called by the EDT.

  3. Every time you register a listener with Swing, you're using the EDT, even if you don't explicitly mention it.

    For example:

    JButton button = new JButton();
    button.addActionListener(e -> System.out.println("Clicked!"));
    

    Even though you don't mention the EDT, you're still using it. The listener is called from the EDT when the button is clicked.

    Another way to use the EDT is with SwingUtilities.invokeLater(). You would use this when you need to update the UI from a thread that's not the EDT. Swing is thread unsafe, which means that it's not safe to update the UI from outside the EDT. Even if things work on your computer, it's not guaranteed to always work. You can read a little more on a relevant question here.

  4. As I've said, not using the EDT is unsafe, since Swing makes no guarantees about thread safety.

  5. If you use Swing, you're always using the EDT. But you can schedule tasks to run on the EDT with methods like invokeLater() and invokeAndWait().

Slaw
  • 37,820
  • 8
  • 53
  • 80
a.deshpande012
  • 715
  • 7
  • 18