Question

So I have this situation:

I have a JList that displays a bunch of Strings.

However, these strings are really long and JList is really narrow. Meaning the strings won't fit.

What I want to do is make each entry to have two rows, like this:

|Word word word  |
|word word wor...|

It would do wordWrap for the first row, and then finish the secon't row by cutting the rest of the string and appeinging the three dots to what is left in a way maximum space is filled.

It doesn't really matter what I do, the important thing is that I have to use FontMetrics to measure all this stuff so I can make it work. And that's the catch.

Until whole getListCellRendererComponent(...) method is executed, the component will not be painted, thus having no graphics, thus making any font measurement impossible.

How do I get around it?

P.S. I need to use the JLabel for the visuals.

Was it helpful?

Solution

the component will not be painted, thus having no graphics, thus making any font measurement impossible.

You don't need the Graphics to use the FontMetrics.

See Left Dot Renderer which is used for a JTable, but the concepts should be the same for a list renderer as well.

OTHER TIPS

You need to use JLabels? It'd be a lot easier to have getListCellRendererComponent() return a JTextArea whose height has been set to 2 lines and which has setLineWrap() and setWrapStyleWord() both set to true. Make the JTextArea uneditable, and it'll look like a 2-line JLabel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top