2

I'm trying to resize JButton to always be a certain size. I have 9 of these buttons. I understand from the API that it JButton inherits setSize(int a, int b) and setSize(Dimension d). I opted to use the second though I tried the other and it didn't fix my problem. Here is the code.

// setup buttons 
        reset = new JButton("Reset");
        square1 = new JButton();
        square2 = new JButton();
        square3 = new JButton();
        square4 = new JButton();
        square5 = new JButton();
        square6 = new JButton();
        square7 = new JButton();
        square8 = new JButton();
        square9 = new JButton();

    //set button size
    Dimension d = new Dimension(100,100);
    square1.setSize(d);
    square2.setSize(d);
    square3.setSize(d);
    square4.setSize(d);
    square5.setSize(d);
    square6.setSize(d);
    square7.setSize(d);
    square8.setSize(d);
    square9.setSize(d);

I've tried several diferent dimensions and none of them make any difference. What am I missing? I'm using a gridLayout(3,3,5,5) for the the JPanel that the buttons are on. The dimensions for the JFrame are (400,425). Thanks for any help!

Rostro
  • 148
  • 2
  • 3
  • 15
  • Two good strategies for big buttons. 1) Large font (or a large icon) 2) [`setMargin(Insets)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractButton.html#setMargin-java.awt.Insets-). – Andrew Thompson Sep 20 '14 at 06:13

4 Answers4

13

The layout managers usually don't respect the size of a component, so setting size often has little effect. Instead, the layout managers usually respect the preferred sizes of components. Having said this, it's usually better to not try to set the preferredSizes yourself via setPreferredSize(Dimension d) but rather to let the layout managers and the components do this for you. If you must do it yourself, consider extending the component and overriding getPreferredSize() and adding smart code that returns a decent and proper preferredSize Dimension.

For example, please have a look at my code in my answer to a recent question here. In it, I change the size of the JButton's Font to make it larger, but i never set the sizes or preferredSizes of any component, but rather let the components themselves and the container's layout managers do this work for me.

If you don't believe me, please read some of the posts and comments by kleopatra, one of the smartest and most influential Swing coders that I know of, and one of the authors of the SwingX toolkit. For example her answer here.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
5

Try using square1.setPreferredSize(d) instead of setSize(d).

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Jon
  • 233
  • 1
  • 2
  • 6
1

You can use setBounds() method as:

Frame f=new Frame();
f.setLayout(null);
Button b=new Button("Dummy");
//setBounds() methods accepts 4 
// integer parameters
/* 1. Location on x axis
2. Location on y axis
3. Width
4. Height */
b.setBounds(20,30,50,30);
f.add(b);

Thats how you set size for a button There are more ways to specify size but i use this one Hope this will help you....

Priyank
  • 37
  • 1
  • 7
0

The setPreferredSize(Dimension d) should work. Your last block of code will look like this:

square1.setPreferredSize(d);
square2.setPreferredSize(d);
square3.setPreferredSize(d);
square4.setPreferredSize(d);
square5.setPreferredSize(d);
square6.setPreferredSize(d);
square7.setPreferredSize(d);
square8.setPreferredSize(d);
square9.setPreferredSize(d);

The rest of your code is the same.

kautom
  • 29
  • 3
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '22 at 21:17