Re: How to lock 3 JSliders together
"TonyZ" <tonyz@link-research.com> wrote in message
news:1150386559.321223.195020@i40g2000cwc.googlegroups.com...
Hi all,
I have 3 JSliders and I want the option of locking their movement
together. I use
a checkbox labeled "Lock Sliders" to determine whether the sliders
should be locked or not.
I thought I could do this by using the "setValue()" method on the two
sliders that were not
being draged. For example, if the user was dragging slider A, I would
set the value of B and C in the Change Listener routine.
The problem is that the setValue() fires a changed state for the B and
C sliders (each of
which, in turn, fires a changed state for the other two, causing a big
problem).
Anybody ever tried locking sliders? How can it be done?
maybe (?) something like this
import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
{
JSlider[] sliders = new JSlider[3];
BoundedRangeModel[] models = new BoundedRangeModel[sliders.length];
JPanel colorPanel = new JPanel();
SliderListener listener = new SliderListener();
public Testing()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(200,100);
colorPanel.setPreferredSize(new Dimension(400,200));
colorPanel.setBackground(new Color(0,0,0));
JPanel panel = new JPanel(new GridLayout(3,1));
for(int x =0; x < sliders.length; x++)
{
sliders[x] = new JSlider(0,255,0);
sliders[x].setMajorTickSpacing(20);
sliders[x].setMinorTickSpacing(10);
sliders[x].setPaintTicks(true);
sliders[x].setPaintLabels(true);
sliders[x].addChangeListener(listener);
models[x] = sliders[x].getModel();
panel.add(sliders[x]);
}
JPanel p = new JPanel();
final JCheckBox cbx = new JCheckBox("Lock");
p.add(cbx);
getContentPane().add(panel,BorderLayout.NORTH);
getContentPane().add(colorPanel,BorderLayout.CENTER);
getContentPane().add(p,BorderLayout.SOUTH);
pack();
cbx.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent ae){
if(cbx.isSelected())
{
sliders[1].setModel(sliders[0].getModel());
sliders[2].setModel(sliders[0].getModel());
colorPanel.setBackground(new
Color(sliders[0].getValue(),sliders[1].getValue(),sliders[2].getValue()));
}
else
{
sliders[1].setModel(models[1]);
sliders[2].setModel(models[2]);
colorPanel.setBackground(new
Color(sliders[0].getValue(),sliders[1].getValue(),sliders[2].getValue()));
}}});
}
class SliderListener implements javax.swing.event.ChangeListener
{
public void stateChanged(javax.swing.event.ChangeEvent ce)
{
colorPanel.setBackground(new
Color(sliders[0].getValue(),sliders[1].getValue(),sliders[2].getValue()));
}
}
public static void main(String[] args){new Testing().setVisible(true);}
}