What is the default styling for html in components like JLabel - java

Swing has a nice feature where some components allow text to be specified using html. For example, the JLabel component can use html formatting simply using:
JLabel jl = new JLabel("<html><body><h1>Hello World</h1></body></html>");
I find this feature to be really convenient and I am using it in several places in my app.
I would like to know what the default styling is when I use this feature. I know that I can easily override these by adding my own style attributes, but I would like to know what the starting set is, because it is different than most browser defaults.
Where can I find out the default styling (font, line spacing, etc)? I have not seen anything in the documentation. I tried to browse through the source code, but I was unable to see where the tag gets parsed and the styling gets applied.
Can someone give me a pointer as to where to look for this information, possibly in the OpenJDK source?

The default style sheet is default.css, which can be found in resources.jar under the directory javax/swing/text/html/default.css.
You can also obtain the styles programmatically:
StyleSheet sheet = new HTMLEditorKit().getStyleSheet();

I believe the first answer is correct but incomplete.
Check out SwingUtilities2.displayPropertiesToCSS().
It adds a rule to the document stylesheet based on the component's font and foreground color. It is used by BasicHTML.BasicDocument.

Related

How to change JavaFX slider thumb color without CSS

I'm using ScalaFX, which is currently proving itself to be thoroughly useless and bugging out anytime I try to use css. Is there any way to change the fill color of a Slider without using css, or at least without separate css files?
I've managed to change the track color with slider.setStyle("-fx-control-inner-background: #f08080;"), but can't get the thumb working.
You could use the -fx-base property, which sets the component colors palette according to the value you specify.
Example:
slider.setStyle("-fx-base: #f08080;");
Result:
However why don't you consider using a CSS file? It simplifies everything and allows you to make a lot more customizations. Take a look at the Slider JavaFX CSS reference, I think you will find what you're looking for.

Struts2 s:checkbox label not showing up

In the most bizarre of circumstances, the label for my checkbox do not show up in my Struts2 JSP. I have a series of checkboxes, all following the same format, but none of the labels appear. Here is a sample declaration:
<s:checkbox name="chronCheck1" label="New Sales"/>
This is among the most basic of tasks, but something is obviously missing. I am running Struts2 version 2.2.1 via Weblogic 10. Not really sure what else is necessary to help troubleshoot.
You are probably using simple theme in your JSP. With simple theme you must create labels for inputs by yourself.
About simple theme from documentation:
For example, the textfield tag renders the HTML tag without a label, validation, error reporting, or any other formatting or functionality.
See http://struts.apache.org/2.x/docs/themes-and-templates.html.
It is not very clear from the official documentation for the s:checkbox, but in my experiment, if you don't specify the theme, the default theme does not support label. When I change the theme to either xhtml or css_xhtml it successfully showed the label. However these themes also add additional markup and css classes, so you probably need extra css styling and specifying labelposition (top, left, right or bottom) to achieve your desired look and feel. Another option is to create your own theme and apply it. Please refer to this documentation:
https://struts.apache.org/docs/struts-2-themes.html

GWT - DecoratorPanel - dynamic background color

I wondering how to set background color for DecoratorPanel dynamically? All examples I could see just showing CSS static modification but I couldn't find any dynamic examples. If you have some helpful snippets please share
For dynamically editing styles, you can use the Style object, reached via the underlying DOM Element. Something like the following should work:
DecoratorPanel panel = new DecoratorPanel();
panel.getElement().getStyle().setBackgroundColor("#000000");
Doing this will assign an inline style to your element in the DOM. You'll find methods for most properties on the Style object, with "setProperty(String, String)" available for your more rare style needs.
If you are only changing backgrounds between a few preset color, you may also consider simply changing a css class name on the panel. This gives you the benefit of keeping all background styling within css. You can do this via:
panel.addStyleName("css-class-name");
and
panel.removeStyleName("css-class-name");

Loading custom fonts at runtime for use with JTextPane

Thanks for your time. My question is regarding the display of different fonts within the one JTextPane. My client wishes to view a word in two different languages within the one field. They've explicitly specified that they wish the different languages (namely Amharic, Arabic, Coptic and Hebrew) to be shown with different fonts. These are obviously non-standard fonts and I can't rely on the user having the required fonts installed on their OS.
From my research I've found that I can load a font file at runtime and set the JTextPane's font accordingly, which is fine if I just wanted to use one font, not two. I've also read about adding fonts to the OS' font directory or the JRE's font directory, outlined here.
I was hoping however, that there might be away to use the fonts without altering the user's OS. Am I out of luck?
Thanks again for your time and I look forward to any replies with bright ideas!
From my research I've found that I can load a font file at runtime and set the JTextPane's font accordingly, which is fine if I just wanted to use one font, not two.
A JTextPane can use multiple fonts.
Check out the section from the Swing tutorial on Text Component Features for an example of playing with the attributes of the text in the text pane.
Edit:
However to use multiple fonts, the only way I have worked out to set the font is by creating a MutableAttributeSet and setting the "FontFamily" attribute (a string) to the desired font name, and then assigning the Attribute set to the text using the StyledDocument.setCharacterAttributes
Reading the API for the createFont() method it looks like you should be able to use:
GraphicsEnvironment.registerFont(Font)

Styling HTML in a JEditorPane or JTextPane

I want to display HTML in a JEditorPane or JTextPane, but I want the style (font size) to come from the Look and Feel. Is there a way to do this, or do you have to use embedded HTML styling?
Here is an example:
epText = new JEditorPane("text/html", content);
StyleSheet ss = ((HTMLEditorKit)epText.getEditorKit()).getStyleSheet();
ss.addRule("p {font-size:" + FontManager.getManager().getFontSize() + "}");
HTMLEditorKit kit = (HTMLEditorKit) epText.getEditorKit();
kit.setStyleSheet(ss);
epText.setEditorKit(kit);
Whenever I set the editor kit, all text disappears. Do I need to set the text everytime?
Yes.
Dig into this snippet, from the Java API for HTMLEditorKit:
Customization from current LAF
HTML provides a well known set of features without exactly specifying
the display characteristics. Swing has
a theme mechanism for its
look-and-feel implementations. It is
desirable for the look-and-feel to
feed display characteristics into the
HTML views. An user with poor vision
for example would want high contrast
and larger than typical fonts.
The support for this is provided by the StyleSheet class. The
presentation of the HTML can be
heavily influenced by the setting of
the StyleSheet property on the EditorKit.
Edit: when you set the Editor Kit, it de-installs the old kit and installs the new one. This changes the underlying model, which is why the text 'disappears'.
Read the API for more info.
But you may not need to re-create the entire kit... just add a new sheet to your style.
Call this from the main method after you set L&F.
public static void setDefaultStyle()
{
// looks like WTF but that is the way to set default CSS
StyleSheet sheet=new HTMLEditorKit().getStyleSheet();
// add your rules
sheet.addRule("...");
sheet.addRule("...");
}
EDIT: At least this should work for JTextPane.

Categories