javaswingjpaneljlabeljslider

Huge space between my JSlider and text underneath


I'm currently working on a media player for java, and with the power of VLCJ I was working on implementing an equalizer adjust window. There will be 11 vertical sliders with a JLabel underneath them indicating the hZ band and the dB level of the band. However, the slider keeps adding a huge gap between itself and the JLabel. I tried stacking just two JLabels on top of each other and there's barely a gap at all. My code is below. (The return equalizer stuff hasn't been implemented yet. I just want a basic UI working before I start adding in the functionality)

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import uk.co.caprica.vlcj.player.Equalizer;

public class VideoEQFrame {

    public VideoEQFrame() {
        //constructor
    }

    public Equalizer show() {
        JFrame frame = new JFrame("Effects");

        JPanel panel = new JPanel();

        JPanel sliders= new JPanel();

        JPanel gainObjects = new JPanel(new GridLayout(2, 0, 2, 0));
        JSlider gainS = new JSlider(JSlider.VERTICAL, -12, 12, 0);

        gainS.setMajorTickSpacing(2);
        gainS.setPaintTicks(true);
        gainS.setToolTipText("Adjust the gain");
        JLabel gainL = new JLabel("Text");
        gainObjects.add(gainS);
        gainObjects.add(gainL);
        sliders.add(gainObjects);

        panel.add(sliders);

        frame.add(panel);
        frame.setSize(new Dimension(600, 300));
        //frame.setResizable(false);
        frame.setVisible(true);

        Equalizer eq = new Equalizer(0);
        return eq;
    }

}


Solution

  • You are using GridLayout to lay the slider and the text label. That means that they will both occupy the same height. So because the slider has bigger height, the height of the label also adjusts to this height. Try using another LayoutManager like BorderLayout, like so:

    import java.awt.BorderLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    
    public class VideoEQFrame {
    
        public VideoEQFrame() {
            //constructor
        }
    
        public void show() {
            JFrame frame = new JFrame("Effects");
    
            JPanel panel = new JPanel();
    
            JPanel sliders= new JPanel();
    
            JPanel gainObjects = new JPanel(new BorderLayout());
            JSlider gainS = new JSlider(JSlider.VERTICAL, -12, 12, 0);
    
            gainS.setMajorTickSpacing(2);
            gainS.setPaintTicks(true);
            gainS.setToolTipText("Adjust the gain");
            JLabel gainL = new JLabel("Text");
            gainObjects.add(gainS, BorderLayout.CENTER);
            gainObjects.add(gainL, BorderLayout.PAGE_END);
            sliders.add(gainObjects);
    
            panel.add(sliders);
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            //frame.setResizable(false);
            frame.setVisible(true);
    
    //        Equalizer eq = new Equalizer(0);
    //        return eq;
        }
    
        public static void main(final String[] args) {
            new VideoEQFrame().show();
        }
    }