0

I have created a JLabel and draw a long String on it. But, the problem is, however long is the content, the content always display at the middle of the label's box! I want to push the text to the top of the box.

How can I fix it? Thank you so much!

IMAGE

Font times = new Font("Times New Roman", Font.CENTER_BASELINE, 13);
JLabel mess = new JLabel();
mess.setFont(times);
mess.setLocation(300, 125);
mess.setSize(250, 200);
mess.setText(message);
mess.setForeground(Color.WHITE);
getContentPane().add(mess);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You want to change the horizontal alignment of the text? – MadProgrammer Jun 02 '19 at 05:17
  • I want to push the text to the top of the box. Besides, Why when I using "\n" char, the String does not go to the next line but display "..." ? – Phạm Thiên Phong Jun 02 '19 at 05:20
  • 2
    Start by looking at [`JLabel#setVerticalAlignment`](https://docs.oracle.com/javase/10/docs/api/javax/swing/JLabel.html#setVerticalAlignment(int)); `JLabel`, like most Swing components, supports HTML rendering of strings (over `\n`), so you can create a multiline label by using basic HTML – MadProgrammer Jun 02 '19 at 05:34
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. .. – Andrew Thompson Jun 02 '19 at 06:19
  • 1
    .. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) `mess.setLocation(300, 125);` This is better determined by a layout manager. 5) `mess.setSize(250, 200);` This is better determined by the `String` content and the size of the font and any borders. When the top level container is packed, it will be reduced to the exact size it needs to be. – Andrew Thompson Jun 02 '19 at 06:19
  • 1
    .. (layout manager permitting, they might stretch the width and/or height according to manager and layout constraint). 6) In regard to 'THE BOX' as indicated twice in the picture. Better to `setBorder(new LineBorder(Color.RED))` as the result is sometimes surprising. – Andrew Thompson Jun 02 '19 at 06:25
  • 1
    7) Just noticed `new Font("Times New Roman", Font.CENTER_BASELINE, 13);`. The `Font.CENTER_BASELINE` uses faulty reasoning. It is actually for the font style (plain, *italic*, **bold** etc.) and has nothing to do with positioning. – Andrew Thompson Jun 02 '19 at 06:32
  • More on the HTML mentioned by @MadProgrammer. See [`FixedWidthText`](https://stackoverflow.com/a/5767825/418556) for a short example of multi-line rendering via CSS/HTML. – Andrew Thompson Jun 02 '19 at 06:41
  • Thanks ! It's really useful for me :) – Phạm Thiên Phong Jun 02 '19 at 10:53
  • *"It's really useful for me"* Tip: Add @MadProgrammer (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Jun 03 '19 at 13:47

0 Answers0