Results 1 to 2 of 2
  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Question JSlider pointer behaviour

    Hello,
    I am working on a GUI which contains JSlider. I am interested, is it possible for the slider to instantly jump from point to point on mouse clicks on the specific area? If the pointer is in the middle, when I click on the beginning, the pointer to appear there? I`ve had an idea to use setValue() and get mouse coordinates but that seems quite gloomy variant involving huge code block, so I am wondering if there is a better, more elegant solution.

  2. #2
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Default Re: JSlider pointer behaviour

    I`ve solved it... Kind of small math idea. Here is some code if anyone tires to do it.
    Java Code:
    package com.ilian.Quiz;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.plaf.SliderUI;
    
    import java.awt.Toolkit;
    import java.awt.event.*;
    
    
    public class QSlider {
    	private static long[] times = new long[2];
    	private static int index = 0;
    	private static long stime = 0;
    	private static long etime = 0; 
    	public static void main(String[] args) {
    		final JFrame jf = new JFrame();
    		final JPanel jp = new JPanel();
    		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jf.setSize(400,400);
    		Toolkit tk = Toolkit.getDefaultToolkit();
    		int xsize = (int)tk.getScreenSize().getWidth();
    		
    		final JSlider sl = new JSlider(JSlider.HORIZONTAL, 0, 190, 1);
    		sl.setSnapToTicks(true);
    		sl.setSize(100, 20);
    
    		sl.setFocusable(false);
    		sl.addChangeListener(new ChangeListener() {
    
    			@Override
    			public void stateChanged(ChangeEvent arg0) {
    				// TODO Auto-generated method stub
    				System.out.println("Value is:"+sl.getValue());
    			}
    			
    		});	
    		sl.addMouseListener(new MouseListener() {
    
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    					}
    
    			@Override
    			public void mouseEntered(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    	
    			}
    
    			@Override
    			public void mouseExited(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void mousePressed(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				stime = System.currentTimeMillis();
    
    				int x = arg0.getX();
    				System.out.println("X:"+x);
    		
    			}
    
    			@Override
    			public void mouseReleased(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				etime = System.currentTimeMillis();
    				if ( (etime - stime) < 60 ) {
    					
    				}
    				else {
    					sl.setValue(arg0.getX());
    				}
    			}
    			
    		});
    		sl.addMouseMotionListener(new MouseMotionListener() {
    
    			@Override
    			public void mouseDragged(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    	
    			}
    
    			@Override
    			public void mouseMoved(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				/*
    				ChangeEvent cev = new ChangeEvent(this);
    				for (ChangeListener L: sl.getChangeListeners() ) {
    					L.stateChanged(cev);
    				}*/
    			}
    			
    		});
    		jp.add(sl);
    		jf.add(jp); 
    		jf.setVisible(true);
    		
    	}
    
    }

Similar Threads

  1. JSlider problem
    By ktden in forum AWT / Swing
    Replies: 7
    Last Post: 03-29-2013, 08:24 PM
  2. Replies: 1
    Last Post: 12-12-2012, 08:43 PM
  3. Jslider changeListner
    By Basit56 in forum Java Applets
    Replies: 1
    Last Post: 09-14-2009, 04:04 PM
  4. Got Trouble with JSlider
    By hungleon88 in forum Advanced Java
    Replies: 6
    Last Post: 08-30-2008, 05:02 PM
  5. jmf & JSlider
    By nathanr_kamal in forum AWT / Swing
    Replies: 6
    Last Post: 06-28-2008, 07:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •