user2328798
user2328798

Reputation: 1

Displaying multiple line text in JLabel

I have a problem to display multiple line in Jlabel. I tried to use html tag and that didn't helped me. I just wonder why the following code is not working. I used <br> tag and till it displays in one line. Any help please...

My Java code is the following

package p1;
import javax.swing.*;
import java.awt.*;

public class MemoryUtil 
{
    private static final int MegaBytes = 10241024;

    public static void main(String args[])
    {

        long freeMemory = Runtime.getRuntime().freeMemory()/MegaBytes;
        long totalMemory = Runtime.getRuntime().totalMemory()/MegaBytes;
        long maxMemory = Runtime.getRuntime().maxMemory()/MegaBytes;

        String data="";
        data= data + " <html> JVM Free Memory:  " + Long.toString(freeMemory)+" MB <br>";
        data=data + "Initial Heap Size of JVM : "+ Long.toString(totalMemory) +" MB <br>";
        data= data + " Maximum Heap Size  <br>of JVM: " + Long.toBinaryString(maxMemory) +" MB </html>";
        createAndShowGUI(data); 
    }

    private static void createAndShowGUI(String input) 
    {       
        JFrame frame = new JFrame("JVM Setting of your Machine ");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new GridLayout());
        frame.setSize(new Dimension(450, 400));
        frame.setLocation(new Point(400, 200));
        frame.setResizable(true);

        JLabel label = new JLabel(input);
        label.setFont(new Font("Serif", Font.BOLD, 20));
        label.setHorizontalAlignment(JLabel.CENTER);     
        frame.add(label);        
        frame.setVisible(true);
    }   
}

Upvotes: 0

Views: 1730

Answers (2)

Glyb
Glyb

Reputation: 96

You have a white space in front of <HTML>. remove it and it works :data= data + "<html> JVM

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168815

data + " <html> JVM Free Memory:  "

Should be more along the lines of:

"<html><body>JVM Free Memory:  "
  1. It requires the <html> element to be the 1st part of the String.
  2. Best practice would be to make it valid HTML by adding the <body> prefix.

Upvotes: 1

Related Questions