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?
No, it is not bad or good practice to use HTML syntax in the Swing JComponents
Today, Java supports HTML 3.2 and there is reduced support for certain CSS attributes
JTextPane and JEditorPane support adding and positioning JComponents using HTML syntax
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With