Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: problems with customising a JSlider

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problems with customising a JSlider

    Recommend posting an SSCCE to better describe the problem and help troubleshoot for a solution

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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.
    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();
    	}
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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?

Similar Threads

  1. MP3 problems
    By relion65 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 15th, 2011, 12:09 PM
  2. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  3. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  4. How to Use a JSlider - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 29th, 2010, 09:33 AM
  5. How to Use a JSlider - Java Swing
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: March 29th, 2010, 09:33 AM