2

With this code I will have the following window. I created 2 panels and added the mainp one to the frame and the panel to the mainp I did this in order to make window resizing dynamic (so the panel wont resize to the frame) I tried making my default panel size wider so that the text fields and label become wider but panel.setsize doesn't seem to do anything.

enter image description here

// creates the labels
 studId = new JLabel("Student ID");
 studAvg = new JLabel("Student Average");
 studName = new JLabel("Student Name");

 // creates the text fields
 JTextField studIdText = new JTextField();
 JTextField studAvgText = new JTextField();
 JTextField studNameText = new JTextField();

 JPanel mainp = new JPanel();

 JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(3, 2, 2, 2));

            panel.setSize(300, 100);

    // adds to the GridLayout
    panel.add(studId);
    panel.add(studIdText);
    panel.add(studName);
    panel.add(studNameText);
    panel.add(studAvg);
    panel.add(studAvgText);

    mainp.add(panel);
    add(BorderLayout.CENTER,mainp);

    // verifies the textfields
    studIdText.setInputVerifier(new IntVerifier());
    studAvgText.setInputVerifier(new DoubleVerifier());

    setTitle("Student Form");
    setSize(300, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

enter image description here

Meesh
  • 508
  • 1
  • 5
  • 17

3 Answers3

3

The method you are looking for is setPreferredSize. Use it instead of panel.setSize(300, 100).

panel.setPreferredSize(new Dimension(300, 100));

I would also recommend to not setting the size of your JFrame to the fixed value (300,200) but do pack() instead. This will set the size of your JFrame to fit the panels inside.

halex
  • 16,253
  • 5
  • 58
  • 67
  • +1 for `pack()`; see also these [caveats](http://stackoverflow.com/q/7229226/230513). – trashgod Nov 06 '12 at 15:06
  • I'd strongly discourage the use of `setPreferredSize` where possible. You've made (all bit a simple example) an arbitrary decision of the size without consideration to things like the font, resolution, DPI, insets of other components that might work on the development machine, but may not appear the same on deployed machines (may of which I sure are pointed on by the link supplied by Trashgod). Instead, I'd probably simply use an `EmptyBorder` which provided additional padding ontop of the containers calculated preferred size...IMHO – MadProgrammer Nov 06 '12 at 19:23
  • @MadProgrammer `sing panel.setPreferredSize(new Dimension(400, 50));` get me sort of what i want but like you said that seems to be a hack, so i figured out that using a different layout might be the solution according to the link trashgod sent i posted the image and resizing horizontally and vertically wont stretch the labels and fields – Meesh Nov 07 '12 at 06:19
  • @MichelLayyous just remember, layout managers can ignore the pref/min/max sizes – MadProgrammer Nov 07 '12 at 07:07
  • @MadProgrammer checkout my answer and tell me what you think.. this is what i wanted better format – Meesh Nov 07 '12 at 08:15
2

using Advise from @Dan and @MADprogrammer and @trashgod i came up with the following

JTextField studIdText = new JTextField(20);

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints r1 = new GridBagConstraints();

r1.fill = GridBagConstraints.HORIZONTAL;
r1.weightx = 0.0;
r1.gridx = 0;
r1.gridy = 0;
panel.add(studId,r1);

r1.weightx = 0.5;
r1.gridx = 1;
r1.gridwidth = GridBagConstraints.REMAINDER; 
panel.add(studIdText,r1);

of course you can make GridBagConstraints for every row and just change the gridy

enter image description here

Meesh
  • 508
  • 1
  • 5
  • 17
1

Set the layout for mainp as BorderLayout.

mainp.setLayout(new BorderLayout());

Then, in order to avoid having the textfields resize vertically and look strange, you can add panel to BorderLayout.NORTH, for instance.

mainp.add(panel, BorderLayout.NORTH);
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • doing that would make my panel resize horizontally thou i want it to stay a fixed size horizontally and vertically and not stretch with the frame.but at the same time i want my Textfields wider but i see that using 'setPreferredSize' is bad practice – Meesh Nov 07 '12 at 06:16
  • Use a GridBagLayout and place the labels on one column and text fields on two columns. – Dan D. Nov 07 '12 at 06:32