Search code examples
javaswingjslider

Add multiple instances of slider component to Jpanel


Working on mixer app with multiple sliders. I want to create multiple instances of the same slider side by side for as many channels as I determine (1-16). This will very so I want to just create new side by side instances of VolumeControl from left to right. For now I just they can all work from the same inputs, listeners, etc. I'm just working on getting them to display. Here's what I have in order to create a single instance of JSlider (mostly taken from a demo). NOTE: I'm coming from Android development so I could be going the complete wrong, complicated way!

public class VolumeControl extends JPanel implements ChangeListener {

    ...

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(VolumeControl::createAndShowGUI);
    }

    public VolumeControl () {

        super(new BorderLayout());

        ...

        JSlider volumeControl = new JSlider(JSlider.VERTICAL,
                VOLUME_MIN, VOLUME_MAX, currentVolume);

        volumeControl.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
        add(volumeControl, BorderLayout.LINE_START);
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    }

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("Volume Control");
        VolumeControl animator = new VolumeControl();
        frame.add(animator, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
    }

}

Solution

  • Simply create one or more factory methods which you can use to create and configure the sliders the way you want...

    Sliders

    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new VolmeControlPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public static class VolmeControlPane extends JPanel {
    
            protected static final int VOLUME_MIN = 0;
            protected static final int VOLUME_MAX = 100;
            protected static final String SLIDER_CONTROL_KEY = "Slider.controlKey";
    
            public VolmeControlPane() {
                setLayout(new GridBagLayout());
                JSlider[] sliders = makeSliders(16);
                for (JSlider slider : sliders) {
                    add(slider);
                }
            }
    
            public JSlider[] makeSliders(int count) {
    
                JSlider[] sliders = new JSlider[count];
                for (int index = 0; index < count; index++) {
                    sliders[index] = makeSlider(VOLUME_MIN, VOLUME_MAX, VOLUME_MIN + (VOLUME_MAX / 2));
                    sliders[index].putClientProperty(SLIDER_CONTROL_KEY, index);
                }
    
                return sliders;
    
            }
    
            public JSlider makeSlider(int min, int max, int value) {
                JSlider volumeControl = new JSlider(JSlider.VERTICAL,
                                min, max, value);
    
                volumeControl.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
    
                return volumeControl;
            }
    
        }
    
    }
    

    One little trick I added was using putClientProperty to provide a key to identify the slider, you could also use setName. The idea here is when you attach a listener, you can interegate the slider for the key and identify which channel it represents, as a an idea