home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / awt / radioimagebutton.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  1.8 KB  |  87 lines

  1. package como.awt;
  2.  
  3. import como.util.*;
  4. import java.util.*;
  5. import java.awt.*;
  6.  
  7. public class RadioImageButton extends Panel {
  8.     public RadioImageButton() {
  9.         super();
  10.  
  11.         // this is just the standard layout
  12.         setLayout( new VertLayout() );
  13.     }
  14.  
  15.     public ImageButton add( String con, Image normal, Image current, int width, int height ) {
  16.         ImageButton i = new ImageButton( normal, current, width, height );
  17.  
  18.         // use as a switch!
  19.         i.setSwitch( true );
  20.  
  21.         if( con != null )
  22.             add( con, i );
  23.         else
  24.             add( i );
  25.  
  26.         return i;
  27.     }
  28.  
  29.     public ImageButton add( Image normal, Image current, int width, int height ) {
  30.         return add( null, normal, current, width, height );
  31.     }
  32.  
  33.     public void deselectExcept( int except ) {
  34.         Component comp[] = getComponents();
  35.  
  36.         for( int i = 0; i < comp.length; i++ ) {
  37.             if( i == except ) continue;
  38.  
  39.             ((ImageButton)comp[i]).setState( false );
  40.         }
  41.     }
  42.  
  43.     public void switchButtonOn( int i ) {
  44.         Component comp[] = getComponents();
  45.  
  46.         if( i < comp.length ) {
  47.             deselectExcept( i );
  48.             ((ImageButton)comp[i]).setState( true );
  49.         }
  50.     }
  51.  
  52.     public boolean action( Event evt, Object what ) {
  53.         Component comp[] = getComponents();
  54.  
  55.         for( int i = 0; i < comp.length; i++ ) {
  56.             if( evt.target == comp[i] ) {
  57.                 if( ((ImageButton)comp[i]).getState() == true ) {
  58.                     // means button is down
  59.                     // switch the other button off.
  60.                     deselectExcept( i );
  61.  
  62.                     // now let the others know, that the button
  63.                     // was pressed.
  64.     
  65.                     if( getParent() != null )
  66.                         getParent().postEvent( new Event( this, Event.ACTION_EVENT, new Integer(i) ) );
  67.                     
  68.                     return true;
  69.                 }
  70.                 else {
  71.                     // means button was already on
  72.                     // switch it on again, cause we don't
  73.                     // want deselected buttons!
  74.  
  75.                     ((ImageButton)comp[i]).setState( true );
  76.  
  77.                     // that means nobody else is interested
  78.                     // in this event.
  79.                     return true;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         return false;
  85.     }
  86. }
  87.