Here you can find the source of createHorizontalBox(int[] ratios, Component... comps)
public static JPanel createHorizontalBox(int[] ratios, Component... comps)
//package com.java2s; /*//from w w w. j av a 2 s . co m * Spirit, a study/biosample management tool for research. * Copyright (C) 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91, * CH-4123 Allschwil, Switzerland. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/> * * @author Joel Freyss */ import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.Box.Filler; import javax.swing.JPanel; import javax.swing.border.Border; public class Main { public static JPanel createHorizontalBox(Border border, Component... comps) { JPanel res = createHorizontalBox(comps); res.setBorder(border); return res; } public static JPanel createHorizontalBox(Component... comps) { return createHorizontalBox((int[]) null, comps); } public static JPanel createHorizontalBox(int[] ratios, Component... comps) { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = GridBagConstraints.RELATIVE; c.gridy = 0; c.fill = GridBagConstraints.BOTH; for (int i = 0; i < comps.length; i++) { Component comp = comps[i]; if (comp == null) continue; if (comp instanceof Filler && ((Filler) comp).getMinimumSize().width == 0) { c.weightx = c.weighty = 1; } else { c.weightx = ratios == null || i >= comps.length ? 0.01 : ratios[i]; ; c.weighty = 1; } panel.add(comp, c); } panel.setOpaque(false); return panel; } }