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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Swing Components » LabelScreenshots 
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.