Technical Support
🟒 Online now
Hello πŸ‘‹

How can we help you today?
Contact on WhatsApp
B4X Store
jdashboardUI / Core & Utilities / jdashboardUtils
Core & Utilities

jdashboardUtils

38 static helpers for data, colors, JavaFX, text, tables, images, Views and PDF

Source
jdashboardUI.b4xlib Β· Public API only
Store language: en

Core & Utilities Β· jdashboardUI 23.5 Β· B4J / JavaFX

jdashboardUtils is a StaticCode toolbox with reusable helpers for UI styling, colors, text measurement, tables, images, SQL calculations, validation, view positioning and HTML β†’ PDF conversion.

It does not require Initialize:

Dim Width As Int = jdashboardUtils.MeasureTextWidth("Hello", xui.CreateDefaultFont(14))

The analyzed version exposes 38 public Subs.

Highlights

  • SQL aggregates: average, maximum, minimum, sum, count and median.
  • Median calculation for in-memory Lists.
  • Text width/height measurement with optional wrapping.
  • ARGB / HEX / RGBA color utilities.
  • Automatic contrast-color selection.
  • Alpha manipulation and related random colors.
  • JavaFX shadow and glow effects.
  • Buttons with images or icon fonts.
  • Rich image/icon tooltips.
  • TableView width and wrapped-header helpers.
  • Quick TextArea / Node styling.
  • Font/icon to bitmap conversion.
  • Circular and rounded-rectangle bitmaps.
  • Code-created CustomListView, JImageView and Label helpers.
  • Email syntax validation.
  • View absolute-position helper.
  • HTML β†’ PDF conversion using Flying Saucer + iText.

Basic usage

Because it is StaticCode, call it directly:

Dim c As Int = jdashboardUtils.HexToInt("#35C48D")
Log(jdashboardUtils.intToHex(c))
Log(jdashboardUtils.ValidateEmail("user@example.com"))

There are no Designer Properties, events or AddToParent.


SQL calculations

calcValue

Supported actions:

avg
max
min
sum
count
median
Private SQL1 As SQL
SQL1.InitializeSQLite(File.DirApp, "shop.db", True)

Dim Total As Object = jdashboardUtils.calcValue( _
    "sum", _
    "products", _
    "price", _
    SQL1)

Log(Total)

An uninitialized SQL object returns -1.

TableName and Column are concatenated into SQL. Use only application-controlled table/column names.


calcValue2

Adds a value filter:

Dim Result As Object = jdashboardUtils.calcValue2( _
    "count", _
    "products", _
    "status", _
    "Active", _
    SQL1)

It supports the same six actions.

The current implementation also concatenates value into SQL, so this helper is intended for trusted/validated data, not arbitrary external input.


CalculateMedian

Dim Values As List
Values.Initialize2(Array As Double(5, 9, 2, 10, 7))

Log(jdashboardUtils.CalculateMedian(Values))

The source List is copied before sorting.

Use a non-empty numeric List.

The public return type is String, while the internal calculation uses Double.


valueformated

Dim Value As Object = jdashboardUtils.valueformated("123")

The current implementation returns the incoming value unchanged and is mainly a compatibility/helper hook.


Text measurement

Dim F As B4XFont = xui.CreateDefaultFont(14)

Dim W As Int = jdashboardUtils.MeasureTextWidth("Dashboard", F)
Dim H As Int = jdashboardUtils.MeasureTextHeight("Line 1" & CRLF & "Line 2", F)

Dim WrappedH As Float = jdashboardUtils.MeasureTextHeightWithWrap( _
    "A long paragraph that wraps.", _
    F, _
    220dip)

On B4J these helpers use javafx.scene.text.Text.

MeasureTextHeightWithWrap uses a fallback of MeasureTextHeight * 1.5 if the JavaFX measurement fails.


Colors

HEX conversion

Dim Hex As String = jdashboardUtils.intToHex(0xFF35C48D)
Dim Color As Int = jdashboardUtils.HexToInt("#35C48D")

Darker/background variation

Dim Darker As Int = jdashboardUtils.colortobackcolor(0xFF35C48D, 20)

The helper subtracts the adjustment from R/G/B and clamps every channel to 0–255.


Contrast color

Dim TextColor As Int = jdashboardUtils.GetIntContrastColor( _
    BackColor, _
    xui.Color_Black, _
    xui.Color_White)

It uses YIQ brightness:

R * 0.299 + G * 0.587 + B * 0.114

If YIQ is greater than 200, ForLightColor is returned; otherwise ForDarkColor.

A practical pattern is to pass dark text as ForLightColor and light text as ForDarkColor.

GetPaintContrastColor provides the same behavior using Paint.


Alpha

Dim C As Int = jdashboardUtils.SetAlphaToIntColor(0xFF35C48D, 128)

Dim P As Paint = jdashboardUtils.SetAlphaToPaintColor( _
    fx.Colors.Red, _
    100)

Alpha range: 0–255.


RGBA string

Dim C As Int = jdashboardUtils.rgbaToColor("rgba(255, 0, 0, 0.5)")

Expected format:

rgba(r, g, b, a).


Dim C As Int = jdashboardUtils.CompareAndGenerateRandomColor( _
    xui.Color_Blue, _
    xui.Color_Green)

The result is based on the average RGB value plus a random Β±20 variation per channel.


Shadows, glow and styling

SetShadow

jdashboardUtils.SetShadow(pnlCard, 8, fx.Colors.Black)

On B4J it creates a JavaFX CSS dropshadow(gaussian,...).

The implementation calls setStyle, so existing inline styles on the Node can be replaced.


ApplyGlow

jdashboardUtils.ApplyGlow(lblTitle, fx.Colors.Cyan, 15, 0.4)

B4J uses a dropshadow(three-pass-box,...) effect through CSSUtils.


customtextarea

jdashboardUtils.customtextarea( _
    txtDescription, _
    fx.Colors.From32Bit(0xFF101827), _
    fx.Colors.White)

It configures background, text fill, transparent inner background, prompt color and focus styling.


customNode

jdashboardUtils.customNode(txtSearch, fx.Colors.DarkGray, 10)

Adds background styling and rounded border behavior.


SetPanelCornerRadius

jdashboardUtils.SetPanelCornerRadius( _
    pnlCard, _
    14, _
    True, True, False, False)

Boolean order:

TopLeft
TopRight
BottomLeft
BottomRight

Buttons with graphics

ButtonSetImage2

jdashboardUtils.ButtonSetImage2( _
    btnSave, _
    File.DirAssets, _
    "save.png", _
    0xFF2563EB, _
    24dip, _
    "LEFT")

The helper creates a JImageView, assigns it as the JavaFX button graphic, changes the mouse cursor and generates a gradient background.

Position is passed to JavaFX ContentDisplay.valueOf.

Common values include LEFT, RIGHT, TOP, BOTTOM, CENTER, GRAPHIC_ONLY and TEXT_ONLY.


ButtonSeticon2

Dim IconFont As Font = fx.CreateFont("FontAwesome", 18, False, False)

jdashboardUtils.ButtonSeticon2( _
    btnEdit, _
    Chr(0xF044), _
    fx.Colors.White, _
    IconFont, _
    0xFF258A61, _
    "LEFT")

Useful for FontAwesome, Material Icons and similar fonts.


Rich tooltips

ToolTip

Image + wrapped text:

jdashboardUtils.ToolTip( _
    btnInfo, _
    "This action creates a new project.", _
    File.DirAssets, _
    "info.png", _
    32dip, _
    32dip)

ToolTip2

Icon + wrapped text:

jdashboardUtils.ToolTip2( _
    btnUser, _
    "Open user profile", _
    IconFont, _
    Chr(0xF007), _
    fx.Colors.DodgerBlue)

Both helpers internally wrap the text around a logical length of approximately 40 characters.


TableView helpers

Wrapped headers

Dim NativeTable As JavaObject = TableView1
jdashboardUtils.SetTableHeaderWrap(NativeTable, 42dip)

Pass -1 to avoid forcing a preferred header height.


Fixed width

jdashboardUtils.SetTableWidth(TableView1, 150dip)

Automatic equal width

jdashboardUtils.SetAutoTableWidth(TableView1)

Current formula:

(Table.PrefWidth / ColumnsCount) - 5

Call it only after the TableView has at least one column and a useful PrefWidth.


Bitmap and image helpers

FontToBitmap

Dim Icon As B4XBitmap = jdashboardUtils.FontToBitmap( _
    Chr(0xF013), _
    xui.CreateFontAwesome(20), _
    20, _
    xui.Color_White)

The output canvas is 32Γ—32.


CreateRoundBitmap

Dim Avatar As B4XBitmap = jdashboardUtils.CreateRoundBitmap( _
    OriginalBitmap, _
    96dip, _
    xui.Color_White)

Non-square images are center-cropped to a square first. The helper draws a 3dip border.


CreateRoundRectBitmap

Dim Preview As B4XBitmap = jdashboardUtils.CreateRoundRectBitmap( _
    OriginalBitmap, _
    16dip, _
    xui.Color_White)

It keeps the original dimensions and uses a 4dip internal border.


CreateJImageView

Dim Image1 As JImageView = jdashboardUtils.CreateJImageView(200dip, 120dip)

Image1.Load(File.DirAssets, "photo.jpg")
Root.AddView(Image1.mBase, 20dip, 20dip, 200dip, 120dip)

Defaults include FILL_NO_DISTORTIONS and transparent background.

The returned object's mBase must be attached to the visual tree.


Code-created Views

CreateLabel

Dim lbl As B4XView = jdashboardUtils.CreateLabel("Title")
lbl.Text = "Dashboard"
Root.AddView(lbl, 20dip, 20dip, 200dip, 40dip)

Default alignment: CENTER_LEFT.


AddforCode_Customlistview

Private CLV1 As CustomListView

jdashboardUtils.AddforCode_Customlistview( _
    Host, _
    CLV1, _
    "CLV1", _
    True, _
    "Vertical", _
    1dip)

The helper builds the properties normally supplied by the Designer.


Email validation

If jdashboardUtils.ValidateEmail(txtEmail.Text) Then
    Log("Valid")
End If

This is syntax validation only. It does not verify DNS or mailbox existence.


View position

The public API name is intentionally documented exactly as implemented:

ViewScreenPositionLetfButtom
Dim Pos() As Int = jdashboardUtils.ViewScreenPositionLetfButtom(btnMenu)
Log(Pos(0)) 'Left
Log(Pos(1)) 'Bottom

The function walks parent Views and returns approximate screen-relative left/bottom coordinates.


HTML β†’ PDF

jdashboardUI 23.5 declares:

flying-saucer-core-9.1.22.jar
flying-saucer-pdf-itext5-9.1.22.jar
flying-saucer-pdf-9.1.22.jar
itextpdf-5.5.13.2.jar

ConvertHTMLToPDF

Dim HtmlPath As String = File.Combine(File.DirApp, "invoice.html")
Dim PdfPath As String = File.Combine(File.DirApp, "invoice.pdf")

If jdashboardUtils.ConvertHTMLToPDF(HtmlPath, PdfPath) Then
    Log("PDF created")
End If

ConvertTextHTMLToPDF

Dim Html As String = $"<html>
<body>
    <h1>Invoice</h1>
    <p>Generated from B4J.</p>
</body>
</html>"$

Dim PdfPath As String = File.Combine(File.DirApp, "invoice.pdf")

If jdashboardUtils.ConvertTextHTMLToPDF(Html, PdfPath) Then
    Log("PDF created")
End If

Flying Saucer works best with well-formed HTML/XHTML.

Both functions return Boolean and log the exception message on failure.


Complete public API

Data / calculations

valueformated, calcValue, CalculateMedian, calcValue2.

UI / effects

SetShadow, ApplyGlow, customtextarea, customNode, SetPanelCornerRadius, ViewScreenPositionLetfButtom, CreateLabel.

Buttons / tooltips

ButtonSetImage2, ButtonSeticon2, ToolTip, ToolTip2.

Text

MeasureTextWidth, MeasureTextHeight, MeasureTextHeightWithWrap.

Colors

intToHex, HexToInt, colortobackcolor, GetIntContrastColor, GetPaintContrastColor, CompareAndGenerateRandomColor, SetAlphaToIntColor, SetAlphaToPaintColor, rgbaToColor.

TableView

SetTableHeaderWrap, SetTableWidth, SetAutoTableWidth.

Images

FontToBitmap, CreateRoundBitmap, CreateRoundRectBitmap, CreateJImageView.

Other

AddforCode_Customlistview, ValidateEmail.

PDF

ConvertHTMLToPDF, ConvertTextHTMLToPDF.


Best practices

  • Call jdashboardUtils directly; do not instantiate it.
  • Keep SQL table/column names application-controlled.
  • Do not feed untrusted text directly to calcValue2.
  • Ensure a List is non-empty before CalculateMedian.
  • Ensure ColumnsCount > 0 before SetAutoTableWidth.
  • Remember that B4J SetShadow can overwrite inline Node styles.
  • Add JImageView.mBase to the visual tree.
  • Use well-formed HTML/XHTML for PDF conversion and check the returned Boolean.