Solved

JSlider Middle Mouse Button Handler

Posted on 2008-10-20
4
802 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

776 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