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

is there any method to do in WebList, #678

Closed wyj3531 closed 2 years ago

wyj3531 commented 2 years ago

jlist item text in center,and Change JList item background color on hover. by code not xml

wyj3531 commented 2 years ago

i use render but not success.

mgarin commented 2 years ago

You can do it the same way you do it with any other L&F - by using a custom list renderer.

If you haven't worked with list/table/tree renderers before I strongly recommend reading official guide first: https://docs.oracle.com/javase/tutorial/uiswing/components/list.html#renderer https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

Idea behind renderers is simple - they're used to optimize list/table/tree performance by "stamping" (painting) the same Swing component over and over again (with different settings) in different cells of the component. This way when you have, let's say, a JList with 1 million values - it will still show up instantly and won't have any impact on application performance since it doesn't have to create 1 million labels, but just 1 - for rendering each of those million cells.

I do have a custom renderer in WebLaF which I recommend reusing for custom ones. It also has some methods separated for convenience, including one for alignment. Here is how you can do it using WebListCellRenderer:

list.setCellRenderer ( new WebListCellRenderer ()
{
    @Override
    protected int horizontalAlignmentForValue ( final ListCellParameters parameters )
    {
        return SwingConstants.CENTER;
    }
} );

There are a few key differences between WebListCellRenderer and default Swing renderer:

Hope this helps.

mgarin commented 2 years ago

i use render but not success.

Just add to the previous answer - You might be trying to change alignment before renderer changes it, this way it will obviously not have any effect. If you can provide the code you were trying to use - I can tell where the issue was.