1

I've created a class which extends the JLabel class. This class essentially makes it "nicer" for me to attach the relevant IconImage to my JLabel, and size it correctly too.

When I create a new JLabel, by code, I now do it by something = new MyClass("message", 1); For that code above I'd have the MyClass(String msg, int type) {} constructor in place. In this constructor I want to be able to get the width of the text the user specifies. If that width is over a certain value, let's say 300px then I'd like to insert a line break (essentially making the JLabel word-wrap according to a maximum width).

I've removed the part in the code below which adds the image to the JLabel because that's not relevant to the question - I've created a separate class to handle this, which already works.

package org.me.myimageapp;

import javax.swing.JLabel;

/**
 *
 * @author Jordan
 */
public class chatBubble extends JLabel {

public static final int BUBBLE_TO_USER = 1;
public static final int BUBBLE_FROM_USER = 2;

chatBubble(String message, int bubble_type) {
    if ( (bubble_type!=1) && (bubble_type!=2) ) {
        return;
    }

    this.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    this.setText(message);
    this.setVisible(true);
    System.out.println("WIDTH: "+this.getSize().toString()); //WIDTH: java.awt.Dimension[width=0,height=0]

}

}

When I run this code it returns "WIDTH: java.awt.Dimension[width=0,height=0]" like I placed in the comments.

Is there any way that I can get the width of a JLabel in the constructor? Also is there any way that exists for a JLabel which would allow me to word wrap?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • please learn java naming conventions and stick to them – kleopatra Dec 01 '11 at 09:10
  • Sorry. I'm guessing that you're referring to `chatBubble` which should be `ChatBubble` and `bubble_type` which should be `bubbleType`? I do usually stick to them unless throwing some quick ideas together whilst trying to figure out how to do something - like above. –  Dec 01 '11 at 11:54

2 Answers2

5

Any Swing component's size will be zero until it has been rendered, no way around that. You could estimate the size of the text though by using the FontMetrics class (check out the link to the API).

There are also kludges available to gain you word wrap in a JLabel by using HTML

Edit 1
Regarding your question:

What would you suggest being the best way to estimate it? Something like counting the characters and the multiplying by an average character width?

I don't know as I haven't used FontMetrics. I'd recommend playing with it some and seeing what you can come up with, and also searching for its use in answers on this forum.

Regarding my second solution, please check my answer to a question here: word-wrap-in-jlist-items

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • What would you suggest being the best way to estimate it? Something like counting the characters and the multiplying by an average character width? Edit: I'll check out that class. –  Nov 30 '11 at 19:32
2

You can use html to get a line break in the label's text:

"<html>first line <br>second line"

You can't get the size of any swing component in the constructor, however.

mort
  • 12,988
  • 14
  • 52
  • 97
  • I believe OP already knows about embedding HTML. Therefore, your answer provides little-to-no value. – mre Nov 30 '11 at 19:35
  • "Also is there any way that exists for a JLabel which would allow me to word wrap?" - last sentence in his question. – mort Nov 30 '11 at 19:36
  • "If that width is over a certain value, let's say 300px then I'd like to insert a line break (essentially making the JLabel word-wrap according to a maximum width)" - using the phrase "link break" indicates to me that OP knows about embedding HTML. – mre Nov 30 '11 at 19:38
  • Quite a long shot. Why would he specifically ask about a method then? Maybe he knows HTML, but doesn't know that one can use it in swing components. – mort Nov 30 '11 at 19:41
  • 1
    I didn't know how to insert a line break into a JLabel (or if you could). I know the term from using "\n" and "\r\n" in PHP (I do know HTML and its "
    " though, just not that you could use it in a JLabel).
    –  Nov 30 '11 at 19:50