Hello.

I've got simple JFrame and into that I put JPanel (object of subclass of JPanel class). I want to check, what's this panel's size from inside of this panel class, but every method (getSize, getBounds) returns [0,0]. If I call getSize method from JFrame class, then it's fine, but how to get same result calling getSize from MyPanel class?

Is it possible at all?!

Code is nothing complicated or unusual, but what could be maybe important, I create frames in drag&drop interface (NetBeans 6.5.1).

Simply: I have a frame, put some panel into that, where my panel is:

public class MyPanel extends JPanel
{
    public MyPanel()
    {
        System.out.println( getSize().height );    //    result -> 0.0
    }
}

When call is from parent frame:

public class MainFrame extends JFrame
{
    (...)
    public MainFrame()
    {
        initComponents();    //    placing panel etc.

        System.out.println( m_panel.getSize().height );    //    result -> correct
    }

    private JPanel m_panel;
}

As I said, I'm creating GUI by placing components with mouse, so calling setSize can't help too much here.


Looks basic to me, but can't find/understand that, why isn't it returning correct value...

From my assessment, the JPanel is sized according to the LayoutManager of the container that it is in. In this case, it is in the JFrame. So calling getSize from the JPanel's constructor doesn't make much sense. When it is still in the constructor, your JPanel isn't even finished creating yet, so how could it's height possibly be anything other than 0 unless you explicitly set it to something other than 0? And the JPanel also isn't visible yet, so its height will be 0. In any case, consider this code:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.add(new JButton("what"));
		frame.add(panel);
		frame.setSize(200,200);
System.out.println(panel.getSize().height);
		frame.setVisible(true);
System.out.println(panel.getSize().height);
	}

This prints:
0
166

This example is meant to show you that if your panel is inside another Component, such as my panel being inside the JFrame, the panel will be resized according to the size of the JFrame. However, in your code that you are having trouble with, since your panel hasn't finished being created (and your frame has not yet been setVisible), the JFrame has not resized the panel yet. Therefore, its height is 0.

Now consider this piece of code:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		JPanel panel = new JPanel();
		panel.setSize(0,166);
		System.out.println(panel.getSize().height);
	}

This is just meant to show you that one way the panel could have a height other than 0 is if you explicitly set the height to something other than 0, as I did by saying panel.setSize(). If you were to do this in your JPanel's constructor, then immediately print out the height, you'd find that it is no longer 0. You could also call panel.getSize().height after your frame is visible. But be warned that if you explicitly call panel.setSize() on your JPanel, once the JFrame is set visible, the JFrame will probably set the Jpanel's size (so that the panel fits properly inside the JFrame). This might lead to you setting the JPanel's size, and then printing it out, and getting what you think are wrong results.

Yes, that's it. I was trying to set size/height/width/whatever in constructor or next to field declaration.

Now it won't drive me crazy again. :).

Thanks.

This question has already been answered. Start a new discussion instead.