JSlider Middle Mouse Button Handler

I've created a JSlider with a range of 1 to 15, with steps of 1 when either the left or middle
mouse button is clicked on the slider bar.
I need to have the left mouse button behave that way, but when the operator clicks the
middle mouse button on the slider, I need to have the slider "jump" to that position - not
just increment the slider by 1.
I've seen mouse handlers deal with the middle mouse button, but not in this way.

I'm looking for ideas - or better yet an example...
dbCncAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dbCncAuthor Commented:
Attached is my code snippet to show what listeners I have...

Thanks
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.BoundedRangeModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
 
// A slider that goes from 1 to 16 - if the value is 16, an aditional
// text string is displayed...
public class SliderPopup extends JDialog {
 
	private static final long serialVersionUID = 1L;
 
	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SliderPopup dialog = new SliderPopup();
					dialog.addWindowListener(new WindowAdapter() {
						@Override
						public void windowClosing(WindowEvent e) {
							System.exit(0);
						}
					});
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
	// in the real application, there is a global database with getters &
	// setters
	int hackGlobalSliderValue = 1;
 
	private JTextField textScanLabel;
 
	// in the real application you get to play with this slider - but when you
	// select OK the global value is set; if you select Cancel, the global value is
	// reset to what it was when this popup popped up...
	int localSliderValue;
	int savedSliderValue;
 
	/**
	 * Create the dialog
	 */
	public SliderPopup() {
		super();
 
		setAlwaysOnTop(true);
		localSliderValue = 1;
		savedSliderValue = 1;
 
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setTitle("SliderTest");
		getContentPane().setLayout(null);
		setName("SliderTestDlg");
		getContentPane().setName("SliderTestContentPanel");
		getContentPane().setBackground(Color.LIGHT_GRAY);
		setBounds(908, 80, 375, 150);
 
		textScanLabel = new JTextField();
		textScanLabel.setFont(new Font("", Font.BOLD, 12));
		textScanLabel.setEditable(false);
		textScanLabel.setText(String.valueOf(hackGlobalSliderValue));
		textScanLabel.setBackground(Color.LIGHT_GRAY);
		textScanLabel.setBounds(95, 10, 26, 20);
		getContentPane().add(textScanLabel);
 
		final JLabel maxPersistLabel = new JLabel();
		maxPersistLabel.setBounds(168, 12, 183, 16);
		maxPersistLabel.setText("");
		getContentPane().add(maxPersistLabel);
 
		final JSlider sliderJSlider = new JSlider();
 
		sliderJSlider.getModel().addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				BoundedRangeModel s = (BoundedRangeModel) e.getSource();
				textScanLabel.setText(String.valueOf(s.getValue()));
			}
		});
 
		sliderJSlider.setValueIsAdjusting(true);
		sliderJSlider.setValue(1);
		sliderJSlider.setMajorTickSpacing(3);
		sliderJSlider.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(final MouseEvent e) {
				localSliderValue = sliderJSlider.getValue();
				// will be 1-16 only
				if (localSliderValue == 16) {
					textScanLabel.setText("16");
					maxPersistLabel.setText("(Infinite SliderTest)");
					// TODO do something here...
				} else {
					textScanLabel.setText(String.valueOf(localSliderValue));
					maxPersistLabel.setText("");
					// TODO do something else here...
				}
			}
		});
		sliderJSlider.setMinimum(1);
		sliderJSlider.setPaintLabels(true);
		sliderJSlider.setForeground(Color.BLACK);
		sliderJSlider.setPaintTicks(true);
		sliderJSlider.setMaximum(17);
		sliderJSlider.setExtent(1);
		sliderJSlider.setBackground(Color.LIGHT_GRAY);
		sliderJSlider.setBounds(31, 32, 320, 43);
 
		// Set minor tick marks 
		int tickSpacing = 1;
		sliderJSlider.setMinorTickSpacing(tickSpacing);
		getContentPane().add(sliderJSlider);
 
		final JButton okButton = new JButton();
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				// set the database to the new current value
				hackGlobalSliderValue = localSliderValue;
				System.exit(0);
			}
		});
		okButton.setText("OK");
		okButton.setBounds(56, 83, 106, 26);
		getContentPane().add(okButton);
 
		final JButton cancelButton = new JButton();
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				// reset the database with the same value that we entered the
				// dialog box with
				hackGlobalSliderValue = savedSliderValue;
				System.exit(0);
			}
		});
		cancelButton.setText("Cancel");
		cancelButton.setBounds(218, 83, 106, 26);
		getContentPane().add(cancelButton);
 
		final JLabel scansLabel = new JLabel();
		scansLabel.setText("Scans");
		scansLabel.setBounds(125, 12, 50, 16);
		getContentPane().add(scansLabel);
 
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowActivated(final WindowEvent e) {
				// preset the values - note that this also gets called when the
				// popup gets focus again - usually a bad thing...
				localSliderValue = hackGlobalSliderValue;
				savedSliderValue = hackGlobalSliderValue;
 
				if ((localSliderValue == 0) || (localSliderValue == 16)) {
					sliderJSlider.setValue(16);
					textScanLabel.setText("16");
					maxPersistLabel.setText("(Infinite SliderTest)");
				} else {
					sliderJSlider.setValue(localSliderValue);
					textScanLabel.setText(String.valueOf(localSliderValue));
					maxPersistLabel.setText("");
				}
			}
		});
	}
}

Open in new window

0
dbCncAuthor Commented:
After quite a bit of digging, I have something that works for me.
Note that the math is layout specific...

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JSlider;
 
public class Test2 {
	static JSlider js;
 
	public static void main(String[] args) {
 
		JFrame f = new JFrame();
		js = new JSlider(0, 100, 0);
		js.setMajorTickSpacing(20);
		js.setMinorTickSpacing(10);
		js.setPreferredSize(new Dimension(300, 50));
		js.setPaintLabels(true);
		js.setPaintTicks(true);
		//js.setSnapToTicks(true);
		f.add(js);
		js.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {}
 
			public void mouseEntered(MouseEvent arg0) {}
 
			public void mouseExited(MouseEvent arg0) {}
 
			public void mouseReleased(MouseEvent arg0) {}
 
			public void mousePressed(MouseEvent e) {
				
				int modifier = e.getModifiers();
 
				if((modifier & MouseEvent.BUTTON1_MASK)!=0){
				//left button is pressed
				}
				if((modifier & MouseEvent.BUTTON2_MASK)!=0){
				//middle button is pressed - warp to that value
					int w = js.getWidth() - 20;
					/*the standard slider starts and ends 10 pixels away from 
					 * the east/west border - when using this layout...*/
					int range = js.getMaximum() - js.getMinimum();
					int x = e.getX() - 10;
					js.setValue(js.getMinimum() + range * x / w);
				}
				if((modifier & MouseEvent.BUTTON3_MASK)!=0){
				//right button is pressed
				}
			}
		});
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		f.pack();
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.