1

This is my first time here, I'm not yet familiar with the site but I heard it was great. I'm currently trying to learn Java. I'm having problems with layouts. The one I'm specifically looking at right now is the FlowLayout. Would anyone be able to tell me how I can still change the size of a JButton for example when a FlowLayout it being used? Thanks :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Robin
  • 143
  • 12

1 Answers1

3

As FlowLayout uses preferred sizes, you can override getPreferredSize in that component:

JButton myButton = new JButton(" Click Me !") {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(60, 25);
    };
};

In case you're wondering why I didn't call setPreferredSize, check out

Should I avoid the use of set[Preferred|Maximum|Minimum]Size methods in Java Swing?

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Why extend it like that when you could just call myButton.setPreferedSize(new Dimension(60,25)) it seems unnecessary. – Adude11 Jan 28 '13 at 22:04