How can we rotate a JLabel text in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

A JLabel is a subclass of JComponent class and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface. 

By default, JLabel can display a text in the horizontal position and we can rotate a JLabel text by implementing the rotate() method of Graphics2D class inside the paintComponent().


public abstract void rotate(double theta, double x, double y)


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class RotateJLabelTest extends JFrame {
   public RotateJLabelTest() {
      setTitle("Rotate JLabel");
      JLabel label = new RotateLabel("TutorialsPoint");
      add(label, BorderLayout.CENTER);
      setSize(400, 300);
   private class RotateLabel extends JLabel {
      public RotateLabel(String text) {
         Font font = new Font("Verdana", Font.ITALIC, 10);
         FontMetrics metrics = new FontMetrics(font){};
         Rectangle2D bounds = metrics.getStringBounds(text, null);
         setBounds(0, 0, (int) bounds.getWidth(), (int) bounds.getHeight());
      public void paintComponent(Graphics g) {
         Graphics2D gx = (Graphics2D) g;
         gx.rotate(0.6, getX() + getWidth()/2, getY() + getHeight()/2);
   public static void main(String[] args) {
      new RotateJLabelTest();


Published on 06-Sep-2019 15:30:15