1

Basically I am trying to attach two panels onto my frame, my frame uses BorderLayout and the two panels will be placed North and south. I am done with the top panel but for the bottom one I cannot get it right. I used GridLayout for the bottom one and it's supposed to look like this.

http://i47.tinypic.com/wa0lsz.png

So here is the code for the gridlayout

public class WeaponComp extends JPanel{


protected JPanel first = new JPanel();

public WeaponComp(){

    setLayout(new GridBagLayout());
    setPreferredSize(new Dimension(800,550));
    GridBagConstraints c = new GridBagConstraints();
    JLabel ntg = new JLabel("");
    JLabel oldweap = new JLabel("OLD WEAPON");
            JLabel newweap = new JLabel("NEW WEAPON");

    JLabel onetwohand = new JLabel ("1H / 2H");
    JLabel offhand = new JLabel ("Off Hand");
    JLabel dps = new JLabel ("DPS :");
    JLabel str = new JLabel ("Str :");
    JLabel dex = new JLabel ("Dex :");
    JLabel vit = new JLabel ("Vit :");
    JLabel intel = new JLabel ("Int :");
    JLabel manareg = new JLabel ("Mana Regen :");
    JLabel aspd = new JLabel ("Attack Speed:");
    JLabel critch = new JLabel ("Crit chance:");
    JLabel critdmg = new JLabel ("Crit damage:");

    JTextField dpstf = new JTextField(12);
    JTextField strtf = new JTextField(5);
    JTextField dextf = new JTextField(5);
    JTextField vittf = new JTextField(5);
    JTextField inteltf = new JTextField(5);
    JTextField manaregtf = new JTextField(3);
    JTextField aspdtf = new JTextField(3);
    JTextField critchtf = new JTextField(3);
    JTextField critdmgtf = new JTextField(3);
    JTextField offdpstf = new JTextField(12);
    JTextField offstrtf = new JTextField(5);
    JTextField offdextf = new JTextField(5);
    JTextField offvittf = new JTextField(5);
    JTextField offinteltf = new JTextField(5);
    JTextField offmanaregtf = new JTextField(3);
    JTextField offaspdtf = new JTextField(3);
    JTextField offcritchtf = new JTextField(3);
    JTextField offcritdmgtf = new JTextField(3);

    first.setLayout(new GridLayout(3,4));
    first.setPreferredSize(new Dimension(750,150));

    first.add(oldweap); first.add(ntg); first.add(newweap); first.add(ntg);
    first.add(onetwohand); first.add(ntg); first.add(offhand); first.add(ntg);
    first.add(dps); first.add(dpstf); first.add(dps); first.add(offdpstf);


    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0; c.gridy = 0;
    add (first,c); 

}
}

Here is the current result of my program

http://i50.tinypic.com/107p9uu.png

Thank you in advance for your time and answers

PS : and in case you're wondering, yes, it has something to do with diablo 3 But I am not that ambitious, this is for learning purpose and won't have many functionality.

Surender Thakran
  • 3,958
  • 11
  • 47
  • 81
Alvin Sanda
  • 53
  • 2
  • 5

2 Answers2

3

GridLayout is a poor choice, because all cells automatically have the same size. I suggest using MigLayout instead: http://miglayout.com

The layout code would look like this:

first.setLayout(new MigLayout("wrap 2, fill"));
first.add(oldweap);
first.add(newweap);
first.add(onetwohand);
first.add(offhand);
first.add(dps);
first.add(dpstf);
first.add(dps);
first.add(offdpstf);
Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
  • +1, MigLayout is amost always a good option for many JComponents in 1 JPanel. – Hidde May 28 '12 at 15:59
  • Thanks Emmanual, I will look at the miglayout but this does not solve my curiosity, I thought grid is perfectly fine with 3 rows and 4 columns in this particular case but having their order messed is strange and this has made me really curious. I am still looking for a solution based on gridlayout, but I will still check out the mig, thank you once again. – Alvin Sanda May 28 '12 at 16:05
  • GridLayout will really look horrible, the textfields will have a non standard height and the text baselines will not be aligned. If you really want to see how it looks, you have to duplicate the 'ntg' blank element, because an object can only be added once to a container. – Emmanuel Bourg May 28 '12 at 16:21
  • I think that's the answer I'm looking for, ntg can be used only once Thank you so much for this – Alvin Sanda May 28 '12 at 16:45
3

You might use GroupLayout1 or a nested layout2 for the bottom panels.

  1. See How to Use GroupLayout for details. E.G. enter image description here
  2. E.G.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433