JLabel width always returning 0 in the constructor. Then if the width is over X value, insert a line break?

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).

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.

Edit: I'll check out that class. – jordanmoore Nov 30 at 19:32.

You can use html to get a line break in the label's text: "first line second line" You can't get the size of any swing component in the constructor, however.

I believe OP already knows about embedding HTML. Therefore, your answer provides little-to-no value. – КрыÑ?

A Nov 30 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 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. – КрыÑ?

A Nov 30 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 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). – jordanmoore Nov 30 at 19:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions