How to set size of a JSlider?

Aviv Cohn picture Aviv Cohn · Feb 17, 2014 · Viewed 11k times · Source

I have searched the web for a solution to this problem and didn't find anything that worked.

I have a vertical JSlider inside a JPanel that uses GridBagLayout and a GridBagConstraints for positioning the objects on the panel.

Currently I have the following code:

    gbc.gridy = 1;
    add(button1,gbc);

    gbc.gridy = 2;
    add(button2,gbc);

    gbc.gridy = 3;
    add(slider,gbc);

The objects are positioned vertically along the panel.

The slider always appears in the same size (length). Tried to use setPreferredSize - didn't work. Tried to use gridheight in order to have to slider cross two rows - didn't work either. Tried to change the actual min and max values of the slider, didn't help.

Shouldn't GridBagLayout respect preferred size?

EDIT: Also tried creating anoter JPanel inside the main JPanel, set it's layout to FlowLayout, BoxLayout or GridLayout, and add the slider to it. Didn't change a thing. Very weird.

Any ideas?

Answer

Paul Samsotha picture Paul Samsotha · Feb 17, 2014

Use a different layout manager (or put the slider in a nested JPanel with a different layout manager), that will stretch the JSlider. In the example I use BorderLayout

enter image description here

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;

public class DifferentSizeSlider extends JPanel{
    public DifferentSizeSlider() {
        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(new JSlider());

        JPanel bottomPanel = new JPanel(new GridLayout(1, 2));
        bottomPanel.add(new JSlider());
        bottomPanel.add(new JPanel());

        setLayout(new GridLayout(2, 1));
        add(topPanel);
        add(bottomPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JOptionPane.showMessageDialog(null, new DifferentSizeSlider());
            }
        });
    }
}

EDIT with your code example

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;

public class DifferentSizeSlider extends JPanel{
    public DifferentSizeSlider() {
        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.setPreferredSize(new Dimension(400, 50));
        topPanel.add(new JSlider());

        JButton jbtOne = new JButton("Button");
        JButton jbtTwo = new JButton("Button");

        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(new GridBagLayout());

        gbc.gridy = 0;
        add(jbtOne,gbc);  

        gbc.gridy = 1;
        add(jbtTwo,gbc);

        gbc.gridy = 2;
        add(topPanel, gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JOptionPane.showMessageDialog(null, new DifferentSizeSlider());
            }
        });
    }
}