Question

I tried to use a JSlider to set text in three JTextFields. My condition is the slider should work for textfield_1, only when a textfield_1 get its focus, similarly for the other two textfields. When I tried to use the same slider with other textfield, only the first text field values getting changed.

Expecting valuable suggestions Thanks in Advance.

JSlider slider;
JTextField tf;


tf.addFocusListener(new FoucusListener(){
 public void foucusGained(FocusEvent fe){   
   slider.addChangeListener(new ChangeListener()){
 public void stateChanged(ChangeEvent ce){
   JSlider slider =(JSlider)ce.getSource();
    if(slider.getValueisAdjusting())
        tf.setText(String.valueOf(slider.getValue()))
    }
});
});
Was it helpful?

Solution

The basic idea is you need to know what field was last selected. The problem is, when you select the slider, it will fire a focus gained event...

The simplest idea would be to use a FocusListener registered only to the text fields and maintain a reference to the last field selected.

When the slider changes, you would simply interact with the last selected field (if it's not null)

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderControl {

    public static void main(String[] args) {
        new SliderControl();
    }

    public SliderControl() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JSlider slider;
        private JTextField[] fields;

        private JTextField selectedField;

        public TestPane() {
            setLayout(new GridBagLayout());
            fields = new JTextField[3];
            FocusHandler focusHandler = new FocusHandler();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            for (int index = 0; index < 3; index++) {
                fields[index] = new JTextField(3);
                fields[index].addFocusListener(focusHandler);
                add(fields[index], gbc);
                gbc.gridy++;
            }
            gbc.fill = GridBagConstraints.HORIZONTAL;
            slider = new JSlider();
            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    if (selectedField != null) {
                        selectedField.setText(String.valueOf(slider.getValue()));
                    }
                }
            });
            add(slider, gbc);
        }

        protected class FocusHandler extends FocusAdapter {

            @Override
            public void focusGained(FocusEvent e) {
                if (e.getComponent() instanceof JTextField) {
                    selectedField = (JTextField) e.getComponent();
                }
            }

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