*
The moose likes Swing / AWT / SWT and the fly likes JSlider Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSlider " Watch "JSlider " New topic
Author

JSlider

Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Hi,
At the beginning I paste simple image.


My question is there a chance to change these three elements ?

I want to add main own paintTrack and the element marked number one in this image.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Those are part of the UI. You can create a new SliderUI class and install that for the JSlider. It's not going to be easy though.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Maybe somebody has some example ?
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
What do you think about to create panel which background imitating paintTrack and adding to it some custom for example circle Graphics. Next i can add to my Graphics the mouseListener and write code to move this graphics this will imitate JSlider behavior.

but i don't know how this solutions will affect the application performance.

What do you think about this ?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

No, don't reinvent JSlider. Extend the UI delegate of your chosen L&F and override paintThumb and paintTrack. Start with the JDK code, eliminate or substitute any private variables and/or method calls that aren't available in a subclass and then tweak it from there.

For example, for the default Metal L&F tou would extend MetalSliderUI. In case you didn't know, you can find the JDK source in the file src.zip in your JDK directory.

Why do you need to do this anyways?


luck, db
There are no new questions, but there may be new answers.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
I just want to create Slider with my own graphics. I write this but the slider doesn't displayed. I really don't know how i should to started with this UI.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Setting properties of a Graphics reference doesn't paint anything. Did you go through the code in the same methods of MetlaSliderUI?

Time for a couple of tutorials:
http://download.oracle.com/javase/tutorial/uiswing/painting/index.html
http://download.oracle.com/javase/tutorial/2d/index.html

For the thumb Icon, you can create an Icon and set it usingSee the Icon interface for the three methods you have to implement.

You didn't answer this:
Why do you need to do this anyways?
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Darryl look at this photo, i want to create something like this so i need t change those three elements but question is how ?

1. Standard JSlider
2. look who i wants to achieve


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
I believe you need to add code like the following to your MSlider class:



Then you can add System.out.println(...) statements to your paintThumb() and paintTracks() methods to make sure they are being invoked.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Good luck developing this further for flexibility of usage. Oh, and you owe me a beer.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Thank you very much guys )
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Darryl the code that you pasted was created with using some program or you wrote it yourself ?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

I don't know of any program that can generate UI customization code. Here are the steps I followed, so that next time round you can do this yourself.

1. Examined the Slider properties that can be set via the UIManager using Rob Camick's UIManager Defaults.

2. Attempt to customize those properties: succeeded for the 3 properties as in the 3 UIManager.put(...) statements in the code. Failed for Slider.trackWidth (default value: 7).

3. Developed an Icon using the Graphics2D API (Note: except in the earliest versions of Java, all Graphics objects are instances of Graphics2D) and a suitable Paint.

4. Examined the code of MetalSliderUI and BasicSliderUI. Found that trackWidth is read but never used. (!)

5. Examined the paintTrack(...) method in both those classes. Decided the one in BasicSliderUI was adequate for the desired customizing.

6. Utilized the to Graphics2D API to draw the track using a suitable Stroke.

7. Increased the preferredSize to match the size of the posted image.

The colors were taken from the image you posted, using the hex values obtained via the color picker in a graphics editing program. Since your image was a JPEG, some compression artifacts made it impossible to guess the true original color. A PNG might have been better: lossless compression.

You still haven't told me why exactly you required this customization.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Thank you again Darryl. The reason why i need this customization is simple. I create some program and i want to add my own graphics. I couldn't do it with standard JSlider method so render my own JSlider is the only way to do this.
 
jQuery in Action, 2nd edition
 
subject: JSlider