Class InlineHTML
JFormattedTextField
InlineHTML is a utility class designed to simplify the creation of rich text
content for JLabels.
It provides methods for applying various HTML-like formatting, such as bold, italic,
underline, font colour, font size, and paragraph wrapping, without directly manipulating
raw HTML strings.
The class internally manages a list of opening and closing HTML tags, which are applied
to the content
when toString() or wrapHTML() is called. This allows for method chaining
to build complex
formatted text efficiently.
Example usage:
InlineHTML richText = new InlineHTML("Hello, World!")
.bold() // wraps in <b></b> tag
.font(Color.BLUE) // wraps in <font color="#0000FF"></font>
.paragraph(); // wraps in <p></p>
myLabel.setText(richText.wrapHTML()); // finally wraps in <html></html>
From J3Engine
- Author:
- Lehlogonolo Poole
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAn enumeration representing standard HTML heading tags (H1 to H6). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stringstatic StringHTML entity for the equals symbol.static StringHTML entity for the greater than symbol.static StringHTML constant for a horizontal rule.static StringHTML entity for the less than symbol.static StringHTML constant for a line break. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty InlineHTML instance.InlineHTML(String cont) Constructs a new InlineHTML instance with the given content.InlineHTML(String cont, boolean esc) Constructs a new InlineHTML instance with the given content, optionally escaping HTML special characters. -
Method Summary
Modifier and TypeMethodDescriptionadd(InlineHTML jLabelRichText) Appends additional content to the current rich text.addAttributes(LinkedHashMap<String, String> attributes) Adds attributes to the most recently opened HTML tag.Appends additional content to the current rich text, preceded by an HTML line break.addStyle(LinkedHashMap<String, String> style) Adds inline CSS styles to the most recently opened HTML tag.bold()Applies bold formatting to the content.static StringApplies a specific colour to the font of the content.Applies a specific colour and size to the font of the content.Applies a specific colour, size, and background colour to the font of the content.Applies a specific font size to the content.static InlineHTMLfrom(String string, InlineHTML style) Creates a new InlineHTML instance from a given string and copies the opening and closing tags from an existing InlineHTML style object.getClose()Returns the list of closing HTML tags that will be applied to the content.getOpen()Returns the list of opening HTML tags that will be applied to the content.heading(InlineHTML.Heading heading) Applies a heading tag (H1-H6) to the content.static StringhtmlOf(InlineHTML... richTexts) StaticRefs factory method to combine multiple InlineHTML instances into a single HTML string, wrapped in an<html>tag.static StringStaticRefs factory method to combine multiple String instances into a single HTML string, wrapped in an<html>tag.italic()Applies italic formatting to the content.Wraps the content in a paragraph tag.static InlineHTMLparagraphWrap(InlineHTML... richTexts) StaticRefs factory method to combine multiple InlineHTML instances and wrap them within a single paragraph<p>tag.Sets the list of closing HTML tags.Sets the list of opening HTML tags.Applies subscript formatting to the content.Applies superscript formatting to the content.toString()Generates the HTML string representation of the rich text content, applying all accumulated open and close tags.Applies underline formatting to the content.wrapDiv(int width) Wraps the content in a div tag with a specified width.wrapHTML()Wraps the generated HTML string with<html>tags.wrapTag(String tag, LinkedHashMap<String, String> attributes) Wraps the content with a generic HTML tag, applying specified attributes.wrapTag(String tag, LinkedHashMap<String, String> attributes, LinkedHashMap<String, String> style) Wraps the content with a generic HTML tag, applying specified attributes and inline styles.wrapUsing(InlineHTML other) Applies the opening and closing tags from anotherInlineHTMLinstance to the current instance.
-
Field Details
-
-
open
-
close
-
HORIZONTAL_LINE
HTML constant for a horizontal rule. -
LINE_BREAK
HTML constant for a line break. -
GREATER_THAN
HTML entity for the greater than symbol. -
LESS_THAN
HTML entity for the less than symbol. -
EQUAL
HTML entity for the equals symbol.
-
-
Constructor Details
-
InlineHTML
Constructs a new InlineHTML instance with the given content.- Parameters:
cont- The initial string content.
-
InlineHTML
public InlineHTML()Constructs an empty InlineHTML instance. The content can be added later usingadd()oraddLn()methods. -
InlineHTML
Constructs a new InlineHTML instance with the given content, optionally escaping HTML special characters.- Parameters:
cont- The initial string content.esc- If true, HTML special characters (<,>,=) will be escaped.
-
-
Method Details
-
from
Creates a new InlineHTML instance from a given string and copies the opening and closing tags from an existing InlineHTML style object. This is useful for applying an existing style to new content.- Parameters:
string- The initial string content for the new instance.style- The InlineHTML instance from which to copy the open and close tags.- Returns:
- A new InlineHTML instance with the specified content and copied style.
-
add
Appends additional content to the current rich text.- Parameters:
cont- The string content to append.- Returns:
- The current InlineHTML instance for method chaining.
-
addLn
Appends additional content to the current rich text, preceded by an HTML line break.- Parameters:
cont- The string content to append.- Returns:
- The current InlineHTML instance for method chaining.
-
addAttributes
Adds attributes to the most recently opened HTML tag. This method is useful for dynamically adding properties likeid,class, or other custom attributes to a tag that has just been opened using methods likewrapTag().The attributes are appended to the last tag added to the
openlist.- Parameters:
attributes- ALinkedHashMapwhere keys are attribute names and values are attribute values.- Returns:
- The current InlineHTML instance for method chaining.
-
addStyle
Adds inline CSS styles to the most recently opened HTML tag. This method allows for dynamic application of CSS properties to a tag that has just been opened. It checks if a 'style' attribute already exists on the last opened tag and throws aRuntimeExceptionif it does, to prevent overwriting.The styles are formatted into a CSS string and then added as a 'style' attribute.
- Parameters:
style- ALinkedHashMapwhere keys are CSS property names and values are their corresponding values.- Returns:
- The current InlineHTML instance for method chaining.
-
bold
Applies bold formatting to the content.- Returns:
- The current InlineHTML instance for method chaining.
-
italic
Applies italic formatting to the content.- Returns:
- The current InlineHTML instance for method chaining.
-
underline
Applies underline formatting to the content.- Returns:
- The current InlineHTML instance for method chaining.
-
font
Applies a specific colour to the font of the content. The colour is converted to a hexadecimal RGB string.- Parameters:
col- The Color object to apply.- Returns:
- The current InlineHTML instance for method chaining.
-
font
Applies a specific colour and size to the font of the content. The colour is converted to a hexadecimal RGB string.- Parameters:
col- The Colour object to apply.size- The font size as a string (e.g., "1", "+2", "-3").- Returns:
- The current InlineHTML instance for method chaining.
-
font
Applies a specific font size to the content.- Parameters:
size- The font size as a string (e.g., "1", "+2", "-3").- Returns:
- The current InlineHTML instance for method chaining.
-
colToStr
-
font
Applies a specific colour, size, and background colour to the font of the content. The colours are converted to hexadecimal RGB strings.- Parameters:
col- The font Colour object to apply.size- The font size as a string (e.g., "1", "+2", "-3").backgroundCol- The background Colour object to apply.- Returns:
- The current InlineHTML instance for method chaining.
-
paragraph
Wraps the content in a paragraph tag.- Returns:
- The current InlineHTML instance for method chaining.
-
heading
Applies a heading tag (H1-H6) to the content.- Parameters:
heading- TheInlineHTML.Headingenum representing the desired heading level.- Returns:
- The current InlineHTML instance for method chaining.
-
superscript
Applies superscript formatting to the content.- Returns:
- The current InlineHTML instance for method chaining.
-
subscript
Applies subscript formatting to the content.- Returns:
- The current InlineHTML instance for method chaining.
-
wrapTag
Wraps the content with a generic HTML tag, applying specified attributes. This method is used internally by other formatting methods likebold(),italic(), andfont().- Parameters:
tag- The HTML tag name (e.g., "b", "i", "font").attributes- A LinkedHashMap of attribute names and their values (e.g., "color" -> "#FF0000").- Returns:
- The current InlineHTML instance for method chaining.
-
wrapTag
public InlineHTML wrapTag(String tag, LinkedHashMap<String, String> attributes, LinkedHashMap<String, String> style) Wraps the content with a generic HTML tag, applying specified attributes and inline styles. This method allows for more complex styling thanwrapTag(String, LinkedHashMap).- Parameters:
tag- The HTML tag name (e.g., "div", "span").attributes- A LinkedHashMap of attribute names and their values (e.g., "id" -> "myDiv").style- A LinkedHashMap of CSS style properties and their values (e.g., "color" -> "red", "font-size" -> "12px").- Returns:
- The current InlineHTML instance for method chaining.
-
wrapDiv
Wraps the content in a div tag with a specified width.- Parameters:
width- The width of the div in pixels.- Returns:
- The current InlineHTML instance for method chaining.
-
wrapUsing
Applies the opening and closing tags from anotherInlineHTMLinstance to the current instance. The tags fromotherare addedbeforethe current instance's own tags, effectively wrapping the current instance's content and styling.- Parameters:
other- TheInlineHTMLinstance whose tags are to be applied.- Returns:
- The current
InlineHTMLinstance for method chaining.
-
wrapHTML
Wraps the generated HTML string with<html>tags.- Returns:
- The complete HTML string, including the
<html>and</html>tags.
-
toString
Generates the HTML string representation of the rich text content, applying all accumulated open and close tags. -
htmlOf
StaticRefs factory method to combine multiple InlineHTML instances into a single HTML string, wrapped in an<html>tag.- Parameters:
richTexts- An array of InlineHTML objects to combine.- Returns:
- A single HTML string containing all the rich text content.
-
htmlOf
StaticRefs factory method to combine multiple String instances into a single HTML string, wrapped in an<html>tag.- Parameters:
txt- An array of String objects to combine.- Returns:
- A single HTML string containing all the text content.
-
paragraphWrap
StaticRefs factory method to combine multiple InlineHTML instances and wrap them within a single paragraph<p>tag.- Parameters:
richTexts- An array of InlineHTML objects to combine.- Returns:
- A new InlineHTML instance representing the combined content within a paragraph.
-
getRawContent
-
add
-
getClose
Returns the list of closing HTML tags that will be applied to the content. These tags are applied in reverse order whentoString()is called.- Returns:
- An ArrayList of closing HTML tag strings.
-
getOpen
Returns the list of opening HTML tags that will be applied to the content. These tags are applied in order whentoString()is called.- Returns:
- An ArrayList of opening HTML tag strings.
-
setClose
Sets the list of closing HTML tags. This method is primarily used internally for copying styles or for advanced manipulation.- Parameters:
close- An ArrayList of closing HTML tag strings to set.- Returns:
- The current InlineHTML instance for method chaining.
-
setOpen
Sets the list of opening HTML tags. This method is primarily used internally for copying styles or for advanced manipulation.- Parameters:
open- An ArrayList of opening HTML tag strings to set.- Returns:
- The current InlineHTML instance for method chaining.
-