Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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...</p> <p>The simplest idea would be to use a <code>FocusListener</code> registered only to the text fields and maintain a reference to the last field selected.</p> <p>When the slider changes, you would simply interact with the last selected field (if it's not <code>null</code>)</p> <p><img src="https://i.stack.imgur.com/T3cok.png" alt="enter image description here"></p> <pre><code>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 &lt; 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(); } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload