I have a class called iconLabel that extends JLabel to make buttons. I use the Font Awesome font to set the text of the JLabel to an icon. I also add another JLabel to that iconLabel object in the constructor to display text on the icon. The problem is that the text on the icon exceeds the width of the icon, so you'll get "...". How can I make it so the JLabel text may exceed the icon width? Here is an image for clarification.
1 Answers
The problem is that the text on the icon exceeds the width of the icon, so you'll get "...".
A JLabel can display an Icon with text painted on top of the Icon. There is no need for a custom class to do this.
The basic code is:
JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Easy Way" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);
The thing is that I want to use the font awesome icons
So you use the setText() method and you get an Icon painted? Cool!
Maybe you can use a JPanel with an OverlayLayout
. Then you add the two labels to the panel, making sure the alignmentX/Y values are both 0.5f so the components are centered in the panel. Now the panel should be the size of the largest label.
Or maybe you can use the Text Icon. This class allows you to create an Icon from a text String. You will still need to labels but the sizing should work correctly.
The code should be something like:
JLabel iconLabel = new JLabel();
iconLabel.setFont(...);
TextIcon icon = new TextIcon(iconLabel, "...")
Jlabel label = new JLabel("some text");
label.setIcon(icon);

- 321,443
- 19
- 166
- 288
-
The thing is that I want to use the font awesome icons, because they are easily colored and resized. – RuuddR Mar 24 '18 at 20:54
-
Ah okay, I now use this code, but I still got one problem. My text is still too wide and the parts that are missing are replace by "..." – RuuddR Mar 24 '18 at 21:49
-
@xX4m4zingXx Then, at a guess, you manually setting the size of the parent container and not letting the layout managers do there job – MadProgrammer Mar 24 '18 at 22:44
-
That’s true, I use null layout because it is way easier to use in my opinion and I can get my frame to look like I want it, but not in this case :p – RuuddR Mar 24 '18 at 22:52
-
2*"I use null layout because it is way easier to use.."* And yet it leads to fundamentally broken GUIs - like this one. 'Not using layouts' is the first thing that should be fixed. That arrangement of icons seen above could be achieved using a single column `GridLayout` in a `JPanel` with an `EmptyBorder`, put that panel in the `LINE_START` of a `BorderLayout`. – Andrew Thompson Mar 25 '18 at 00:15
-
BTW - if the problem is fitting the text on an icon, both 'Highscore' and 'Vote for'(?) could be split into two lines. OTOH I'd 1) use actual buttons (`JButton`) for this, 2) use the icons only, and set the text as the tool tip. – Andrew Thompson Mar 25 '18 at 00:20
-
Ah Okay, I'll try to make these buttons using a layout. Also I am using this text for a hover effect. I want the icon to gradually turn dark and then I want the text to appear, so that's why I don't want to use tooltips. I still could put it on 2 lines though – RuuddR Mar 25 '18 at 19:13