tags:

views:

334

answers:

3

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?

A: 

JLabels cannot be editable.

However, you could use a JTextField and just change the foreground / background colors to make it appear as a JLabel. If you wanted to be really fancy you could add code to change the colors when it's selected to indicate that it's editable.

sangretu
+5  A: 

You can use a JTextField without enabling the editing

JTextField f=new JTextField("Hello World");
f.setEditable(false);
content.add(f);

Pierre

Pierre
+2  A: 

When using JTextField, you will also want to remove the border: f.setBorder(null);

and set the disabled text color: f.setDisabledTextColor(Color.black);

akf