monty.py
monty.py

Reputation: 2799

JPanel.getBounds() unexpected returns 0

I have a class that extends from JFrame. In its constructor I generate a JPanel:

    JPanel contentPane = new JPanel();
    contentPane.setBackground(Color.BLACK);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    JOptionPane.showMessageDialog(contentPane, contentPane.getBounds());

I thought with setContentPane, I would fill the whole available space in my JFrame. So it automatically sets the bounds of the JPanel.

I need to get the bounds of the JPanel, but getBounds() returns x = 0, y = 0, width = 0, height = 0. Other size related methods of the JPanel (like getWidth() etc.) also return 0. If I setBounds() of my JPanel explicit, the methods return my given values.

The curious thing about that is, that the setBackground fills the whole available space in my JFrame. So, the size of my JPanel must be exactly the size of my JFrame. But it doesn´t seem so.

Maybe this problem is caused by the layout manager, like most Container-related problems. But with a layout manager, I also get 0.

I think this is a very dumb question and can be easily solved, but I can´t figure out the problem. I didn´t find an answer to this in the web, so hopefully didn´t dublicate some post.

Upvotes: 0

Views: 922

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

The curious thing about that is, that the setBackground fills the whole available space in my JFrame. So, the size of my JPanel must be exactly the size of my JFrame. But it doesn´t seem so.

Actually no, the content pane will be smaller then the frame, because it's contained within the frame decorations. That means the viewable space is the size of the window - the size of the decorations.

Also, a component won't be painted until its sized beyond 0x0

Before the contentPane can know how big it will be, it needs to be laid out by it's parent container. In order for the parent container to lay out the child container(s), it needs to know the size they might like to be.

As you say, this is related to the layout managers, in order for container to know how big it can be, it needs to know how much space it's parent container will give it.

Maybe this problem is caused by the layout manager, like most Container-related problems. But with a layout manager, I also get 0.

A container, even under the management of a layout manager, won't be assigned a size until the parent container is sized appropriately.

While there are several ways to overcome this, the question becomes why? The fact that you're setting the contentPane's layout manager to null is worrisome and would suggest that you're now going to need to reinvent the wheel and replace the job that the layout managers do for you anyway.

I would suggest calling pack before you try and get the component size, but, again, because you've negated the layout manager, this won't result in a value you would be happy with...

Upvotes: 3

Andy Brown
Andy Brown

Reputation: 19161

Presumably as this is in your main frames constructor you haven't called setVisible(true) on the main frame (the top level window) when you call JOptionPane.showMessageDialog?

If so then the GUI components haven't yet been rendered, so they have bounds of [0, 0]. If you move JOptionPane.showMessageDialog to after setVisible you'll get a dimension.

Alternatively you could call pack to lay things out. As per my comment below you would call contentPane.setPreferredSize(new Dimension(width, height)); before you do this.

Upvotes: 2

Related Questions