Button Label : Label « Swing Components « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing Components » Label 




Button Label
     
//package com.towel.swing;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractButton;
import javax.swing.UIManager;

public class ButtonLabel extends AbstractButton {
  private String text;
  private int height;

  public ButtonLabel(String text) {
    setText(text);
    addMouseListener(new MouseClick());
  }

  @Override
  public void paintComponent(Graphics g) {
    try {
      Graphics2D g2d = (Graphics2Dg.create();
      g2d.setFont(UIManager.getFont("TitledBorder.font"));
      g2d.setColor(getColor());
      g2d.drawString(text, 0(height + 62);
      g2d.dispose();
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
    updateSize();
  }

  private void updateSize() {
    FontMetrics metrics = getFontMetrics(UIManager
        .getFont("TitledBorder.font"));
    height = metrics.getHeight();
    setPreferredSize(new Dimension(metrics.stringWidth(getText()), height));
  }

  private class MouseClick extends MouseAdapter {
    @Override
    public void mouseClicked(MouseEvent evnt) {
      for (ActionListener listener : getActionListeners())
        listener.actionPerformed(new ActionEvent(ButtonLabel.this, 0,
            ""));
    }

    @Override
    public void mouseEntered(MouseEvent evnt) {
      if (getActionListeners().length == 0)
        return;
      setCursor(new Cursor(Cursor.HAND_CURSOR));
    }

    @Override
    public void mouseExited(MouseEvent evnt) {
      setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }
  }

  private Color getColor() {
    return UIManager.getColor("TitledBorder.titleColor");
  }
}

   
    
    
    
    
  














Related examples in the same category
1.MultiLine Label
2.Multi Line Label extends JComponent
3.Multi Line Label extends JPanel
4.Link Label
5.Vertical Label UI
6.Label with large font and ANTIALIAS paint
7.Label 3D
8.URL Label
9.Hyperlink Label
10.Bevel TextBevel Text
11.A JLabel that can be underlined, implements Scrollable
12.Gradient Label
13.Hyperlink Label 2
14.Computes a reasonable set of labels for a data interval and number of labels.
15.Multi-label Label
16.Shadow Label
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.