1

I have created class as below. When I create an object called drawpanel of this class and add to the frame using frame.getContentPane().add(BorderLayout.CENTER, drawpanel), I get a rectangle with black background as expected. Does it mean that the Panel's this.getWidth and this.getHeight by default has the same the height and width of the Frame (which is why it fills up the entire frame with black colour) ?

One other way I want to reframe my question is - If I create a custom widget by extending JPanel, and when this custom widget is instantiated, what is its default height and width ?

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g){
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
}

3 Answers3

1

Does it mean that the Panel's this.getWidth and this.getHeight by default has the same the height and width of the Frame (which is why it fills up the entire frame with black colour) ?

No, it means the frame has a layout/constraint (BorderLayout/CENTER) that will stretch the component to whatever size that will fill it.

If I create a custom widget by extending JPanel, and when this custom widget is instantiated, what is its default height and width ?

0x0 (without any components laid out).


The panel in which custom painting is done should return a preferred size for the content. Once added to the frame, call the pack() method and it will be the exact size it need to be in order to honor the preferred size of the component(s).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks - from your comment, I can understand that the CENTER constraint stretches the JPanel to whatever size that can be filled within the frame and that way when this.getWidth, this.getHeight is invoked, it returns the whatever width and height that has been occupied by the JPanel (within the frame) - Is that correct ? – Abhishek Palakkal Kaliyath Apr 25 '15 at 13:31
  • *"when this.getWidth, this.getHeight is invoked, it returns the whatever width and height that has been occupied by the JPanel (within the frame)"* Yes. – Andrew Thompson Apr 26 '15 at 00:44
  • Had there been a button on the bottom of frame, aligning the JPanel at centre would mean that the black colour will be filled only on remaining portion of frame (minus the button) - Or is it that the full frame is filled with black colour but because the button is placed at bottom, the black colour isn't visible (In other words, does the black colour get filled on the back of button as well) ? – Abhishek Palakkal Kaliyath Apr 26 '15 at 09:48
0

I think the JPanel by itself has a prefered size of 0,0. But if you add some components to it with a higher prefered size, its prefered size will not be 0.

Distjubo
  • 959
  • 6
  • 21
  • If that was the case, the fillRect call in my code would have translated to g.fillRect(0, 0, 0, 0) and no black colour would have been filled up in the frame ! – Abhishek Palakkal Kaliyath Apr 25 '15 at 13:18
  • No. It just draws outside of the component, aka it doesn't draw. But if you resize the JPanel then, it becomes visible because it gets redrawn. – Distjubo Apr 25 '15 at 13:20
  • Also, the actual size is NOT the prefered size! If the JPanel is the only component in a JFrame, its size is the same as the size of the JFrame. – Distjubo Apr 25 '15 at 13:22
  • Thanks - I got it - If the JPanel is the only component in a JFrame, its size is the same as the size of the JFrame - This is the answer I was looking for ! Thanks. – Abhishek Palakkal Kaliyath Apr 25 '15 at 13:25
  • Thanks for the reputation you got me @AbhishekPalakkalKaliyath :) The prefered size is, as its name says, only a prefered size :) – Distjubo Apr 25 '15 at 13:35
0

JPanel have default size 0 width and 0 height.

You can set the size:

  • manually: panel.setSize(), panel.setPreferredSize()
  • automatically: panel.pack()
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40