Closed wyj3531 closed 2 years ago
It depends on what you actually want to do.
If your intention is to customize how tree looks - you don't really need to create a custom ComponentUI
with WebLaF, it is sufficient to create a custom Painter
for a specific tree part (background/node background/selection background/drop location/selector) and provide it in the tree style in a custom skin or skin extension.
In a lot of cases you might not even need to provide a custom Painter
because existing ones allow you to flexibly customize the look of the component directly via the style, without the need to temper with the Painter
or ComponentUI
implementation code.
To understand how skins and styles work, I recommend reading this wiki guide: https://github.com/mgarin/weblaf/wiki/Styling-introduction It should explain most things and it provides an example on how you can make custom styles.
From there - all you need is to create a custom tree style which you can make based on the default light skin style here: https://github.com/mgarin/weblaf/blob/master/modules/ui/src/com/alee/skin/light/resources/tree.xml
If your intention is to actually customize how TreeUI
implementation works - you can register a custom descriptor for any existing Swing and custom WebLaF component via StyleManager
:
StyleManager.registerComponentDescriptor ( new CustomTreeDescriptor () );
You would need to do that before initializing L&F.
Descriptor contains all important information about component required for all of the pieces - JComponent
, ComponentUI
and Painter
- to work properly together. Here is an example of default JTree
descriptor:
https://github.com/mgarin/weblaf/blob/master/modules/ui/src/com/alee/laf/tree/TreeDescriptor.java
It can be customized to override default ComponentUI
and/or Painter
implementations for all components of the specific type. Meanwhile the other solution I mentioned earlier - providing custom Painter
via skin and style - will only modify it for the components using the specific style. Either approach can be used depending on which outcome you want to have.
thank you ,i use ExtensionSkin method.but three is some problem,i use a new extensionskin in demo peoject ,it do work;
but when i use it in my project(dependency weblaf-ui) it does not work. i copy demo project files where in package com.alee.demo.skin
but i does not work to. the following pic .what is worng when i use it in my new project.
the my-adaptive-extension.xml file is
<skin xmlns="http://weblookandfeel.com/XmlSkinExtension">
<!-- Extension settings -->
<id>weblaf.demo.myadaptive.extension</id>
<class>com.alee.demo.skin.MyAdaptiveExtension</class>
<extends>weblaf.light.skin</extends>
<extends>weblaf.dark.skin</extends>
</skin>
and the MyAdaptiveExtension file
public class MyAdaptiveExtension extends XmlSkinExtension
{
public MyAdaptiveExtension()
{
super ( new ClassResource ( MyAdaptiveExtension.class, "resources/my-adaptive-extension.xml" ) );
}
}
Judging by the visible piece of stack trace - application seem to be unable to find XML file or the URL to the XML file is invalid.
I had a similar issue on some versions of IntelliJ Idea that didn't copy resources to the output folder which resulted in them not being found when you would try running application from IDE, but it would work once you pack it into a JAR because resources would be properly packed into it. Can you check that those XML extension files do get into IDE output?
That issue appeared in IntelliJ Idea around the end of last year when they did some changes related to Maven projects and I personally didn't find a way to solve it up until now. It does work on some of the PCs I'm using and doesn't on the others, so I'm not even sure what's the source of this issue. So I just made a small script to copy resource files into output directory and added it into the run sequence.
If that doesn't help - could you provide a complete stack trace of the error?
Also a small edit for my initial answer regarding descriptor registration:
You would need to do that before initializing L&F.
You actually want to do it AFTER L&F initialization. I've noticed that after digging a bit more into one of the recent issues: #673
em,my new project pom.file add resource path has fix this problem.
`
</resources>`
Dear, your resource is not loaded, so the XML cannot be found
You need to load your own class files and resources in the Java virtual machine
If it's a Maven project you need to add
the new paitner :MyPainter the new ui:MyTreeUi extends WebTreeUI how it work?