Java Swing Range Slider U.I

為{幸葍}努か 提交于 2019-12-10 17:27:10

问题


I needed a slider with two knobs on it (representing a range) and I found this nifty one here. However, they created their own U.I. which extends Java's BasicSliderUI. They override the paint method to draw their own knobs. I would like to use the default knobs based on the current look and feel. I tried calling BasicSliderUI's paintThumb method, but this gives me a generic looking knob which doesn't seem to be tied to the current look and feel. As far as I can tell, JSlider gets its U.I. from MultiSliderUI. How can I create a U.I which changes like JSlider's, but draws two knobs instead of one?

Thanks in advance!


回答1:


SwingX provides JXMultiThumbSlider which may be what you are looking for.




回答2:


Try getting the current look and feel from the javax.swing.UIManager and calling getUI(new JSlider()) on it. This should return the current UI used for the JSlider, which should be castable to JSliderUI.

Unfortunately JSliderUI doesn't have a paintThumb(Graphics g) method, but the BasicSliderUI does, and at least on Windows XP and Windows Vista the system look and feel is a subclass of BasicSliderUI, so is the Metal look and feel.

So once you have the JSliderUI see if it is an instance of BasicSliderUI, if so you can cast it to BasicSliderUI and use the paintThumb(Graphics g) on it, otherwise default to the way you are doing it already. I don't know if this actually works, but it would be my first attempt if I needed it.

I don't have Linux or Mac to check the source if their JSliderUIs are derived from BasicSliderUI as well, but if you do then check out the java source code.




回答3:


You can use RangeSlider class from Jide Commons Layer

This is opensource.



来源:https://stackoverflow.com/questions/1140303/java-swing-range-slider-u-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!