• Main Page
  • Related Pages
  • Packages
  • Classes
  • Files
  • File List

ui/JImageButton.java

Go to the documentation of this file.
00001 
00002 package ui;
00003 
00004 import java.awt.Color;
00005 import java.awt.event.FocusEvent;
00006 import java.awt.event.FocusListener;
00007 import java.net.URL;
00008 
00009 import javax.swing.ImageIcon;
00010 import javax.swing.JButton;
00011 
00012 /**
00013  *  JButton with transparent images and no borders.
00014  *
00015  *  @author Mikica B Kocic
00016  */
00017 public class JImageButton extends JButton implements FocusListener
00018 {
00019     private static final long serialVersionUID = 2446005873909591693L;
00020 
00021     private ImageIcon normalIcon;
00022     private ImageIcon inFocusIcon;
00023     
00024     /**
00025      *  Creates button with the tool tip and two icons (one normal and one rollover)
00026      */
00027     public JImageButton( Object resourceOwner, 
00028             String tooltip, String iconPath, String rolloverIconPath )
00029     {
00030         setToolTipText( tooltip );
00031 
00032         /* Set icons
00033          */
00034         normalIcon = loadIcon( resourceOwner, iconPath );
00035         inFocusIcon = loadIcon( resourceOwner, rolloverIconPath );
00036         
00037         setIcon( normalIcon );
00038         setRolloverIcon( inFocusIcon );
00039         
00040         /* Make button background transparent and without borders
00041          */
00042         setBorderPainted( false );
00043         setOpaque( false );
00044         setBackground( new Color(0,0,0,0) );
00045         
00046         addFocusListener( this );
00047     }
00048 
00049     /**
00050      *  Loads icon from resources or file system
00051      */
00052     public static ImageIcon loadIcon( Object resourceOwner, String name )
00053     {
00054         String path = "resources/images/" + name;
00055         
00056         /* First try from class resources
00057          */
00058         URL url = resourceOwner.getClass().getResource( path );
00059         
00060         if ( url != null ) {
00061             return new ImageIcon( url );
00062         }
00063         
00064         /* then fall back to file system...
00065          */
00066         return new ImageIcon( path );
00067     }
00068 
00069     
00070     /**
00071      *  On focus gained, sets icon to 'in focus' icon
00072      */
00073     @Override
00074     public void focusGained( FocusEvent evt ) 
00075     {
00076         setIcon( inFocusIcon );
00077         setBackground( null );
00078     }
00079 
00080     /**
00081      *  On focus lost, resets icon to normal
00082      */
00083     @Override
00084     public void focusLost( FocusEvent evt ) 
00085     {
00086         setIcon( normalIcon );
00087         setBackground( new Color(0,0,0,0) );
00088     }
00089 }

Generated on Thu Dec 16 2010 14:44:42 for VoIP Kryptofon by  doxygen 1.7.2