Simple Buttin UI : Button « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » ButtonScreenshots 
Simple Buttin UI
Simple Buttin 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 = (AbstractButtonc;
    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 = (JComponente.getComponent();
    c.setBorder(m_borderLowered);
    c.setBackground(m_backgroundPressed);
  }

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

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

  public void mouseExited(MouseEvent e) {
    JComponent c = (JComponente.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 = (JComponente.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 = (JComponente.getComponent();
      c.setBorder(m_borderRaised);
      c.setBackground(m_backgroundNormal);
    }
  }

  public static void main(String argv[]) {
    JFrame f = new JFrame();
    f.setSize(400300);
    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. Various Swing buttonsVarious Swing buttons
2. Responding to button pressesResponding to button presses
3. Putting buttons on an appletPutting buttons on an applet
4. Uses reflection to create groups of different types of AbstractButtonUses reflection to create groups of different types of AbstractButton
5. The Swing-ified button exampleThe Swing-ified button example
6. Create a JButton that does not show focusCreate a JButton that does not show focus
7. Demonstration of button events including Action, Item and Change event typesDemonstration of button events including Action, Item and Change event types
8. The event demonstration program for JToggleButtonThe event demonstration program for JToggleButton
9. An example of radio button menu items in actionAn example of radio button menu items in action
10. JToggleButton DemoJToggleButton Demo
11. A Selected ButtonA Selected Button
12. Button demo: Mnemonic, alignment and action command Button demo: Mnemonic, alignment and action command
13. Button action to change the panel backgroundButton action to change the panel background
14. HTML buttonHTML button
15. Ploygon ButtonPloygon Button
16. Displaying a Button with an Icon LabelDisplaying a Button with an Icon Label
17. Displaying a Button with a BorderDisplaying a Button with a Border
18. Displaying a Button with Varying IconsDisplaying a Button with Varying Icons
19. Displaying a Button with Various Label AlignmentsDisplaying a Button with Various Label Alignments
20. Working with Tooltip TextWorking with Tooltip Text
21. Sharing Models between Buttons
22. Sharing an Action between JButton ComponentsSharing an Action between JButton Components
23. Display Message
24. Button Html DemoButton Html Demo
25. Swing Button DemoSwing Button Demo
26. LoadSave: Button actionLoadSave: Button action
27. Button with ICON and Multiline Button with ICON and Multiline
28. Action Button SampleAction Button Sample
29. Mnemonic SampleMnemonic Sample
30. Swing Default ButtonSwing Default Button
31. Button Action SampleButton Action Sample
32. Simple ToggleButton SampleSimple ToggleButton Sample
ww_w_.__j___ava___2_s._c_o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.