1

Short question, hopefully simple solution:

I've got my own renderer for a ListView, nothing too fancy, it just connects a Label and and Icon. My questions is, so far, the Label ignores my "\n"s. How can I change that? I'd like to have two lines for the information I present.

Thanks for listening.

pedrofurla
  • 12,763
  • 1
  • 38
  • 49
Lanbo
  • 15,118
  • 16
  • 70
  • 147
  • Note that this issue is not Scala specific. It works the same way as in Java 6, so you can use any resources about Swing you can find. – Raphael Feb 02 '11 at 22:45

2 Answers2

3

Use html for your Label. Like this: new JLabel("<html>line 1<br>line 2</html>");

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
  • If it is a size issue try this approach: myLabel.setPreferredSize(new Dimension(200, 200)); You can make it more sophisticated if you getFontMetrics() and calculate the size. – Costis Aivalis Feb 02 '11 at 20:33
  • Nope, it is not the size, there is plenty of room - it was even enough to swallow the \n and show both in one line. – Lanbo Feb 02 '11 at 20:56
  • Here is another option: new JLabel("
    a line
    another line
    ");
    – Costis Aivalis Feb 02 '11 at 21:04
  • Hrm nope... maybe it is because I cannot use the `JLabel`, it has to be a Scala Label. But thanks for trying. – Lanbo Feb 03 '11 at 14:30
0
scala> import java.awt._
scala> import javax.swing._
scala> val frame = new JFrame()
scala> frame.setVisible(true)
scala> frame.setPreferredSize(new Dimension(400,300))
scala> val l = new JLabel("abc\nefg")
scala> frame.getContentPane.add(l)
scala> frame.pack
scala> l.setText("<html>abc<br>def</html>")

Using \n doesn't work, but <html>abc<br>def</html> does.

ziggystar
  • 28,410
  • 9
  • 72
  • 124