It can be done:
The key is in the javax.swing.plaf.basic.BasicLabelUI class, which is the basic interface for shortcuts.
In the drawing method, we see this code:
View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g, paintTextR); }
The BasicHTML class is a provider of HTML features in Java, so theoretically, if you replace the client property for BasicHTML.propertyKey your own implementation of View, then this class will be used and you can do whatever you want to render the text.
The javax.swing.plaf.basic.BasicLabelUI class is the parent of most other LAF label user interfaces, but not for everyone, so it may not work for all LAFs. LAFs that do not support HTML using the BasicHTML class BasicHTML also not work with your fix.
But IMHO this is more a hack than a feature. You program the implementation again, not the interface. Therefore, if you donβt have really serious reasons for this, I would suggest finding a cleaner way to render your custom HTML, such as a subclass of JLabel.
source share