Skip to content

Instantly share code, notes, and snippets.

@mverzilli
Created May 28, 2015 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mverzilli/679f0e4bc9a7ba6a5f9c to your computer and use it in GitHub Desktop.
Save mverzilli/679f0e4bc9a7ba6a5f9c to your computer and use it in GitHub Desktop.
package simurrythmia.ui.util;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class LayoutUtils {
public static JPanel createPanel(String title) {
JPanel panel = createPanel();
panel.setBorder(BorderFactory.createTitledBorder(title));
return panel;
}
public static JPanel createPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
return panel;
}
public static GridBagConstraints layoutControls(JPanel panel, JComponent[][] controlMatrix){
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 10, 5, 10);
c.fill = GridBagConstraints.BOTH;
for (int row = 0; row < controlMatrix.length; row++){
for (int col = 0; col < controlMatrix[row].length; col++){
c.gridx = col;
c.gridy = row;
panel.add(controlMatrix[row][col], c);
}
}
return c;
}
public static JPanel padding(){
return new JPanel();
}
}
//Main panel es un contenedor, controlMatrix es una matriz de
//componentes de Swing que se van a entrar en un table layout
//de acuerdo a la matriz.
//LayoutUtils.padding() es para poner "aire" en alguna de las celdas de la matriz :P
JComponent[][] controlMatrix = new JComponent[][] {
new JComponent[] { constant, periodLabel, period, msLabel },
new JComponent[] { variable, addReferenceValue },
new JComponent[] { LayoutUtils.padding(), scrollPane }
};
LayoutUtils.layoutControls(mainPanel, controlMatrix);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment