Alright I have been using the setSize method for the JSlider to try to set it's size but it seems to be having no effect what soever. I have the JSliders instead a JPanel that is using a GridBagLayout, the JPanel is then being adding to another JPanel which uses a GridBag Layout. The second JPanel then functions as the contentArea for a JFrame. I have my JSliders set to Vertical (as for an equalizer) and I need to make them shorter, so I figured using setSize would work, but, as I mentioned, it is not :(.

Thanks for any help you can provide!

Dani AI

Generated

Short answer: the layout manager controls component size — not setSize. As suggested, check your layout. is also right: setSize is effectively ignored by most Swing layout managers. To make a vertical JSlider shorter, supply a preferred (or fixed) size and adjust your GridBagConstraints so the slider is not stretched.

Example: set the slider's preferred size, add it with constraints that do not grab extra vertical space, and call pack() on the frame so preferred sizes are honored.

JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 50);
slider.setPreferredSize(new Dimension(40, 120)); // width, height

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0;
gbc.weighty = 0;                     // don't give extra vertical space
gbc.fill = GridBagConstraints.NONE;  // don't stretch
gbc.anchor = GridBagConstraints.NORTH;

panel.add(slider, gbc);

// after building the UI:
frame.pack();
frame.setVisible(true);

If you need the slider completely fixed, either set its maximum size to the preferred size (slider.setMaximumSize(slider.getPreferredSize())) or override getPreferredSize() in an anonymous subclass. Beware: forcing pixel sizes can break look-and-feel and portability, so prefer constraints-based sizing when possible.

Quick checklist

  • Make sure gbc.fill is not GridBagConstraints.VERTICAL/BOTH and gbc.weighty is 0 for sliders you don’t want stretched.
  • Call frame.pack() after adding components.
  • Avoid null layout; use layout managers or BoxLayout (with setMaximumSize) for equalizer-style columns.
  • If something still expands, inspect sibling components’ weights and fills — they determine how extra space is allocated.

Recommended Answers

All 2 Replies

Check your layout manager. Maybe alter the gridbaglayout a little bit so it works.

setSize has little effect for most controls, it's mainly there to enable resizing of entire windows (and might have been better implemented at a different level in the class hierarchy), you're not supposed to control the size in pixels of user interface controls in Java as they're controlled by the layoutmanager.
This is to make sure it looks decent on whichever operating system your application may run on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.