14 Replies Latest reply: Dec 21, 2012 7:23 AM by piratolo RSS

    Event Dispatching Thread

    piratolo
      Hi,
      I'm a beginner java student and I'm having problem understanding how to manage the Event Dispatching Thread.

      I read a lot of stuff on the internet; but i didn't find anything written in italian (my language), so I'm afraid i didn't understand well what i read.

      Sure enough, I haven't still understood where to put the run() methos and how to call it.

      I should set the text in a JLABEL after clicking on a JButton; where should i put the run() method with the JLabel.setText("bla bla") instruction? in the actionPerformed() method of my JButton?

      After a lot of attempts i still havent get it. :(

      Thank for yout help.

      Edited by: piratolo on 18-dic-2012 0.54
        • 1. Re: Event Dispatching Thread
          Kayaman
          piratolo wrote:
          I should set the text in a JLABEL after clicking on a JButton; where should i put the run() method with the JLabel.setText("bla bla") instruction? in the actionPerformed() method of my JButton?
          You don't need a run() method. You can set the text in actionPerformed, since you're already on the EDT. It's when you're outside the EDT that you need to use invokeLater().
          • 2. Re: Event Dispatching Thread
            piratolo
            Why am I already on the EDT? Because setText() method is managed automatically by the EDT?
            • 3. Re: Event Dispatching Thread
              gimbal2
              piratolo wrote:
              Why am I already on the EDT? Because setText() method is managed automatically by the EDT?
              No, listeners and events happen on the EDT. So when you're doing things and stuff in actionPerformed() (which is part of a listener) you can be sure its happening on the EDT.
              • 4. Re: Event Dispatching Thread
                piratolo
                Good! This means that I didnt understand anything about edt. :(
                Then what is edt useful for? When have I to use it?
                For instance, when i use the setText() method calling it from another method not in a actionPerformed method?
                thank for helping me

                Edited by: piratolo on 18-dic-2012 9.37
                • 5. Re: Event Dispatching Thread
                  Kayaman
                  piratolo wrote:
                  Then what is edt useful for?
                  It's useful for handling the events and other things related to the Swing GUI.
                  When have I to use it?
                  You need to run (most)code that affects the GUI on the EDT, otherwise you'll experience glitches in the GUI.
                  For instance, when i use the setText() method calling it from another method not in a actionPerformed method?
                  Yes. If you're on another thread and wish to update the GUI (let's say you have a thread that polls the database every minute and displays the contents in a JTable), you need to use the SwingUtilities.invokeLater method to make sure that the updating code is run on the EDT, where it will work correctly.
                  • 6. Re: Event Dispatching Thread
                    jtahlborn
                    Kayaman wrote:
                    piratolo wrote:
                    For instance, when i use the setText() method calling it from another method not in a actionPerformed method?
                    Yes. If you're on another thread and wish to update the GUI (let's say you have a thread that polls the database every minute and displays the contents in a JTable), you need to use the SwingUtilities.invokeLater method to make sure that the updating code is run on the EDT, where it will work correctly.
                    Actually, the question was calling it from another method, not another thread . The answer is, of course, if the other method is also on the EDT, everything is fine. if it's not, then Kayaman's answer about using SwingUtilities.invokeLater applies.
                    • 7. Re: Event Dispatching Thread
                      980759
                      you need to use your code in EDT
                      SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                              MyApplication app = new MyApplication();
                              app.setVisible(true);
                            }
                          });
                      http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeAndWait(java.lang.Runnable)
                      • 8. Re: Event Dispatching Thread
                        piratolo
                        Thank everybody.
                        I wrote a little code, to be sure i get the issue. It's an exsample
                        public static void main(String[] args){     
                             // I shouldn't do this because i'm not in EDT               
                             JLabel.setText("ciao");
                        
                                // i should do this  because i'm not in EDT, so i call the run method below
                             try{ 
                                 SwingUtilities.invokeAndWait(runnable);
                             }
                             catch(Exception er){}     
                        }
                        
                        
                        Runnable runnable = new Runnable(){
                              public void run(){
                             JLabel.setText("write in the JLabel");
                                  }
                             };
                        
                        
                        public class Listener implements ActionListener{
                             public void actionPerformed(ActionEvent e){
                                        // I can do this because i'm in EDT                    
                                  JLabel.setText("write in the JLabel");     
                             }
                        }
                             
                        Is it correct?

                        Edited by: piratolo on 20-dic-2012 6.47
                        • 9. Re: Event Dispatching Thread
                          TPD-Opitz
                          piratolo wrote:
                          Is it correct?
                          basically yes.

                          The point is that in <tt>main</tt> you usually just start the GUI (and the EDT) and do (almost) nothing else.

                          When Swing calls one of your listeners the GUI is blocked until you return from the listeners method. As long as you work is short every thing is fine.

                          But you meight have work to do that requires some time. In this case you should delegeate your work to a new thread (running outside the EDT) so that your listener method can return early. From this other worker thread you need to use <tt>SwingUtilities</tt> to update UI components.

                          bye
                          TPD
                          • 10. Re: Event Dispatching Thread
                            gimbal2
                            Technically yes. But the example isn't really accurate; changes done to the model are generally inside some piece of event code (run on the EDT), such as a button press. You wouldn't often need to run an invokeLater to execute such code.
                            • 11. Re: Event Dispatching Thread
                              piratolo
                              Thank you, all of you are very helpful and kind.

                              Now i'm trying to understand how to handle the situation TPD Opitz-Consulting talked about:
                              >
                              TPD Opitz-Consulting com wrote:
                              But you meight have work to do that requires some time. In this case you should delegeate your work to a new thread (running outside the EDT) so that your listener method can return early. From this other worker thread you need to use SwingUtilities to update UI components.
                              >


                              Should i do something like this?
                              // this is my thread in which i create 100000 JLabel, so it requires some time
                              Thread     t1 = new Thread(){     
                                  public void run(){
                                      SwingUtilities.invokeLater(new Runnable() {
                                       public void run() {
                                           for(int i = 0; i <100000;i++){
                                            JPanel.add(new JLabel("i")); 
                                           }
                                       }
                                     });
                                  }     
                              };
                              // after clicking on a button a call the start() method of my thread
                              public class Listener implements ActionListener{
                                  public void actionPerformed(ActionEvent e){                    
                                      t1.start(); 
                                  }
                              }
                              Edited by: piratolo on 21-dic-2012 1.07
                              • 12. Re: Event Dispatching Thread
                                gimbal2
                                ... No, you don't get it. At this point I don't know what can be said differently to be honest.
                                • 13. Re: Event Dispatching Thread
                                  Kayaman
                                  piratolo wrote:
                                  Should i do something like this?
                                  Not really, that's a very bad example. When would you ever want to add 100000 JLabels anywhere.

                                  He was talking about a situation such as
                                  public void actionPerformed(ActionEvent e) {
                                      loadDataFromDatabase();    // Loads a lot of data, while loading the GUI will be frozen since you're on the event thread
                                  }
                                  In that case you would create another thread to load the data, instead of loading it on the EDT. The EDT is needed if you're making changes to the GUI.
                                  • 14. Re: Event Dispatching Thread
                                    piratolo
                                    Thank everybody, now i get it!
                                    I'm slow-witted :D