problems with customising a JSlider
I'm trying to modify the look of a JSlider and so far I've managed to do what I want, but 2 problems remain.
- When I get anywhere near it with the mouse, the slider get's highlighted. I want to turn of highlighting but i can't find how to do that anywhere.
- The slider is vertical and when the thumb is on the bottom of the track, part of the thumb falls outside of the drawing area. The customised thumb has the exact same size as the 'thumbRect' variable so I don't know why this would happen. Is making the thumb smaller the only option to fix this?
Re: problems with customising a JSlider
Recommend posting an SSCCE to better describe the problem and help troubleshoot for a solution
Re: problems with customising a JSlider
Well, I found the first problem thanks to the SSCCE, I forgot to reset the basicStroke before drawing the slider. The other problem is still there though, if you drag the thumb all the way down it get's cut off.
Code :
package Test;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.plaf.basic.BasicSliderUI;
public class SSCCE
{
public SSCCE()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.getContentPane().setBackground(Color.BLACK);
frame.getContentPane().setPreferredSize( new Dimension(100,200) );
JSlider slider = new JSlider(SwingConstants.VERTICAL,1,20,10);
slider.setUI( new displaySliderUI(slider) );
int x = 50;
int y = 25;
int width = 25;
int height = 150;
slider.setBounds(x,y,width,height);
slider.setOpaque(false);
frame.add(slider);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
SSCCE s = new SSCCE();
}
}
class displaySliderUI extends BasicSliderUI
{
private Color bordercolour = Color.YELLOW;
private Color fillcolour = Color.BLACK;
private JSlider slider;
public displaySliderUI(JSlider s)
{
super(s);
slider = s;
}
public void paintFocus(Graphics g)
{
//do nothing
}
public void paintTrack(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Rectangle r = trackRect;
g2.setColor(fillcolour);
int x = (r.width/2)-1;
g2.fill( new Rectangle2D.Double(x,r.y,6,r.height) );
g2.setColor(bordercolour);
g2.draw( new Rectangle2D.Double(x,r.y,6,r.height) );
}
public void paintThumb(Graphics g)
{
paintTrack(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle r = thumbRect;
g2.setColor(fillcolour);
g2.fill(r);
g2.setColor(bordercolour);
g2.draw(r);
slider.repaint();
}
}
Re: problems with customising a JSlider
This seems to work for me. Could be an operating system issue? What OS are you running this on?