Create a custom JLabel by extending JLabel in Java

Description

The following code shows how to create a custom JLabel by extending JLabel.

Example


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;
/*from  w w w . j  a  v  a2 s  . c  o m*/
import javax.swing.JLabel;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.plaf.basic.BasicArrowButton;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Popup JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new Colors());
    frame.setSize(300, 200);
    frame.setVisible(true);
  }

}
class Colors extends JLabel implements Serializable {
  transient private Color color; // not persistent

  private boolean rectangular; // is persistent

  public Colors() {
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent me) {
        change();
      }
    });
    rectangular = false;
    setSize(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);
    }
  }
}

The code above generates the following result.

Create a custom JLabel by extending JLabel in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer