Java Swing How to - Extend JLabel to create custom control








Question

We would like to know how to extend JLabel to create custom control.

Answer

 /*w  w w  .  ja  va  2s .c  o  m*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Main{
  public static void main(String[] args) {
    JOptionPane.showMessageDialog(null, new Colors());
  }
  
}
 class Colors extends JLabel implements Serializable {
  private Color color; // not persistent

  private boolean rectangular; // is persistent

  public Colors() {
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent me) {
        change();
      }
    });
    rectangular = false;
    super.setPreferredSize(new Dimension(200, 100));
    change();
  }

  public boolean getRectangular() {
    return rectangular;
  }

  public void setRectangular(boolean flag) {
    this.rectangular = flag;
    repaint();
  }

  public void change() {
    color = randomColor();
    repaint();
  }

  private Color randomColor() {
    int r = (int) (255 * Math.random());
    int g = (int) (255 * Math.random());
    int b = (int) (255 * Math.random());
    return new Color(r, g, b);
  }

  public void paint(Graphics g) {
    Dimension d = getSize();
    int h = d.height;
    int w = d.width;
    g.setColor(color);
    if (rectangular) {
      g.fillRect(0, 0, w - 1, h - 1);
    } else {
      g.fillOval(0, 0, w - 1, h - 1);
    }
  }
}