Share this page 

Make a JLabel selectable with the mouseTag(s): Swing


Simulate the "look and feel" of a JLabel with a JTextfield.
import javax.swing.*;
import java.awt.*;

public class SelectableJLabel extends JPanel {
 public SelectableJLabel() {
  // a regular JLabel
  add(new JLabel("You can't select me"));
  
  // a look-alike JLabel
  JTextField f = new JTextField("You can select me");
  f.setEditable(false);
  f.setBorder(null);
  f.setForeground(UIManager.getColor("Label.foreground"));
  f.setFont(UIManager.getFont("Label.font"));
  add(f);
  }

 public Dimension getPreferredSize() {
  return new Dimension(100, 100);
  }
    
 public static void main(String s[]){
  JFrame frame = new JFrame("SelectableJLabel");
  SelectableJLabel panel = new SelectableJLabel();
        
  frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  frame.getContentPane().add(panel,"Center");
  frame.setSize(panel.getPreferredSize());
  frame.setVisible(true);
  }
}