Closed wyj3531 closed 2 years ago
i use render but not success.
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:
WebListCellRenderer
uses WebStyledLabel
as base renderer componentWebListCellRenderer
has a custom style that can be modified via skinWebListCellRenderer
has a few methods that can be overridden for convenient modification of rendererWebListCellRenderer
has generics for item value type, JList
type and ListCellParameters
ListCellParameters
is simply used to unify all the different information about the rendered cellListCellParameters
contains list, value, value index and whether it is selected or focusedListCellParameters
can also be overridden to provide additional information if necessaryHope this helps.
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.
jlist item text in center,and Change JList item background color on hover. by code not xml