48

despite many tries I can't get the result that I would like to see - text centered within the JLabel and the JLabel somewhat centered in the BorderLayout. I said "somewhat" because there should be also another label "status" in the bottom-right corner of the window. Here the bit of code responsible for that:

setLayout(new BorderLayout());
JPanel area = new JPanel();
JLabel text = new JLabel(
        "<html>In early March, the city of Topeka," +
        " Kansas,<br>temporarily changed its name to Google..." +
        "<br><br>...in an attempt to capture a spot<br>" +
        "in Google's new broadband/fiber-optics project." +
        "<br><br><br>source: http://en.wikipedia.org/wiki/Google_server" +
        "#Oil_Tanker_Data_Center</html>", SwingConstants.CENTER);
text.setVerticalAlignment(SwingConstants.CENTER);
JLabel status = new JLabel("status", SwingConstants.SOUTH_EAST);
status.setVerticalAlignment(SwingConstants.CENTER);
Font font = new Font("SansSerif", Font.BOLD, 30);
text.setFont(font);
area.setBackground(Color.darkGray);
text.setForeground(Color.green);
// text.setAlignmentX(CENTER_ALIGNMENT);
// text.setAlignmentY(CENTER_ALIGNMENT);
// text.setHorizontalAlignment(JLabel.CENTER);
// text.setVerticalAlignment(JLabel.CENTER);
Font font2 = new Font("SansSerif", Font.BOLD, 20);
status.setFont(font2);
status.setForeground(Color.green);      
area.add(text, BorderLayout.CENTER);        
area.add(status, BorderLayout.EAST);
this.add(area);

Thanks for any help provided.

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Hurdler
  • 891
  • 3
  • 15
  • 30
  • Possible duplicate of [how to center JLabel in Jframe Swing?](http://stackoverflow.com/questions/19506769/how-to-center-jlabel-in-jframe-swing) – 200_success Feb 12 '16 at 07:33

3 Answers3

133

The following constructor, JLabel(String, int), allow you to specify the horizontal alignment of the label.

JLabel label = new JLabel("The Label", SwingConstants.CENTER);
Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
nimmi
  • 1,331
  • 2
  • 8
  • 2
45
myLabel.setHorizontalAlignment(SwingConstants.CENTER);
myLabel.setVerticalAlignment(SwingConstants.CENTER);

If you cannot reconstruct the label for some reason, this is how you edit these properties of a pre-existent JLabel.

ramsey0
  • 1,587
  • 1
  • 12
  • 10
36
String text = "In early March, the city of Topeka, Kansas," + "<br>" +
              "temporarily changed its name to Google..." + "<br>" + "<br>" +
              "...in an attempt to capture a spot" + "<br>" +
              "in Google's new broadband/fiber-optics project." + "<br>" + "<br>" +"<br>" +
              "source: http://en.wikipedia.org/wiki/Google_server#Oil_Tanker_Data_Center";
JLabel label = new JLabel("<html><div style='text-align: center;'>" + text + "</div></html>");
Ercksen
  • 668
  • 9
  • 23
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Thank you. That worked. By the way: can I use html to change the font size within one JLabel? I wanted the source address to be smaller. – Hurdler Jul 25 '11 at 00:10
  • 2
    Try `"source: http://en.wikipedia.org/wiki/Google_server#Oil_Tanker_Data_Center"` – Eng.Fouad Jul 25 '11 at 00:14
  • Actually, the edited version does not work. The methods that I am using later give the following error message: "... is undefined for the type String" – Hurdler Jul 25 '11 at 00:16
  • I've just found the mistake. Thank you for your help. And by the way 2: Do you know how to put the "status" label in the bottom-right corner? – Hurdler Jul 25 '11 at 00:24
  • 3
    Using single quotes can save some backslashes: `"
    "`
    – npostavs Mar 13 '12 at 15:40
  • 1
    This answer didn't work for me, but [the one by nimmi](http://stackoverflow.com/a/18008358/641451) did. – mgarciaisaia Dec 10 '14 at 14:16
  • Thank you! The key thing is if the text is _HTML formatted_ that it ignores JLabel.setHorizontalAlignment() and you have to use text-align instead. – Rolf Feb 04 '21 at 13:48