Solved

JSlider Middle Mouse Button Handler

Posted on 2008-10-20
4
811 Views
Last Modified: 2013-11-23
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...
0
Comment
Question by:dbCnc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:dbCnc
ID: 22757638
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
 

Accepted Solution

by:
dbCnc earned 0 total points
ID: 22852965
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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Crystal Reports Licensing Questions 4 86
swing controls 2 37
American Express @Work site and Java 4 95
Strange router problem - can't access hotmail.com 14 108
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question