0

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);
    }   
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2328798
  • 1
  • 1
  • 1
  • 2
  • possible duplicate of [Is there some "Word Wrap" Property of JLabel exist?](http://stackoverflow.com/questions/7861724/is-there-some-word-wrap-property-of-jlabel-exist) – Andrew Thompson Apr 28 '13 at 10:18

2 Answers2

1

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

Glyb
  • 96
  • 3
  • OK - That was maybe to quick. Look at: http://docs.oracle.com/javase/tutorial/uiswing/components/html.html – Glyb Apr 28 '13 at 10:12
  • Glyb, I have already tested those. Based on those tutorials, it should work but it is not working for me. I have no idea. HTML tag in JLabel should work. – user2328798 Apr 28 '13 at 10:16
  • Ahh - Got it. You have a white space in front of . remove it and it works :data= data + " JVM ... – Glyb Apr 28 '13 at 10:22
  • Yes Glyb; That was the problem. I spend the whole day and I did that funny mistake. Big thanks for your help!!! – user2328798 Apr 28 '13 at 10:26
1
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.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Andrew Thompson; you saved me. I make the first tag and it worked fine. Thank you very very much!!! It looks that, even space shouldn't be there Example String data=" ... " not String data=" " – user2328798 Apr 28 '13 at 10:21