0

So I'm trying to resize a JSlider which is in a layout manager. Because of this (or so I have heard) I need to set its preferred size. The setPreferedSize does not accept int, int as I expected, but instead accepts a dimension. My question is, how do I actually set the dimension?

I have tried:

dimension sliderLength = 500, 10

dimension sliderLength = (500, 10)

And I have tried

dimension sliderLength(500, 10)

All without success.

Jake Stanger
  • 449
  • 1
  • 8
  • 24
  • So you skipped the basics of Java programming and can't even construct an object? How did you manage to use a JSlider if you don't know even that? – Kayaman Jan 06 '14 at 16:18
  • [Read this link](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – nachokk Jan 06 '14 at 16:19

2 Answers2

2
slider.setPreferredSize(new Dimension(500, 10));

or:

Dimension sliderLength = new Dimension(500, 10);
slider.setPreferredSize(sliderLength);
Boann
  • 48,794
  • 16
  • 117
  • 146
2

You could do

JSlider slider = new JSlider() {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 10);
    }
};
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • That's true, but you broke this [principle](http://www.oodesign.com/liskov-s-substitution-principle.html), and you are creating a new dimnesion each time.. – nachokk Jan 06 '14 at 16:20