Question

...or maybe my programming is the odd behavior...

I am trying to create a class that I'm using a lot in my program. It is a class that creates a JPanel control that contains a Jlabel at the top in one pane and a JSlider and JSpinner in another pane. I want the JSpinner to change when the JSlider changes and vice versa. I want their value tied to each other. This is for the sake of the JSpinner fine tuning the JSpinner selection.

Here is the code for the class ("ij" imports are for the government's ImageJ application):

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

 public class LabeledSliderSpinner extends JPanel implements ChangeListener {

JPanel sspanel = new JPanel();
JSlider slider;
SpinnerModel model;
JSpinner spinner;

public LabeledSliderSpinner() {}

public LabeledSliderSpinner(String label, int left, int x, int right) {

    JLabel sslabel = new JLabel(label);

    JSlider slider = new JSlider(JSlider.HORIZONTAL, left, right, x);
    slider.setMajorTickSpacing(25);
    slider.setMinorTickSpacing(5);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.setPaintTrack(true);
    slider.setVisible(true);

    slider.addChangeListener(this);

    model = new SpinnerNumberModel(x, left, right, 1);
    JSpinner spinner = new JSpinner(model);
    spinner.setEditor(new JSpinner.NumberEditor(spinner, "####"));

    spinner.addChangeListener(this);

    sspanel.add(slider);
    sspanel.add(spinner);

    add(sslabel);
    add(sspanel);
    setVisible(true);
}

public void stateChanged(ChangeEvent e) {
    String a = "";

    Object o = e.getSource();
    Class c = o.getClass();
    String controlName = c.getName();


    if (controlName == "javax.swing.JSpinner") {
// not coded yet
    }

    else 

    if (controlName == "javax.swing.JSlider") {
        if (!slider.getValueIsAdjusting()) {
            int fps = (int)slider.getValue();
            spinner.setValue(new Integer(fps));
        }
    }

}
}

It is called like this:

LabeledSliderSpinner lss = new LabeledSliderSpinner("Homing Line", minx, x, maxx);

In the stateChanged method, I have determined that when it reaches the getValueIsAdjusting test, it goes doesn't even finish the stateChanged method. It just goes back to the main function. The slider knob moves. The spinner value doesn't change. I even opened a dialog box within the getValueIsAdjusting test. It never makes it in. I tried opening a dialog box as an else condition to the getValueIsAdjusting test and it never opens either dialog box. It just returns from stateChanged().

I could use a better way to determine if the stateChanged is called because of the slider or the spinner too.

Was it helpful?

Solution

SpinSlider may be helpful in this context.

image

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top