title: Java - best practice for event handling and reflect results back
body: <p>Since I start to programming in Java, every time when I want to create a GUI and a more complex business logic every time I encounter the same problem.</p>
  
  <ol>
  <li>How can you entirely separate the GUI part from the logic part?
  Edited: Maybe is slightly wrong said "GUI part from the logic part". Rather, more correctly is "GUI part from the listeners part" because we don't want files with a huge amount of lines like when we wrote anonymous functions or inner classes.</li>
  </ol>
  
  <p>A result can be that:</p>
  
  <pre><code>public class MainFrame extends JFrame{
      private final Handler handler = new Handler();
      public MainFrame(){
          //some code...
          final JButton b = new JButton("Click Me!");
          b.putClientProperty("command", "some command or a static variable from a some commands class");
          b.addActionListener(this.handler);
      }
  }
  
  // another class in another file 
  public class Handler implements ActionListener{
      @Override
      public void actionPerformed(ActionEvent e){
           if(e.getSource() instanceof JButton){
               final JButton source = (JButton)e.getSource();
               if(source.getClientProperty("command").equals("some command or a static variable from a some commands class")){
                   //do something
               }
           }
      }
  }
  </code></pre>
  
  <p>If some of you know a better way to do that please tell to me.</p>
  
  <ol>
  <li><p>How to make the GUI respond with a refresh when an action is taken from? Because in the above example we wrote:</p>
  
  <pre><code>b.addActionListener(this.handler);
  </code></pre></li>
  </ol>
  
  <p>so only button b knows when the action was performed.</p>
  
  <p>In case that my solution for the first problem is not reliable for the second one please provide a complete solution for this post.</p>
  
  <p>I hope that will be helpful for more people.</p>
  
  <p>Thanks.</p>