Creating a multi-line text tooltip for a JLabel

An easy way is to have your tooltip text to be HTML:

   label.setToolTipText("<html>This is a two-line<br> tooltip text!</html>");

as in following example:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
                            
public class Main extends JFrame
{ 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
      JLabel label = new JLabel("Mouse-over me!");
      getContentPane().add(label);
                         
      label.setToolTipText("<html>This is a two-line<br> tooltip text!</html>");
                         
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });      
                           
      setSize(new Dimension(200, 200));
   } 
                         
   public static void main(String[] args) throws Exception {
      Main main = new Main();
      main.setVisible(true);
   }
}

Embedding a clickable HTML image in a JLabel

A JLabel is able to render HTML but it does not respond to clicks on anchor tags. You could do something simple like add a MouseListener to your JLabel and respond to mouse events like a normal browser would.

Main.java:

import java.awt.event.*;
import java.awt.font.*; 
import javax.swing.*;
import java.util.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      final JLabel label = new JLabel("<html><a href="dummy"><img src="file:c:\esuslogo.gif"></a></html>");
      getContentPane().add(label);
 
      label.addMouseListener(new MouseAdapter() {
         public void mouseEntered(MouseEvent me) {
            label.setCursor(new Cursor(Cursor.HAND_CURSOR));
         }
         public void mouseExited(MouseEvent me) {
            label.setCursor(Cursor.getDefaultCursor());
         }
         public void mouseClicked(MouseEvent me) {
            System.out.println("Clicked on JLabel image");
         }
      });
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Displaying the EURO sign in a JLabel

JLabel is able to render HTML (3.2). You can use the code € inside html tags as shown in following example:

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JLabel label = new JLabel("<html>&#8364;</html>");
      getContentPane().add(label);     
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Adding a border around a JLabel

Use the method setBorder.

Main.java:

import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JLabel label = new JLabel("This label should have a thick red border");
      getContentPane().add(label);     
      label.setBorder(new LineBorder(Color.red, 3)); 
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Changing the font color of a JLabel

Use the method setForeground.

Main.java:

import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JLabel label = new JLabel("The font and color of this JLabel have been changed!");
      getContentPane().add(label);  
      label.setFont(new Font("Tahoma", Font.ITALIC, 30));
      label.setForeground(Color.red);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Catching events when the mouse moves over a JLabel

You can add a MouseMotionListener for mouse movements or a MouseListener for other interesting mouse events. This example shows how to invert the color of the JLabel when the mouse is positioned over it.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      final JLabel label = new JLabel("Move your mouse over this JLabel!");
      label.setOpaque(true);
      getContentPane().add(label);  
 
      label.addMouseMotionListener(new MouseMotionAdapter() {
         public void mouseMoved(MouseEvent me) {
            System.out.println("Mouse is being moved over JLabel");
         }
      });
 
      label.addMouseListener(new MouseAdapter() {
         public void mouseEntered(MouseEvent evt) {
            Color c = label.getBackground();
            label.setBackground(label.getForeground());
            label.setForeground(c);
         }
 
         public final void mouseExited(MouseEvent evt) { 
            Color c = label.getBackground();
            label.setBackground(label.getForeground());
            label.setForeground(c);            
         } 
         
         public final void mouseClicked(MouseEvent evt) { 
            System.err.println("Mouse has been clicked on JLabel"); 
         } 
      }); 
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(300, 200);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Creating an editable JLabel

You can use a CardLayout, assign card 1 a JLabel and assign card 2 a JTextField. Show card 1 and switch to card 2 when it is time to edit. In the following example, the behavior is defined to edit the JLabel whenever the mouse moves over it.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      final JLabel label = new JLabel("Move your mouse over this JLabel!");
      final JTextField textfield = new JTextField();
 
      final CardLayout cl = new CardLayout();
      final JPanel panel = new JPanel(cl);
 
      panel.add(label, "label component");
      panel.add(textfield, "textfield component");
 
      getContentPane().add(panel);
  
      label.addMouseListener(new MouseAdapter() {
         public final void mouseEntered(MouseEvent evt) { 
            textfield.setText(label.getText());
            cl.show(panel, "textfield component");
         } 
      });
 
      textfield.addMouseListener(new MouseAdapter() {
         public final void mouseExited(MouseEvent evt) { 
            label.setText(textfield.getText());
            cl.show(panel, "label component");
         } 
      }); 
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(300, 200);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Creating a JLabel with strike-through text

The easiest way is to use the HTML tag strike.

Main.java:

import java.awt.event.*;
import java.awt.font.*; 
import javax.swing.*;
import java.util.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JLabel label = new JLabel("<html><strike>Strike-through text</strike></html>");
      getContentPane().add(label);
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(300, 200);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Creating a JLabel with the text underlined

The easiest way is to use the HTML tag u. If you don’t want to use the HTML feature, check out How do I create an underlined JLabel without using HTML?

Main.java:

import java.awt.event.*;
import java.awt.font.*; 
import javax.swing.*;
import java.util.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JLabel label = new JLabel("<html><u>Underlined text with HTML</u></html>");
      getContentPane().add(label);
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(300, 200);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}