Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a text in JLabel

Tags:

java

swing

jlabel

I need to display a JLabel text in this format.

Hoffenheim  1 : 0  Koeln
    Bayern  2 : 1  HSV

I just can't get this done. I've tried String.format() without luck. Any advice?

like image 655
user1533166 Avatar asked Dec 12 '22 00:12

user1533166


1 Answers

You can use HTML. Read How to Use HTML in Swing Components for details. For example you could build a table, similar to the following:

import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class StringDemo {

    public static void main(String arg[]){

        StringBuilder buff = new StringBuilder();
        buff.append("<html><table>");
        buff.append(String.format("<tr><td align='right'>%s</td><td>:</td><td>%s</td></tr>", "Hoffenheim  1", "0  Koeln"));
        buff.append(String.format("<tr><td align='right'>%s</td><td>:</td><td>%s</td></tr>", "Bayern  2", "1  HSV"));
        buff.append("</table></html>");

        JOptionPane.showMessageDialog(null, new JLabel(buff.toString()));
    }
}
like image 114
tenorsax Avatar answered Jan 03 '23 19:01

tenorsax