是否可以使用控制多个JTextfield的JSlider?

时间:2013-06-06 06:57:17

标签: java swing jtextfield jslider

我尝试使用JSlider在三个JTextField中设置文本。 我的条件是滑块应该适用于textfield_1,仅当textfield_1得到它的焦点时,类似于其他两个文本字段。 当我尝试将同一个滑块与其他文本字段一起使用时,只会更改第一个文本字段值。

期待有价值的建议提前感谢。

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()))
    }
});
});

1 个答案:

答案 0 :(得分:1)

基本思路是你需要知道上次选择的字段。问题是,当您选择滑块时,它将触发焦点获取事件...

最简单的想法是使用仅FocusListener注册到文本字段并保持对所选最后一个字段的引用。

当滑块更改时,您只需与最后选择的字段进行交互(如果它不是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();
                }
            }

        }
    }
}