• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

howto prevent JLabel text selection

 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/AWT forum.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since when can you "select" text in a JLabel?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic