Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By jim829

Thread: Move JSlider per click

  1. #1
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Move JSlider per click

    Hi everyone,

    Can somebody shed some light on how can I move the JSlider just by clicking on it without dragging. I have looked up the official tutorial and API documentation.

    Appreciate it very much and thanks for you time.

    Bascially it is enough just point the direction ,with which method?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: Move JSlider per click

    You'll need to provide a better description of what you want. Do you want to actually reposition the slider in its parent component?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Move JSlider per click

    add a mouse listener in mousePressed(MouseEvent ev) { yourslider.setValue(ev.getX(); } I am working on the same problem

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: Move JSlider per click

    Ah, I didn't quite understand what you were talking about. It already does that. Just click on the bar.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: Move JSlider per click

    Thank you heatblazer, your suggestion is really helpful. Now my Slider has reaction to mouse click, but not yet correctly.
    I have implemented in this way:

    Java Code:
    public class test implements MouseListener {
    	
    	JSlider slider;
    	
    	public static void main(String[] args){
    		
    		JFrame f=new JFrame();
    		JPanel p=(JPanel) f.getContentPane();
    		p.setLayout(new GridBagLayout());
    	
    		
    		
    		int FPS_MIN = 0;
    		int FPS_MAX = 100;
    		int FPS_INIT = 0;    
    		
    		JSlider slider = new JSlider(JSlider.HORIZONTAL,FPS_MIN, FPS_MAX, FPS_INIT);
    		slider.setMajorTickSpacing(10);
    		slider.setMinorTickSpacing(100);
    	
    
    		test t=new test();
    		Hashtable labelTable = new Hashtable();
    		slider.addMouseListener(t);
    		t.slider=slider;
    		
    		GridBagConstraints c=new GridBagConstraints();
    		c.weightx=1;
    		c.weighty=1;
    		c.fill=GridBagConstraints.HORIZONTAL;
    		p.add(slider,c);
    		f.setSize(800,800);
    		f.setVisible(true);
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		JSlider sourceSlider=(JSlider)e.getSource();
    		System.out.println(e.getX());
    		this.slider.setValue(e.getX());
    		
    	}
    
    	@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
    		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    }
    May be you will have some ideas on why I get always a large number back for X values by using getX( ). Shouldn't it return the relative X value to the component(here slider) as described in the documentation?
    Thanks :)
    Last edited by tianchen; 01-16-2014 at 11:48 AM.

  6. #6
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: Move JSlider per click

    Thank you for your time Jim. Sorry for the vague description. What I really want is to set the value of JSlider just by clicking on the slider. I have added the codes on the reply to heatblazer.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,877
    Rep Power
    5

    Default Re: Move JSlider per click

    Quote Originally Posted by tianchen View Post
    Thank you for your time Jim. Sorry for the vague description. What I really want is to set the value of JSlider just by clicking on the slider. I have added the codes on the reply to heatblazer.
    Uh okay, but now you repeat almost exactly the same problem description. Do note that repeating the same thing does not make it more clear.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: Move JSlider per click

    Uhm.... then I do not know how to say it. I try another time then.

    Normally you have a JSlider and you can only set its value(move cursor to a certain position on the slider bar) by dragging the cursor along the slider bar to the position you want, is that right?
    And what I want is just to move the cursor to that position just by clicking on the slider bar. without dragging the cursor :)

  9. #9
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Move JSlider per click

    My suggestion is that your JSlider is na JPanel with setPreferredSize() to the JSlider ticks you need. So if 100 then the preferredSize to be 100 too. Then arg0.getX() will return where the mouse was clicked and update on the same place you`ve clicked it.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Move JSlider per click

    You need to use the slider UI to translate the mouse point to the slider value:

    Java Code:
    @Override
    public void mouseClicked(MouseEvent e)
    {
        JSlider sourceSlider=(JSlider)e.getSource();
        BasicSliderUI ui = (BasicSliderUI)sourceSlider.getUI();
        int value = ui.valueForXPosition( e.getX() );
        slider.setValue(value);
    }

  11. #11
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: Move JSlider per click

    Yep thank you heatblazer

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Move JSlider per click

    I hope you didn't set the preferred size of the panel or slider to solve your problem. That is NOT the way to fix the problem. You should never hard code a preferred size like that.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: Move JSlider per click

    Quote Originally Posted by camickr View Post
    You should never hard code a preferred size like that.
    I disagree. Setting the preferredSize (or Minimum or Maximum size) just provides hints for the layout managers to do their job. And sometimes you absolutely must use it or your application will not display correctly.

    Regards,
    Jim
    heatblazer likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Move JSlider per click

    I agree the term "never" should not have been used. I was attempting to stress that playing with the preferred size was not the solution in this case and is rarely required in other cases either.

    However, if you feel you need to override a default size calculation then you should be overriding the getPreferredSize() method (and related min/max methods).

  15. #15
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: Move JSlider per click

    Thank you camickr :) got it

  16. #16
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Move JSlider per click

    I don`t see why. In the 2 cases I`ve used - your suggestion and mine works the same. And besides, sometimes JPanel must be set properly.

Similar Threads

  1. Replies: 12
    Last Post: 07-25-2013, 06:55 AM
  2. Can't get key click to move robot
    By DesertFlower in forum New To Java
    Replies: 1
    Last Post: 12-07-2012, 08:54 AM
  3. Replies: 3
    Last Post: 07-04-2011, 12:50 PM
  4. Replies: 2
    Last Post: 06-22-2011, 01:19 PM
  5. Problem with click to move ball algorithm
    By Laythe in forum Java Applets
    Replies: 1
    Last Post: 12-19-2009, 12:00 AM

Posting Permissions

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