CS 235 - Week 10 Lecture - 2015-10-28

*   Threads and Swing
    
    *   Swing is not thread-safe.

        That is: if you try to manipulate
	the user interface from multiple
	threads, then your user interface
	can become corrupted...

    *   in a Swing application,
        your main method runs in the main
	    thread,
	and once the first top-level window
	    is shown (in our case, our JFrame
	    subclass instance),
        a second thread is created,
	called the EVENT DISPATCH THREAD

	...and that's where event
	notifications take place
	(calls to actionPerformed,
	paintComponent, etc.)

    *   how deal with?
        you follow a couple of rules:

        1. if an action takes a long time,
	   do it in a separate worker thread
	   and NOT in the event dispatch
	   thread

           (so it doesn't cause the
	   interface to be "laggy"
	   or to seem "dead"...)

        2. Do not touch Swing components
	   in any thread other than
	   the event dispatch thread.

           ^ called "the single-thread
	     rule for Swing programming"

           *   that is,
	       (usually) read info from
	       the user interface
	       before launching the
               thread

               then launch the thread

	       and when the thread is
	       done, update the user
	       interface from the
	       event dispatch thread

	  (...and the above is safest!

	  and there are some exceptions to the above;

          *   there are a few "thread-safe"
	      Swing methods:

	      some of the most useful:

	      JTextComponent.setText
              JTextArea.insert
	      JTextArea.append
	      JTextArea.replaceRange
              JComponent.repaint
              JComponent.revalidate

           *   so, yes, you can call repaint,
	       revalidate from other threads;

           *   (and there are some other
	       exceptions as well, see Core Java
	       Section 14.11)

      *   in the "worst" case,
          the separate thread CAN request that something
	  be done by adding it to the event queue --

	  using methods invokeLater
	                invokeAndWait

*   SEE CHAPTER 14 for more on these topics...

NOW for something somewhat different...
*   how about some more components?

    JCheckBox - can be checked or unchecked
    *   it has different states, then;

    *   can be used with ActionListener instances
        OR ItemListener instances
	OR with no listeners (and queried as to its
	state -- checked or unchecked --
	at a given time)

    *   commonly used for select-0-or-more
        of a set of options

    *   one of many JCheckBox methods:
        isSelected() - returns a boolean to indicate
	   if the calling JCheckBox is currently
	   checked/selected or not

*   JRadioButton
    *   use a radio button (RECOMMENDED STYLE!!!!!!!!!!!)
        for selecting exactly one of not-too-many options

    *   (choosing one in a logical group causes
        whichever other is chosen to be UNchosen)

    *   to make these as they should,
        you create a ButtonGroup,
	and add JRadioButton instances TO that ButtonGroup

        (don't forget to add the radio button to the
	layout also!)