mgarin / weblaf

WebLaF is a fully open-source Look & Feel and component library written in pure Java for cross-platform desktop Swing applications.
http://weblookandfeel.com
GNU General Public License v3.0
1.13k stars 234 forks source link

How to center text in WebComboBox ? #646

Closed mokun closed 3 years ago

mokun commented 3 years ago

I've been trying to center the string text of the elements in WebComboBox but can't.

image

It's left aligned by default.

If the elements are objects and not strings, does it make it harder to do so ?

Appreciate for any hints !

mgarin commented 3 years ago

It doesn't matter what kind of objects you are using as combobox data. Visual representation is always defined by the renderer, so you can adjust anything you want through it. This part is coming from Swing: https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

If you don't want to break the default view - you can simply use default WebLaF renderer and provide custom settings in it:

comboBox.setRenderer ( new WebComboBoxRenderer ()
{
    @Override
    protected int horizontalAlignmentForValue ( final ListCellParameters parameters )
    {
        return JLabel.CENTER;
    }
} );

You can also provide this and a lot of other visual settings through a custom style for the renderer component, you can look at the default combobox.xml style to see where those are located:

<!-- Default cell renderer label -->
<style type="styledlabel" id="renderer" />

<!-- Selected value renderer label -->
<style type="styledlabel" id="box-renderer" extends="renderer" padding="2,4,2,4" />

<!-- Popup list cell renderer label -->
<style type="styledlabel" id="list-renderer" extends="renderer" padding="4,6,4,6" />

Style box-renderer is used for the value in the combobox itself, style list-renderer is used for values displayed in the combobox popup list. As you can see those are simply default WebStyledLabel styles with some padding applied.

mokun commented 3 years ago

I see. It works now. Thanks !