3

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?

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131

3 Answers3

3

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());
            }
        });
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks, but for some reason it still doesn't work. I put my JSlider inside a JPanel with FlowLayout, set a preferred size for the JPanel and added it to the main JPanel. Still didn't change anything. Tried several different preffered-sizes, still didn't help. Tried BorderLayout, BoxLayout and GridLayout, still nothing changed. Any idea? – Aviv Cohn Feb 17 '14 at 21:22
  • FlowLayout respects preferrred size of the slider. You need a layout that doesnt. – Paul Samsotha Feb 17 '14 at 21:47
  • 1
    I also tried BorderLayout. The difference in the result the two layouts gave me, was that flow layout made the slider appear 'cut' at the edges, like it doesn't fit it's cell, and border layout shrinked the slider to a small size (my current problem). I think my problem is that the main JPanel doesn't respect the preferred size of the nested JPanel. and for some reson, using `gbc.gridheight` to make the nested panel cross two cells, doesn't change anything – Aviv Cohn Feb 17 '14 at 21:55
  • *"I also tried.."* If you cannot work it out, try posting a MCTaRE like peeskillet has now provided 2 of. – Andrew Thompson Feb 18 '14 at 07:13
1

To change the size of a swing component you use the setPrefferedSize() method

setPreferredSize(new Dimension(300, 80));

You may have to use a separate panel with a flowLayout for the slider then add that panel to the . Some layout managers may not take the preferred size.

Jason White
  • 5,495
  • 1
  • 21
  • 30
  • 1
    Thanks for your answer. As I wrote in the question, I tried this method and it didn't change anything. It's weird since I though `GridBagLayout` should respect preferred sizes. Shouldn't it? – Aviv Cohn Feb 17 '14 at 17:52
  • Im not familiar with GridBagLayout but I had a similar issue using a JTable. I ended up having to nest panels in order for the setPrefferedSize() to take. – Jason White Feb 17 '14 at 18:52
  • http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html That link might be helpful. You can set constraints, so components span multiple column or multiple rows. You may be restricting the slider to one cell. – Jason White Feb 17 '14 at 19:01
  • 1
    Thanks but as I said in my question, I already tried using gridy and gridheight to solve this, with no luck. also the setpreferredsize method doesn't help. thanks anyway for trying to help – Aviv Cohn Feb 17 '14 at 21:01
0

Bit late to the party, but I had this same issue with a GridBagLayout and resolved it by changing the GridBagConstraints' weighty (or weightx for a vertical slider, I suppose) of the JSlider to 1 when adding it to a nested JPanel.

// parent container ...
JFrame f = new JFrame();

JPanel p = new JPanel(new GridBagLayout()); // nested jpanel, parent to the slider

f.add(p);

GridBagConstraints c = new GridBagConstraints;

c.weighty = 1; // slider will stretch to fill

JSlider slider = new JSlider();

p.add(slider, c);

This means that the JSlider is allowed to stretch to fill the parent JPanel. Setting weighty to 0 is no stretch, 0.5 will stretch the component half as much as the parent container, 1 stretches it all the way.

womble
  • 1
  • 1