Simple Button UI : Button « Swing JFC « Java






Simple Button UI

Simple Button UI
  

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;

public class MyButtonUI extends BasicButtonUI implements java.io.Serializable,
    MouseListener, KeyListener {

  private final static MyButtonUI m_buttonUI = new MyButtonUI();

  protected Border m_borderRaised = UIManager.getBorder("Button.border");

  protected Border m_borderLowered = UIManager
      .getBorder("Button.borderPressed");

  protected Color m_backgroundNormal = UIManager
      .getColor("Button.background");

  protected Color m_backgroundPressed = UIManager
      .getColor("Button.pressedBackground");

  protected Color m_foregroundNormal = UIManager
      .getColor("Button.foreground");

  protected Color m_foregroundActive = UIManager
      .getColor("Button.activeForeground");

  protected Color m_focusBorder = UIManager.getColor("Button.focusBorder");

  public static ComponentUI createUI(JComponent c) {
    return m_buttonUI;
  }

  public void installUI(JComponent c) {
    super.installUI(c);

    c.addMouseListener(this);
    c.addKeyListener(this);
  }

  public void uninstallUI(JComponent c) {
    super.uninstallUI(c);
    c.removeMouseListener(this);
    c.removeKeyListener(this);
  }

  public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;
    Dimension d = b.getSize();

    g.setFont(c.getFont());
    FontMetrics fm = g.getFontMetrics();

    g.setColor(b.getForeground());
    String caption = b.getText();
    int x = (d.width - fm.stringWidth(caption)) / 2;
    int y = (d.height + fm.getAscent()) / 2;
    g.drawString(caption, x, y);

  }

  public Dimension getPreferredSize(JComponent c) {
    Dimension d = super.getPreferredSize(c);
    if (m_borderRaised != null) {
      Insets ins = m_borderRaised.getBorderInsets(c);
      d.setSize(d.width + ins.left + ins.right, d.height + ins.top
          + ins.bottom);
    }
    return d;
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
    JComponent c = (JComponent) e.getComponent();
    c.setBorder(m_borderLowered);
    c.setBackground(m_backgroundPressed);
  }

  public void mouseReleased(MouseEvent e) {
    JComponent c = (JComponent) e.getComponent();
    c.setBorder(m_borderRaised);
    c.setBackground(m_backgroundNormal);
  }

  public void mouseEntered(MouseEvent e) {
    JComponent c = (JComponent) e.getComponent();
    c.setForeground(m_foregroundActive);
    c.repaint();
  }

  public void mouseExited(MouseEvent e) {
    JComponent c = (JComponent) e.getComponent();
    c.setForeground(m_foregroundNormal);
    c.repaint();
  }

  public void keyTyped(KeyEvent e) {
  }

  public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if (code == KeyEvent.VK_ENTER || code == KeyEvent.VK_SPACE) {
      JComponent c = (JComponent) e.getComponent();
      c.setBorder(m_borderLowered);
      c.setBackground(m_backgroundPressed);
    }
  }

  public void keyReleased(KeyEvent e) {
    int code = e.getKeyCode();
    if (code == KeyEvent.VK_ENTER || code == KeyEvent.VK_SPACE) {
      JComponent c = (JComponent) e.getComponent();
      c.setBorder(m_borderRaised);
      c.setBackground(m_backgroundNormal);
    }
  }

  public static void main(String argv[]) {
    JFrame f = new JFrame();
    f.setSize(400, 300);
    f.getContentPane().setLayout(new FlowLayout());

    JPanel p = new JPanel();
    JButton bt1 = new JButton("Click Me");
    bt1.setUI(new MyButtonUI());
    p.add(bt1);
    f.getContentPane().add(p);
    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);
    f.setVisible(true);
  }
}


           
         
    
  








Related examples in the same category

1.Creating a JButton Component from Action
2.Various Swing buttonsVarious Swing buttons
3.Responding to button pressesResponding to button presses
4.Putting buttons on an appletPutting buttons on an applet
5.Uses reflection to create groups of different types of AbstractButtonUses reflection to create groups of different types of AbstractButton
6.The Swing-ified button exampleThe Swing-ified button example
7.Create a JButton that does not show focusCreate a JButton that does not show focus
8.Demonstration of button events including Action, Item and Change event typesDemonstration of button events including Action, Item and Change event types
9.The event demonstration program for JToggleButtonThe event demonstration program for JToggleButton
10.An example of radio button menu items in actionAn example of radio button menu items in action
11.JToggleButton DemoJToggleButton Demo
12.A Selected ButtonA Selected Button
13.Button demo: Mnemonic, alignment and action command Button demo: Mnemonic, alignment and action command
14.Button action to change the panel backgroundButton action to change the panel background
15.HTML buttonHTML button
16.Creating a Multiline Label for a JButton Component
17.Lines are left justified. This label text will center the lines
18.Label text italicizes the second line
19.Changing the Label of a JButton Component
20.Ploygon ButtonPloygon Button
21.Displaying a Button with an Icon LabelDisplaying a Button with an Icon Label
22.Displaying a Button with a BorderDisplaying a Button with a Border
23.Displaying a Button with Varying IconsDisplaying a Button with Varying Icons
24.Displaying a Button with Various Label AlignmentsDisplaying a Button with Various Label Alignments
25.Working with Tooltip TextWorking with Tooltip Text
26.Sharing Models between Buttons
27.Sharing an Action between JButton ComponentsSharing an Action between JButton Components
28.Display Message
29.Button Html DemoButton Html Demo
30.Swing Button DemoSwing Button Demo
31.LoadSave: Button actionLoadSave: Button action
32.Button with ICON and Multiline Button with ICON and Multiline
33.Action Button SampleAction Button Sample
34.Mnemonic SampleMnemonic Sample
35.Swing Default ButtonSwing Default Button
36.Button Action SampleButton Action Sample
37.Button icons, a default button, HTML in a button,and button mnemonics.
38.Adding a Rollover and Pressed Icon to a JButton Component
39.Adding a Disabled Icon to a JButton Component
40.Setting the Gap Size Between the Label and Icon in a JButton Component
41.Moving the Icon in a JButton Component
42.Adding an Icon to a JButton Component
43.Moving the Label/Icon Pair in a JButton Component
44.If the action does not have an icon or a different icon must be used, add or change the icon using setIcon():
45.Dynamically update the appearance of a component
46.Simple ToggleButton SampleSimple ToggleButton Sample