0

I have a problem with Java Swing JLabel. The text i want to display on the JLabel exceeds the bounds of the JLabel. I want to display it via a Marqueeeffect. I already implemented the effect but when there is a string that exceeds the bounds of the JLabel it gets cut off and the rest gets replaced with "...".

My question is, if there is any opportunity to set the textlength for a JLabel individually, not depending on the bounds, that it doesnt get cut off?

Hope somebody got an answer for me.

I dont use any LayoutManagers and i dont want the JLabel to get resized, it should only can contain text longer than the bounds of it.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chocolate
  • 434
  • 1
  • 5
  • 16
  • see if [this](http://stackoverflow.com/questions/5380037/java-jlabel-jbutton-on-some-systems-i-get-an-ellipsis-and-on-some-syste) post helps. – Bala R Mar 19 '13 at 13:14
  • see [this](http://stackoverflow.com/questions/3617326/marquee-effect-in-java-swing) post too. – Alepac Mar 19 '13 at 13:19
  • 2
    _I dont use any LayoutManagers_ That's your first, your main and only problem. Use them and many problems will solved themselves automatically, including the one you expose hereabove. _i dont want the JLabel to get resized_ it's like saying _I want an icecream, but I don't want a cold one_ Icecreams are cold. If you set a bigger text on a JLabel and want to see it all, you have to change its size. – Guillaume Polet Mar 19 '13 at 13:20
  • Actually, i search for a way to turn off the cut off effect, i want to set the length of the text individually not depending on the bounds – Chocolate Mar 19 '13 at 13:24
  • Guillaume Polet i am using a marquee effect where its possible that the text that should be displayed on the label is longer than the jlabel – Chocolate Mar 19 '13 at 13:28
  • 2
    @PaulKloeppl (you need to add the '@' sign before a name to notify). Check out Alepac link. Anyway, you should definitely use LayoutManager's. There are almost no situation where you don't need one. I seriously doubt that your case does not need one. – Guillaume Polet Mar 19 '13 at 13:33
  • Hey Guys, thank your for your posts, i made a work around with substrings, the effect is not that smooth now but it works! Thank you Guillaume for your answer, but in my particular case the only layoutmanager that would be useful is gridbaglayout and as you know the code gets so long and confusing. – Chocolate Mar 19 '13 at 14:13
  • You can try MigLayout instead of GridBagLayout, it's much easier to use and able to handle more complex layouts. – Emmanuel Bourg Mar 19 '13 at 15:16
  • now we have two duplicate answers (both good :-) from the link referenced by @Alepac - time to close as duplicate question, IMO – kleopatra Mar 19 '13 at 16:58
  • *"I want to display it via a Marqueeeffect."* IE 6 wants its Marquee back. It was never included in HTML ***for good reasons.*** – Andrew Thompson Mar 20 '13 at 02:19

3 Answers3

2

I want to display it via a Marqueeeffect.

Check out the Marquee Panel.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

In this LayoutTest, you can see how the label's UI delegate uses layoutCompoundLabel() to elide the text when label's size falls below the preferred size.

In this MarqueeTest, MarqueePanel has a default FlowLayout, which adopts the display label's preferred size.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

The Swing JLabel was not designed to do marquee scrolling.

Here's the source code for JLabel. You can modify the text handling routines to do a marquee scroll rather than compressing the text with an ellipsis.

Oh, you'd better use a layout manager. Your marquee JLabel won't layout correctly without a layout manager.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Hey thanks for your answer. I made a work around with substring. It isnt that smooth but atleast i can display text that is longer than the label – Chocolate Mar 19 '13 at 14:23