0
public void renderPanels(){
    for(int i = 1; i<4; i++){
        for(int j = 1; j<4; j++){
            panels[i][j] = new JPanel();
            frame.add(panels[i][j]);

            buttons[i][j] = new JButton();
            panels[i][j].add(buttons[i][j]);

            panels[i][j].setBounds(i*50, j*50, 50, 50);

            panels[i][j].setLayout(null);

            buttons[i][j].setBounds(0, 0, 50, 50);

            System.out.println(panels[i][j].getBounds());
        }
    }
}

Ok so this is my code to render a 3x3 grid of panels with buttons inside them. the frame variable contains a JFrame object but sadly my output is like in the following screenshot:

Screenshot

the frame in the last iteration seems to be rendered at a location as if i and j are 1 unit below the initial values. Any Ideas?

Omran Jamal
  • 379
  • 5
  • 12
  • Please use a layout manager instead of calling `setBounds`. – msrd0 Dec 23 '14 at 11:35
  • What about CardLayout ? – Anptk Dec 23 '14 at 11:37
  • 1
    My Question is why won't this work? – Omran Jamal Dec 23 '14 at 11:37
  • My question is, what are your debugging results? – Tom Dec 23 '14 at 11:50
  • 3
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) **It is likely the null layout is the root of the problems seen.** – Andrew Thompson Dec 23 '14 at 11:53
  • 2
    The bits of that GUI that appear, suggest `GridLayout` to me.. – Andrew Thompson Dec 23 '14 at 11:55

2 Answers2

6
  1. use GridLayout instead of setBounds(), dont use NullLayout at all

  2. frame.add(panels[i][j]); should be last code line in loop

  3. System.out.println(panels[i][j].getBounds()); returns reasonable coordinates after JFrame.pack() is called or JFrame is already visible on the screen

  4. for better help sooner post an SSCCE/MCVE, short, runnable, compilable

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

It is happening because JFrame default layout is not null. It is a BorderLayout. If you really want to set bounds please make the JFrame layout null.

frame.setLayout(null);

** Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales. Layout managers also can be reused easily by other containers, as well as other programs.

Although, GridLayout is the way to go. Here some things you might be need to consider.

Absolute Positioning

How to use GridLayout

Here is some basic of GridLayout

  JPanel m = new JPanel();

        GridLayout gridLayout = new GridLayout(3, 3,0,0);
        m.setLayout( gridLayout);
        for (int i = 0; i < gridLayout.getRows(); i++) {
            for (int ii = 0; ii < gridLayout.getColumns(); ii++) {
                JPanel p = new JPanel();
                p.add(new JButton("X"));
                m.add(p, i, ii);
            }

        }
        frame.add(m);
Lalith J.
  • 1,391
  • 4
  • 16
  • 27
  • 1
    Sorry, I can't endorse `null` layout in this context. – trashgod Dec 23 '14 at 12:04
  • @trashgod he said specifically "My Question is why won't this work?" in the comment. I also know that the better way is to use the GridLayout, but I just gave the answer. – Lalith J. Dec 23 '14 at 12:09
  • Your statement about default layout is correct; the default layout of `JPanel` is `FlowLayout`; both are irrelevant until `pack()` is called or the enclosing window is resized; as to _why won't this work_, note how the poster's indexes start with `1`; not your down-voter. – trashgod Dec 23 '14 at 12:21
  • @trashgod , "not your down-voter." yes I know. thank you. by the way if you want to improve this answer be my guest. – Lalith J. Dec 23 '14 at 12:27