Question

I am interested in creating a new widget similar to the JSlider widget, but with a "ghost" (translucent) knob displaying the previous slider position, as well as adding a trails-like animation to the real knob's movement.

I am a bit confused on the proper way of extending a Java Swing widget to add new functionality.

As I see it I have a few options:

1) Extend JSlider, place all my new model information here, and overwrite paint to first draw the JSlider, and than to overlay my desired additions. The problem with this solution is I would need to use trial and error to get the marks in the correct position (as I won't have access to full geometrical information) which will could make it not work in all situations.

2) Extend various classes of the Slider widget (JSlider, SliderUI, BasicSliderUI, MetalSliderUI, BoundedRangeModel, DefaultBoundedRangeModel). The advantage I see here is maintaining the proper model-view-controller architecture. However, this will require overloading various functions within the classes. I believe the result would seem very hacked together.

3) Copying all of the Slider widget's code, and modifying to create my new widget. This would be similar to options (2), but may be a bit simpler to modify code then to extend functions (which will be basically copying/modifying code anyways).

4) Re-create the slider widget from scratch with my desired functionality. After looking at the existing Swing Slider widget's code, this is not a trivial task.

Am I missing some more elegant method of creating a new widget that borrows functionality from an existing one?

Thank you for your insight.

Was it helpful?

Solution

I would choose 2) with some changes Create WrapperSliderUI class which delegates all the methods calls to the delegate UI. And override just paint method.

SetUI of your JSlider should wrap original UI in the WrapperSliderUI

public void setUI(SliderUI ui) {
    super.setUI(new WrapperSliderUI(ui));
}

The in the paint you will check original UI class and adapt your painting accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top