0

I've been trying to use a GridBagLayout to organize a bunch of panels (containing a bunch of labels which alter the size of the panel, mostly vertically). The major reason why i chose this is because the panel sizes can vary quite different, and I would like the panels to scale when the frame size is changed.

I keep coming across problems ranging from different sized panels (checking via borders on panels), weird sizing issues, cant get the panels to stick to each other vertically (massive space inbetween panels).

Gridbag seemed like the best option but it seems like its becoming a bigger pain than i imagined. What other options provide proper scaling when altering the frame size?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Eric G
  • 928
  • 1
  • 9
  • 29
  • 2
    1) look into use of MigLayout, a 3rd party layout manager that you will have to download. 2) or if you want our help to help you to optimize GridBagLayout or any other layouts, come back here with code, preferably a [mcve], and with images of your desired and observed GUI's. – Hovercraft Full Of Eels Oct 05 '15 at 21:14
  • Edit - Found download links, but not sure which one to download, so many versions. – Eric G Oct 06 '15 at 12:40

1 Answers1

3

I have become a fan of what @SplungeBob showed me a long time ago. The code snippet below gives you an idea of what it takes to accomplish the GUI shown below it.

    frame.add(menu,             new GBConstraints( 0, 0).spanX(32).fill(HORIZONTAL));
    frame.add(new JLabel("Available letters"), 
                                new GBConstraints( 0, 1).insets( 2, 2, 0, 2));
    frame.add(new JLabel("Play by play..."), 
                                new GBConstraints( 2, 1).insets( 0, 5, 0, 0));
    frame.add(pnlPlays,         new GBConstraints( 2, 1).insets(10, 0, 0, 0).spanY(14).anchor(WEST).fill(BOTH));//.ipad(0,0));

    frame.add(pnlAvail,         new GBConstraints( 0, 2).insets(0, 5, 5, 5));
    frame.add(pnlLegend,        new GBConstraints( 1, 2)                    .spanY(14)              .fill(BOTH));
    frame.add(pnlBonuses,       new GBConstraints( 0, 3).insets(0, 100, 0, 0));
    frame.add(pnlSBBonuses,     new GBConstraints( 0, 3).insets(0, 125, 0, 0));
    frame.add(gamePanel,        new GBConstraints( 0, 5).insets(0, 50, 0, 0));
    frame.add(pnlTheScore,      new GBConstraints( 0, 5)                                .anchor(EAST));
    btnDebug.setPreferredSize(new Dimension(1, 1));
    frame.add(btnDebug,         new GBConstraints( 0, 0)                                .anchor(WEST));

    frame.add(btnNextReplayStep,new GBConstraints( 0, 6).insets(0,310, 0, 0)            .anchor(CENTER));
              btnNextReplayStep.setVisible(false);
    frame.add(new JLabel("User letters"),
                                new GBConstraints( 0, 6).insets(0,240, 30, 0));
    frame.add(btnCopyUser,      new GBConstraints( 0, 6).insets(0,310,0,0)              .anchor(CENTER));

    frame.add(btnCopyAllLetters,new GBConstraints( 0, 6)                                .anchor(EAST));
    frame.add(pnlNotes,         new GBConstraints( 0, 6).insets(25,10,10, 10).spanY(  2).anchor(WEST));
    frame.add(btnClearMnemHi,   new GBConstraints( 0, 7).insets(0,430, 0, 0));
    frame.add(btnClearBorders,  new GBConstraints( 0, 7).insets(0,530, 0, 0));

enter image description here

I won't lie; it can still be a major task, but with careful planning with an actual grid marked off in vertical and horizontal lines to show, e.g., how many columns a field must span, it's worth a try. And chances are excellent that what @Hovercraft suggests is even better.

(I haven't mastered it, it seems as the weird insets values seem to indicate. But it's a nice-looking reliable GUI.)

EDIT

Here's code for how two of the JPanel objects were created prior to executing the code above. One also uses GridBagLayout; other doesn't. Just whatever works easiest and best.

  private void makePlays(){  
    pnlPlays = new JPanel(new GridBagLayout());
    pnlPlays.setSize(500,300);
    pnlPlays.add(scrPlays, new GBConstraints(0,1).span(40, 62)
                                                 .fill(BOTH).ipad(100, 50));
    scrPlays.setEnabled(false);
  }

  private void makeScoremasterBonuses(){
    BonusBackgroundColors c;
    BonusBackgroundColors all   [] = BonusBackgroundColors.values();
    String labels[] = new String[all.length];
    String abbrs [] = new String[all.length];

    JButton but;
    pnlBonuses = new JPanel();
    pnlBonuses.add(new JLabel("Legend:"));

    for (int i = 0; i < all.length; i++) {
      labels[i] = all[i].name().replace("_", " ").toLowerCase();
      abbrs [i] = Utilities.abbreviate(all[i].name());
      c = BonusBackgroundColors.values()[i];
      but = new JButton(abbrs[i]);
      but.setToolTipText(labels[i]);
      but.setBackground(c.background(c));
      but.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      but.setActionCommand("" + i);
      but.addActionListener(this);
      pnlBonuses.add(but);
    }
  }
Community
  • 1
  • 1
DSlomer64
  • 4,234
  • 4
  • 53
  • 88