Results 1 to 3 of 3
  1. #1
    peterhabe is offline Member
    Join Date
    Jul 2010
    Rep Power

    Default Is there a way to change/override the listeners ChangeListener adds to JSliders?

    Is there a way to change/override the listeners ChangeListener adds to JSliders so it does not call the stateChanged() method when the mouse is released over a JSlider. I know there is a createChangeListener() method but i don't have a clue how to use it.

    Can anyone help me?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power


    If you want help, you'll probably want to provide an SSCCE that demonstrates the behavior you want to change.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    peterhabe is offline Member
    Join Date
    Jul 2010
    Rep Power


    In this example user can change the value of the variable 'alphaParticleEnergy' using the JSlider. The value from the JSlider is first tested and if allowable (which depends on other varables in the program), 'alphaParticleEnergy' is set to the value of the JSlider. If the value on the JSlider is not allowable, the JSlider is set to the last value of 'alphaParticleEnergy' which was allowable. The problem is, that after the value of the JSlider is set to an allowable value, the stateChanged() method is called again using the previous value from the Slider which is not allowable. The stateChanged() method apears to be called when the mouse is released, using the position on the Slider which where the mouse was released, which gives the SJlider the value which is not allowable. That i want to do is stop the stateChanged() being called when the mouse is released. In this example 1 is not an allowable value for the variable 'alphaParticleEnergy'.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    public class alphaParticleProgram extends JFrame{
    	public alphaParticleProgram(){
    		this.add(new MainWindow());
    		this.setMinimumSize(new Dimension(800,600));
    		this.setTitle("Closest Approach Program");
    	//main method
    	public static void main(String[] args){
    		new alphaParticleProgram();
    	//inner class
    	private class MainWindow extends JPanel implements ChangeListener{
    		//static variables
    		static final double electricFieldConstant = 1/(4 * Math.PI * 8.85e-12);
    		static final double productOfCharges = 79 * 2 * (1.6e-19) * (1.6e-19);
    		static final double conversionToMeV = 1e6 * 1.6e-19;
    		static final double scaleConversion = 4e15;
    		static final double defaultAlphaParticleEnergy = 5 * MainWindow.conversionToMeV;
    		static final double alphaParticleDefaultActualXPosition = 80/ scaleConversion, alphaParticleDefaultActualYPosition = 205 / scaleConversion, goldAtomDefaultActualXPosition = 590 / MainWindow.scaleConversion, goldAtomDefaultActualYPosition = 205 / MainWindow.scaleConversion;
    		//Swing Components
    		JSlider alphaParticleEnergySlider;
    		JTextField totalEnergyDisplay;
    		//other variables
    		double alphaParticleEnergy = defaultAlphaParticleEnergy, kineticEnergy, potentialEnergy;
    		double alphaParticleActualXPosition, alphaParticleActualYPosition, goldAtomActualXPosition, goldAtomActualYPosition;
    		boolean alphaParticleEnergySet = false, setAlphaParticleSet = false;
    		public MainWindow(){
    			//setting default positions
    			alphaParticleActualXPosition = alphaParticleDefaultActualXPosition;
    			alphaParticleActualYPosition = alphaParticleDefaultActualYPosition;
    			goldAtomActualXPosition = goldAtomDefaultActualXPosition;
    			goldAtomActualYPosition = goldAtomDefaultActualYPosition;
    			alphaParticleEnergySlider = new JSlider(1, 10, 5);
    			//setting default energy values
    			potentialEnergy = (electricFieldConstant * productOfCharges) / (Math.sqrt(Math.pow(alphaParticleDefaultActualXPosition - goldAtomDefaultActualXPosition, 2) + Math.pow(alphaParticleDefaultActualYPosition - goldAtomDefaultActualYPosition, 2)));
    			kineticEnergy = alphaParticleEnergy - potentialEnergy;
    			totalEnergyDisplay = new JTextField(String.valueOf(alphaParticleEnergy / conversionToMeV), 3);
    		///////////////////////////////////Change listener method///////////////////////////////////////////
    		public void stateChanged(ChangeEvent event) {
    			if(kineticEnergyIsPositive(alphaParticleActualXPosition, alphaParticleActualYPosition, goldAtomActualXPosition, goldAtomActualYPosition, alphaParticleEnergySlider.getValue() * conversionToMeV)){
    				setEnergyValues(alphaParticleEnergySlider.getValue() * conversionToMeV);
    				alphaParticleEnergySlider.setValue((int)Math.round(alphaParticleEnergy /conversionToMeV));
    				setAlphaParticleSet = true;
    				JOptionPane.showMessageDialog(null, "The alpha particle can not have that amount of energy and be in that position. If you want the alpha particle to have that amount of energy, you need to move the alpha particle further away from the gold atom.");
    			alphaParticleEnergySet = false;
    				alphaParticleEnergySet = true;
    				setAlphaParticleSet = false;
    		//checking whether values entered are allowed
    		public boolean kineticEnergyIsPositive(double aX, double aY, double gX, double gY, double aE){
    			double kineticEnergy, potentialEnergy;
    			potentialEnergy = (electricFieldConstant * productOfCharges) / Math.sqrt((aX - gX)*(aX - gX) + (aY - gY)*(aY - gY));
    			kineticEnergy = aE - potentialEnergy;
    			if(kineticEnergy >= 0){
    				return true;
    				return false;
    		//setting JTextField
    		public void setEnergyValues(double energy){
    			//calculating variable values
    			alphaParticleEnergy = energy;
    			potentialEnergy = (electricFieldConstant * productOfCharges) / Math.sqrt(Math.pow(alphaParticleActualXPosition - goldAtomActualXPosition, 2) + Math.pow(alphaParticleActualYPosition - goldAtomActualYPosition, 2));
    			kineticEnergy = alphaParticleEnergy - potentialEnergy;
    			//setting displays
    			totalEnergyDisplay.setText(String.valueOf((double)Math.round((alphaParticleEnergy / conversionToMeV) * 1000) / 1000));

Similar Threads

  1. Editable combo box that adds new entries
    By monica in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2011, 09:01 PM
  2. Assigning labels to JSliders?
    By Vase in forum New To Java
    Replies: 5
    Last Post: 04-05-2011, 11:37 PM
  3. Client/ Server system that adds a stream of integers
    By Billywizz in forum New To Java
    Replies: 12
    Last Post: 01-27-2011, 01:28 PM
  4. override/change the name/prefix of log4j loggers
    By adeelmahmood in forum Advanced Java
    Replies: 0
    Last Post: 08-25-2010, 01:26 AM
  5. Replies: 3
    Last Post: 07-30-2010, 10:27 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