Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting text from a JLabel?

Tags:

java

swing

Is it possible to enable the selection of text from a JLabel? If not, what's the best alternative control to use, and how can it be configured to appear like a JLabel?

like image 407
mellis Avatar asked Jun 15 '09 19:06

mellis


People also ask

Can JLabel be editable?

Yes you can do it :D.

What can a JLabel not do?

JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus .


1 Answers

A JTextField doesn't allow html-formatted text like a JLabel. If you want selectable html text you could alternatively try a JTextPane set to html formatting:

JTextPane f = new JTextPane(); f.setContentType("text/html"); // let the text pane know this is what you want f.setText("<html>Hello World</html>"); // showing off f.setEditable(false); // as before f.setBackground(null); // this is the same as a JLabel f.setBorder(null); // remove the border 
like image 67
Arend Avatar answered Sep 24 '22 16:09

Arend