Skip to content

How to use WebLink

Mikle edited this page Jan 30, 2020 · 4 revisions

Available since WebLaF v1.2.9 release, updated for WebLaF v1.2.12 release
Requires weblaf-ui module and Java 6 update 30 or any later to run


What is it for?

The main focus of WebLink is to provide hyperlink-like UI element that mimics most common hyperlink behaviors and view. Its view can be easily modified through the styles and its behavior can be adjusted through custom LinkAction implementations.


What should I know about it?

WebLink is a quite simple component which only has two extra fields on top of WebStyledLabel class, which WebLink extends:

  • visitable - whether or not link can become visited
  • visited - whether or not link have been visited once at the current runtime

Both of these fields do not affect link behavior, but only its visual appearance - visitable & visited link gains additional visited decoration state, unvisitable or unvisited link gains unvisited decoration state.

One more important thing is that WebLink uses LinkAction interface implementations to handle actions on link click. There is also an AsyncLinkAction abstract class available for any asynchronous link actions execution.

If LinkAction implementation will also implement IconSupport and/or TitleSupport - those will be used to provide link icon and/or text, but only if none of those are specified explicitly.

There are a few existing basic LinkAction implementations:

  • UrlLinkAction - opens specified URL in default system internet browser
  • EmailLinkAction - opens new email composition window for the specified email in default system mail agent
  • FileLinkAction - opens specified folder in native file system browser or a file in associated application

You might use them directly or extend upon to create new actions. Note that all those implementations also implement IconSupport and TitleSupport, so if you will not provide icon or text into your link first you will receive both from the basic action.


How to use it?

There are multiple WebLink constructors available with most common combinations of settings, you can either use those or provide all values separately. I personally prefer using constructors:

final WebLink link = new WebLink ( new EmailLinkAction ( "mgarin@alee.com" ) );

This will create a simple link with all necessary settings provided by EmailLinkAction:

WebLink

By providing either custom text and/or icon you will disable the default ones:

final WebLink link = new WebLink ( "Write to me!", new EmailLinkAction ( "mgarin@alee.com" ) );

Resulting in a different visual representation:

WebLink

Assigning custom link action (or actions) is as simple as adding an ActionListener to any button:

final WebLink link = new WebLink ( "Show notification" );
link.addAction ( new LinkAction ()
{
    @Override
    public void linkExecuted ( final ActionEvent event )
    {
        NotificationManager.showNotification ( "Link have been visited!" );
    }
} );

You can always add multiple actions to the same link if you want to perform multiple actions upon clicking the link.


How to style it?

If you are new to WebLaF styling system - I recommend reading Styling Introduction wiki article first. It explains how styling works in WebLaF and has a bunch of code examples for you to try out.

Now, regarding WebLink - even though it isn't a lot different from a common WebStyledLabel visually, it comes with quite complex default style which defines a few important visual behaviors I would like to mention.

It takes advantage of AbstractStyledTextContent globalStyle and ignoreStyleColors settings.

  • globalStyle defines additional text styles applies to the whole text
  • ignoreStyleColors allows disabling customized colors forcing text to get colored with foreground

These settings are used to provide hover underline style by default with an always-base color on hover:

<decoration states="hover">
    <LabelLayout>
        <StyledLabelText ignoreStyleColors="true" globalStyle="u" />
    </LabelLayout>
</decoration>

You can read full description of the style syntax format here.

It uses custom DashFocusContent to represent focused state.

It isn't that interesting, but still a custom thing made for WebLink specifically. Different DashFocusContent settings are used for unvisited and visited states:

<decoration states="enabled,focused">
    <DashFocus bounds="margin" stroke="basic;1;round;round;0;2,2" color="0,0,200" />
</decoration>
<decoration states="visited,enabled,focused">
    <DashFocus color="100,0,0" />
</decoration>

It uses different color for the visited state.

That state can only exist if link is visitable and was visited once:

<decoration states="visited">
    <LabelLayout>
        <StyledLabelText color="100,0,0" />
    </LabelLayout>
</decoration>

These are the specifics of WebLink default styling which are important to know and take into account when modifying default or creating new style for WebLink, rest of the style settings were taken from WebStyledLabel default style and only provide some default text view for normal and disabled states.