This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes howto prevent JLabel text selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "howto prevent JLabel text selection" Watch "howto prevent JLabel text selection" New topic
Author

howto prevent JLabel text selection

Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
Hi all,

I'm looking for a way to prevent the end user to select text displayed in a JLabel with the mouse (my app is actually running on a touchscreen with no mouse). I could not find any method like "setSelectable(boolean b)" in the JLabel javadoc. Anybody has an idea?

Actually, the component needs not to be a JLabel, I just need to display some text.

Thanks in advance


It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Just curious: what's so bad about selecting text?

You could render the text to a BufferedImage and display that, but there must be a simpler way with a JLabel.


There is no emoticon for what I am feeling!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Moving to Swing/AWT forum.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
I didn't think you could do anything with the text in a JLabel in the first place . . . It is all set up by the app and controlled from inside the app. That si why they haven't provided a setSelectable(boolean) method.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Campbell Ritchie:
I didn't think you could do anything with the text in a JLabel in the first place . . . It is all set up by the app and controlled from inside the app. That si why they haven't provided a setSelectable(boolean) method.


The only direct approach I can think of is subclassing the LabelUI or changing some of its default property settings, but that's beyond my meager abilities.
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
Just curious: what's so bad about selecting text?


Well, it just looks ugly on my applicaton !

Here's the long story:

This JLabel is part of a ZoneButton, which is a kind of complex button made of a JPanel containing an Icon on the right, the JLabel on the left and another Icon as background. This ZoneButton acts as a toggle button (the background image changes upon selection/deselection), and the ImageIcon changes depending on the "state" of the zone represented by the ZoneButton (there are 4 possible states at the moment).

The text in the JPanel is configured by the end-user, so using a rendered image is not an option.

The whole stuff runs on a touchscreen mini-pc with no mouse or keyboard. What happens is that when the user drags its finger, the text on the ZoneButton gets selected and, well, it does not look nice.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Since when can you "select" text in a JLabel?
 
 
subject: howto prevent JLabel text selection
 
Similar Threads
How to disable selection in a JTextField?
Mouse click event
generic inheritance
JLabels and setText()
How to build focus rectangle