Posted By

cruelfate on 11/18/10


Tagged

html swing jlabel


Versions (?)

Simple Swing frame for testing HTML in JLabel


 / Published in: Java
 

Note that there is limited support for CSS in Swing components which render HTML text. This example employs padding, margin and font-size.

The full list of supported properties (and their limitations) are in the [Javadoc][1]

[1] http://download.oracle.com/javase/6/docs/api/javax/swing/text/html/CSS.html "CSS"

  1. package org.example;
  2.  
  3. import java.awt.event.WindowAdapter;
  4. import java.awt.event.WindowEvent;
  5. import java.awt.event.WindowListener;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10.  
  11.  
  12. public class Swing extends JPanel {
  13.  
  14.  
  15. private static final long serialVersionUID = -7106531716716468891L;
  16. private JLabel someJLabel;
  17.  
  18. public Swing() {
  19. super();
  20. someJLabel = new JLabel("<html><body><p style=\"padding:10; font-size:30\">First line</p><p style=\"padding:10; font-size:20\">Second line</p></body></html>");
  21. // someJLabel.setFont(new Font("Arial", Font.PLAIN, 16));
  22. this.add(someJLabel);
  23. }
  24.  
  25. public static void main(String[] args) {
  26.  
  27. JFrame frame = new JFrame();
  28.  
  29. frame.getContentPane().add(new Swing());
  30. frame.setSize(30, 140);
  31. frame.pack();
  32.  
  33. WindowListener wndCloser = new WindowAdapter() {
  34. public void windowClosing(WindowEvent e) {
  35. System.exit(0);
  36. }
  37. };
  38. frame.addWindowListener(wndCloser);
  39. frame.setVisible(true);
  40. }
  41. }

Report this snippet  

You need to login to post a comment.