0

StackOverFlow Question Hello for the second time today dedicated stack overflow users! XD

So I'm trying to set the positioning of these 9 buttons in a grid format with .setBounds which accepts (xCoordinate, yCoordinate, #ofPixelsWide, #ofPixelsTall)

Would anyone know of an even more efficient/compact way to do this? I would like to know even if it doesn't use .setBounds, after all I'm here to learn XD

Thanks for any suggestions

for (int i = 0; i < groupOfButtons.length; i++) {
        int x = 0, y = 0;
        if (i == 1 || i == 4 || i == 7) {
            x = 110;
        }
        if (i == 2 || i == 5 || i == 8) {
            x= 220;
        }
        if (i > 2 && i < 6) {
            y = 110;
        }
        if (i > 5 && i < 9) {
            y = 220;
        }
        groupOfButtons[i].setBounds(x, y, 100, 100);
    }

This was instead of writing this btw (this way is actually shorter but looks a lot more messy):

groupOfButtons[0].setBounds(0, 0, 100, 100);
groupOfButtons[1].setBounds(110, 0, 100, 100);
groupOfButtons[2].setBounds(220, 0, 100, 100);
groupOfButtons[3].setBounds(0, 110, 100, 100);
groupOfButtons[4].setBounds(110, 110, 100, 100);
groupOfButtons[5].setBounds(220, 110, 100, 100);
groupOfButtons[6].setBounds(0, 220, 100, 100);
groupOfButtons[7].setBounds(110, 220, 100, 100);
groupOfButtons[8].setBounds(220, 220, 100, 100);
6
  • 3
    Use a layout manager: docs.oracle.com/javase/tutorial/uiswing/layout/visual.html Commented Aug 13, 2016 at 21:29
  • 1
    This is exactly what the layout managers are for -- you're making things way too difficult for yourself. Commented Aug 13, 2016 at 21:29
  • 1
    GridLayout would be perfect here. Commented Aug 13, 2016 at 21:30
  • @HovercraftFullOfEels wow 2 questions in and they're both marked as duplicates, sorry man when I search the terms I'm using are so circumstantial the correct related articles don't pop up XD Commented Aug 13, 2016 at 21:34
  • Archie, there's rarely anything new under the sun. As for your use of null layouts, understand that while null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one. Commented Aug 13, 2016 at 21:37

1 Answer 1

1

You need to use a grid layout. You'll then simply add these buttons and it'll place them in a grid format automatically. Look at this documentation for an in detail explaination of layouts:

https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

If you need to use another layout besides grid layout for other components of your project, you can nest layouts (so this grid of buttons will be a nested layout inside of another layout).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.