[SOLVED] Bug in WebMultilineLabel

Discuss here any questions about the WebLaF library.
Bugs can be posted directly to GitHub issues tracker.

[SOLVED] Bug in WebMultilineLabel

Postby byr » Tue Nov 24, 2015 8:03 am

Hi!

I think I found a bug in the WebMultilineLabel. The problem I had is that if I add an icon to the Label, the text is written from the left side of the label, not taking the Icon in account. I checked the code and did some debugging, and it seems, that in the WebMultilineLabelUI when the text is painted, the Icon width is no longer considered, and in case of LEFT alignment, the text is started to pain from position X = 0.

The problem is in the paintTextLines method around line 170:

Code: Select all
    protected void paintTextLines(Graphics g, JLabel label, FontMetrics fm) {
        List lines = this.getTextLines(label);
        int height = this.getAvailableHeight(label);

        int textHeight;
        for(textHeight = lines.size() * fm.getHeight(); textHeight > height; textHeight -= fm.getHeight()) {
            ;
        }

        paintTextR.height = Math.min(textHeight, height);
        paintTextR.y = this.alignmentY(label, fm, paintTextR);
        int textX = paintTextR.x; // this returns 52pix (width of my image + 4 pix gap)
        int textY = paintTextR.y;

        for(Iterator it = lines.iterator(); it.hasNext() && paintTextR.contains(textX, textY + getAscent(fm)); textY += fm.getHeight()) {
            String text = ((String)it.next()).trim();
            if(it.hasNext() && !paintTextR.contains(textX, textY + fm.getHeight() + getAscent(fm))) {
                text = this.clip(text);
            }

            int x = this.alignmentX(label, fm, text); // this returns 0, left edge of the component
            if(label.isEnabled()) {
                this.paintEnabledText(label, g, text, x, textY); // written to X=0 instead of X=52
            } else {
                this.paintDisabledText(label, g, text, x, textY); // same as above
            }
        }
    }


Here at the first line the paintTextR.x is saved, but at the end, when the label is painted, the textX is no logner used, and the alignmentX returns 0 in case of LEFT alignment, causing the text overwriting the Icon. Probably the alignmentX method would need also the textX as parameter, since in case of CENTER justify it should know the size of the Icon.

Thanks!

Regards

byr
byr
 
Posts: 3
Joined: Tue Nov 24, 2015 7:52 am

Re: Bug in WebMultilineLabel

Postby byr » Tue Nov 24, 2015 12:15 pm

Hi!

I downloaded the source code, and I might found a fix:

In the WebMultiLineLabelUI class, method alignmentX instead of paintViewR the paintTextR should be used (since the paintTextR already narrowed down with the possible icon width/height, however the paintViewR is the size of the whole component). If the justification is not LEFT, we need to adjust the returned X coordinate with the X of the paintTextR rectangle:

Code: Select all
protected int alignmentX ( final JLabel label, final FontMetrics fm, final String s )
    {
        final boolean ltr = label.getComponentOrientation ().isLeftToRight ();
        final int align = label.getHorizontalAlignment ();
        if ( align == JLabel.RIGHT || align == JLabel.TRAILING && ltr ||
                align == JLabel.LEADING && !ltr )
        {
            return paintTextR.x + (paintTextR.width - fm.stringWidth ( s ));
        }
        else if ( align == JLabel.CENTER )
        {
            return paintTextR.x + (paintTextR.width / 2 - fm.stringWidth ( s ) / 2);
        }
        else
        {
            return paintTextR.x;
        }
    }


With this modification, the label looks right with all justification, and the icon is positioned properly before the text.

Thanks

Regards,

byr
byr
 
Posts: 3
Joined: Tue Nov 24, 2015 7:52 am

Re: Bug in WebMultilineLabel

Postby mgarin » Tue Nov 24, 2015 6:00 pm

Good day,

Thanks for finding the issue and providing a fix! :)
I have added it into the pre-release code and have uploaded a fresh v1.29 pre-release build on the site:
http://weblookandfeel.com/downloads/prerelease/

Unfortunately WebMultilineLabel component will be removed with the next update (v1.29) and will be completely replaced by WebStyledLabel which has all of its functions and more. It also doesn't have the issue you've shown here as it was completely rewritten and works differently.

WebMultilineLabel implementation is a pretty old piece of code which was offered as a solution years ago. A pretty outdated and buggy one I should say.
User avatar
mgarin
Site Admin
 
Posts: 223
Joined: Mon Apr 16, 2012 1:01 pm
Location: Russia, Saint-Petersburg

Re: Bug in WebMultilineLabel

Postby mgarin » Tue Nov 24, 2015 6:04 pm

I'll also mark this issue solved :)
User avatar
mgarin
Site Admin
 
Posts: 223
Joined: Mon Apr 16, 2012 1:01 pm
Location: Russia, Saint-Petersburg

Re: [SOLVED] Bug in WebMultilineLabel

Postby byr » Tue Nov 24, 2015 7:19 pm

Hi!

You're welcome :)

This is the best UI library I saw for swing so far, so keep up the good work :)

Looking forward for the new version with styling!

Regards

byr
byr
 
Posts: 3
Joined: Tue Nov 24, 2015 7:52 am


Return to Questions

Who is online

Users browsing this forum: No registered users and 1 guest

cron