E
etantonio
Good Evening,
I've a problem with jslider and its method getValueIsAdjusting, I also
use slider.setSnapToTicks(true)
in a way that the slider can only choose 3 different values, one in
the middle and the other at the extremity of the slider, the problem
is that when I bring the slider to one of these 3 values I've not just
an event fired but two.
Can you help me to solve this problem ?
Thanks,
Antonio
www.etantonio.it/en
Here's the code not working:
public class CommandPanel extends JPanel implements ChangeListener{
public CommandPanel() {
BorderLayout borderLayout = new BorderLayout();
this.setLayout(borderLayout);
JLabel sliderLabel = new JLabel("Direzione Rotazione",
JLabel.CENTER);
sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//Create the slider.
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 359, 180);
slider.setMajorTickSpacing(180);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setSnapToTicks(true);
slider.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
slider.setFont(new Font("Serif", Font.ITALIC, 15));
slider.setBackground(Constants.BACKGROUND_CONTROL_PANEL);
Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer,
JLabel>();
labelTable.put( new Integer( 0 ), new JLabel("Rotazione verso
Sinistra") );
labelTable.put( new Integer( 180 ), new JLabel("Fermo") );
labelTable.put( new Integer( 359 ), new JLabel("Rotazione verso
Destra") );
slider.setLabelTable( labelTable );
slider.addChangeListener(this);
add(sliderLabel, BorderLayout.NORTH);
add(slider, BorderLayout.CENTER);
setBorder(BorderFactory.createTitledBorder(null, " Pannello Comandi
", TitledBorder.LEFT, TitledBorder.TOP, new java.awt.Font("Verdana",
3,14)));
setBackground(Constants.BACKGROUND_CONTROL_PANEL);
}
/** Listen to the slider. */
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int iValue = (int)source.getValue();
BigDecimal bdValue = new BigDecimal(iValue);
int i2bit = bdValue.divide(BigDecimal.valueOf(5.625) ,
BigDecimal.ROUND_DOWN).intValue();
CommandFrame.getInstance().setSelectedPosition(i2bit);
System.out.println("ACS Comando Posizione valore dello
slider = " + bdValue + " valore codificato = " + i2bit);
}
}
}
I've a problem with jslider and its method getValueIsAdjusting, I also
use slider.setSnapToTicks(true)
in a way that the slider can only choose 3 different values, one in
the middle and the other at the extremity of the slider, the problem
is that when I bring the slider to one of these 3 values I've not just
an event fired but two.
Can you help me to solve this problem ?
Thanks,
Antonio
www.etantonio.it/en
Here's the code not working:
public class CommandPanel extends JPanel implements ChangeListener{
public CommandPanel() {
BorderLayout borderLayout = new BorderLayout();
this.setLayout(borderLayout);
JLabel sliderLabel = new JLabel("Direzione Rotazione",
JLabel.CENTER);
sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//Create the slider.
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 359, 180);
slider.setMajorTickSpacing(180);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setSnapToTicks(true);
slider.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
slider.setFont(new Font("Serif", Font.ITALIC, 15));
slider.setBackground(Constants.BACKGROUND_CONTROL_PANEL);
Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer,
JLabel>();
labelTable.put( new Integer( 0 ), new JLabel("Rotazione verso
Sinistra") );
labelTable.put( new Integer( 180 ), new JLabel("Fermo") );
labelTable.put( new Integer( 359 ), new JLabel("Rotazione verso
Destra") );
slider.setLabelTable( labelTable );
slider.addChangeListener(this);
add(sliderLabel, BorderLayout.NORTH);
add(slider, BorderLayout.CENTER);
setBorder(BorderFactory.createTitledBorder(null, " Pannello Comandi
", TitledBorder.LEFT, TitledBorder.TOP, new java.awt.Font("Verdana",
3,14)));
setBackground(Constants.BACKGROUND_CONTROL_PANEL);
}
/** Listen to the slider. */
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int iValue = (int)source.getValue();
BigDecimal bdValue = new BigDecimal(iValue);
int i2bit = bdValue.divide(BigDecimal.valueOf(5.625) ,
BigDecimal.ROUND_DOWN).intValue();
CommandFrame.getInstance().setSelectedPosition(i2bit);
System.out.println("ACS Comando Posizione valore dello
slider = " + bdValue + " valore codificato = " + i2bit);
}
}
}