Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it Bad Practice to use HTML Styling in Java Swing Elements?

Tags:

java

swing

It is considered bad practice to style Swing elements using HTML?

As an example, if I want to make a label bigger and red just once, I have two options:

Either using API calls:

JLabel label = new JLabel("This is a title");  
label.setFont(label.getFont().deriveFont(18));  
label.setForeground(Color.red);

Or by using HTML:

JLabel label = new JLabel("<html><font size='4' color='#ff0000'>This is a title");

In addition, the HTML way allows me to emphasise one word instead of a whole label, and other such uses.

What are the drawbacks of using HTML? Is it more expensive? And is formatting not guaranteed on all JREs?

like image 698
Redandwhite Avatar asked Aug 02 '12 14:08

Redandwhite


2 Answers

  1. No, it is not bad or good practice to use HTML syntax in the Swing JComponents

  2. Today, Java supports HTML 3.2 and there is reduced support for certain CSS attributes

  3. JTextPane and JEditorPane support adding and positioning JComponents using HTML syntax

like image 136
mKorbel Avatar answered Oct 10 '22 15:10

mKorbel


Using <font> is bad even in html. And using objects to customize font color, size etc is lot of work in big applications.

Fortunately, javaFX 2 is out. With it you can use CSS to deal with these kinds of things : http://www.oracle.com/technetwork/java/javafx/documentation/index.html

like image 21
baraber Avatar answered Oct 10 '22 16:10

baraber