Advertisement
mvaganov

Custome Java button UI

May 27th, 2015
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.Insets;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7.  
  8. import javax.swing.AbstractButton;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JButton;
  11. import javax.swing.JComponent;
  12. import javax.swing.SwingConstants;
  13. import javax.swing.plaf.basic.BasicButtonUI;
  14.  
  15. /** http://codelife.de/2014/08/13/swing-jbutton-with-customized-look/ */
  16. public class CustomizedButtonUI extends BasicButtonUI implements
  17.         MouseListener {
  18.     private Color hoverColor = new Color(128, 240, 240);
  19.     private Color normalColor = new Color(240, 128, 240);
  20.     private Color pressedColor = new Color(240, 240, 128);
  21.     private Color btnFontColor;
  22.     private ImageIcon normalIcon;
  23.     private ImageIcon hoverIcon;
  24.     private ImageIcon pressedIcon;
  25.     private Font btnFont;
  26.  
  27.     public CustomizedButtonUI() {
  28.         super();
  29.     }
  30.  
  31.     public CustomizedButtonUI(Color normalColor, Color hoverColor,
  32.             Color pressedColor) {
  33.         this(normalColor, hoverColor, pressedColor, null, Color.BLACK);
  34.     }
  35.  
  36.     public CustomizedButtonUI(Color normalColor, Color hoverColor,
  37.             Color pressedColor, Font btnFont, Color btnFontColor) {
  38.         this.normalColor = normalColor;
  39.         this.hoverColor = hoverColor;
  40.         this.pressedColor = pressedColor;
  41.         this.btnFont = btnFont;
  42.         this.btnFontColor = btnFontColor;
  43.     }
  44.  
  45.     public CustomizedButtonUI(ImageIcon normalIcon, ImageIcon hoverIcon,
  46.             ImageIcon pressedIcon) {
  47.         this(normalIcon, hoverIcon, pressedIcon, null, Color.BLACK);
  48.     }
  49.  
  50.     public CustomizedButtonUI(ImageIcon normalIcon, ImageIcon hoverIcon,
  51.             ImageIcon pressedIcon, Font btnFont, Color btnFontColor) {
  52.         this.normalIcon = normalIcon;
  53.         this.hoverIcon = hoverIcon;
  54.         this.pressedIcon = pressedIcon;
  55.         this.btnFont = btnFont;
  56.         this.btnFontColor = btnFontColor;
  57.     }
  58.  
  59.     @Override
  60.     public void installUI(JComponent comp) {
  61.         super.installUI(comp);
  62.         comp.addMouseListener(this);
  63.     }
  64.  
  65.     @Override
  66.     public void uninstallUI(JComponent comp) {
  67.         super.uninstallUI(comp);
  68.         comp.removeMouseListener(this);
  69.     }
  70.  
  71.     @Override
  72.     protected void installDefaults(AbstractButton btn) {
  73.         super.installDefaults(btn);
  74.         btn.setIcon(this.normalIcon);
  75.         btn.setBackground(this.normalColor);
  76.         btn.setForeground(this.btnFontColor);
  77.         btn.setFont(this.btnFont);
  78.         btn.setHorizontalTextPosition(SwingConstants.CENTER);
  79.     }
  80.  
  81.     @Override
  82.     public Dimension getPreferredSize(JComponent comp) {
  83.         Dimension dim = super.getPreferredSize(comp);
  84.         if (comp.getBorder() != null) {
  85.             Insets insets = comp.getBorder().getBorderInsets(comp);
  86.             dim.setSize(dim.width + insets.left + insets.right, dim.height
  87.                     + insets.top + insets.bottom);
  88.         }
  89.         return dim;
  90.     }
  91.  
  92.     @Override
  93.     public void mouseClicked(MouseEvent e) {
  94.         changeButtonStyle((JButton) e.getComponent(), this.pressedColor,
  95.                 this.pressedIcon);
  96.     }
  97.  
  98.     @Override
  99.     public void mousePressed(MouseEvent e) {
  100.         changeButtonStyle((JButton) e.getComponent(), this.pressedColor,
  101.                 this.pressedIcon);
  102.     }
  103.  
  104.     @Override
  105.     public void mouseReleased(MouseEvent e) {
  106.         changeButtonStyle((JButton) e.getComponent(), this.normalColor,
  107.                 this.normalIcon);
  108.     }
  109.  
  110.     @Override
  111.     public void mouseEntered(MouseEvent e) {
  112.         changeButtonStyle((JButton) e.getComponent(), this.hoverColor,
  113.                 this.hoverIcon);
  114.     }
  115.  
  116.     @Override
  117.     public void mouseExited(MouseEvent e) {
  118.         changeButtonStyle((JButton) e.getComponent(), this.normalColor,
  119.                 this.normalIcon);
  120.     }
  121.  
  122.     private void changeButtonStyle(JButton btn, Color color, ImageIcon icon) {
  123.         btn.setBackground(color);
  124.         btn.setIcon(icon);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement