2

I am making an interface with a JPanel inside a JFrame of which the width of the JPanel is set to be equal to the with of the JFrame. Contrary to my expectation though when I run the program to view the display, the JPanel displays as a very small box smaller than the required size which should have width equal to that of JFrame. However, when I reduce the width of the JPanel by about 6, it displays well it's exact size in the JFrame. Seems to me that the JFrame could have some kind of margin or padding but I don't know really. Please help and show why I cannot make the JPanel width same as that of the parent JFrame and also how it can be done if possible. I really want to have them same width cause that is how my design is set. My code is below:

`import java.awt.*; //importing awt package
import javax.swing.*; //importing swing package

//class definition
public class PMSysClient {

//declaration of variables
private JFrame lgFrame; //login frame
private JFrame fdFrame; //frame for front desk
private JFrame docFrame; //frame for doctor
private JPanel logHeadPan; //header panel in login
private JPanel logAreaPan; //login area panel

//constructor definition
public PMSysClient() {
    initialise();
}//end of constructor

//method to initialise class
public void initialise() {
    lgFrame = new JFrame();
    Container cont = lgFrame.getContentPane();
    lgFrame.setSize(863, 569);
    lgFrame.setLocationRelativeTo(null);
    lgFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cont.setBackground(new Color(231, 228, 204));
    cont.setLayout(new GridBagLayout());

    logHeadPan = new JPanel();
    logHeadPan.setBackground(new Color(204, 153, 255));
    logHeadPan.setPreferredSize(new Dimension(863, 210));
    GridBagConstraints c1 = new GridBagConstraints();
    c1.weighty = 0.1;
    c1.weightx=0.1;
    c1.gridx = 0;
    c1.gridy = 0;
    c1.anchor = GridBagConstraints.FIRST_LINE_START;

    lgFrame.add(logHeadPan, c1);
    lgFrame.setVisible(true);
}//end of initialise

//main method definition
public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception e) {
    }
    new PMSysClient();
}//end of main method
}`
ken
  • 299
  • 3
  • 15
  • 32

3 Answers3

4

As your aim is to have the width of the JPanel the same as the width of your parent JFrame, you can set the fill in your GridBagConstraints to:

c1.fill = GridBagConstraints.HORIZONTAL;

although this will produce a thin horizontal panel, so to get the height to expand in the Y axis also you can use:

c1.fill = GridBagConstraints.BOTH;

Better to leave sizing to Layoutmanagers and avoid setting preferred sizes for components.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • +1 Even a better suggestion then mine as you do not propose to switch `LayoutManager`s. Perhaps I should spend some time time learning that `GridBagLayout` after all – Robin Sep 30 '12 at 16:51
3

Just use the appropriate LayoutManager and do not use the setXXXSize methods. In case where you have a child component which should take over the complete width of its parent, a BorderLayout is good enough for the parent, and you can add the child to the NORTH. Quote from the layout manager tutorial

If the window is enlarged, the center area gets as much of the available space as possible. The other areas expand only as much as necessary to fill all available space. Often a container uses only one or two of the areas of the BorderLayout object — just the center, or the center and the bottom.

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
-1

Add the setMinimumSize() and keep setPreferredSize(). The sizes can be different. But you need to use both methods.

logHeadPan.setMinimumSize(...)
Mentallurg
  • 44
  • 2
  • 3
    [Why one should avoid to call the `setXXXSize` method](http://stackoverflow.com/q/7229226/1076463) (just to beat @kleopatra to it) – Robin Sep 30 '12 at 16:45
  • @ken: You might also want to look at the other solutions provided to this question. You would get some pretty helpful advice in those contributions as well :) – Sujay Sep 30 '12 at 16:56
  • @Robin: setXXSize() should not be avoided. This is a hint to a LayoutManager how much space a component needs independent on others. Of course setXXXSize should not be used INSTEAD of a proper LayoutManager. – Mentallurg Sep 30 '12 at 17:23
  • 2
    @Mentallurg I disagree. Each `Jxxx` know its size, based on its contents, and returns proper values for its sizes – Robin Sep 30 '12 at 20:12