FormLayout: Grouping Example 10
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.com/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class FormLayoutExample10 extends JPanel {
public FormLayoutExample10() {
super(new BorderLayout());
FormDebugPanel panel = new FormDebugPanel();
FormLayout formLayout =
new FormLayout("p, p, p, p", "p, p");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("C1:", 30, 30), c.xy(1, 1));
panel.add(createLabel("C2:", 30, 40), c.xy(2, 1));
panel.add(createLabel("C3:", 30, 50), c.xy(3, 1));
panel.add(createLabel("C4:", 30, 60), c.xy(4, 1));
panel.add(createLabel("C5:", 30, 30), c.xy(1, 2));
panel.add(createLabel("C6:", 40, 30), c.xy(2, 2));
panel.add(createLabel("C7:", 50, 30), c.xy(3, 2));
panel.add(createLabel("C8:", 60, 30), c.xy(4, 2));
formLayout.setRowGroups(new int[][]{{1, 2}});
formLayout.setColumnGroups(new int[][]{{1, 3}});
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Grouping Example 10");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample10());
f.pack();
f.setVisible(true);
}
}
jgoodiesFormLayoutReady.zip( 98 k)Related examples in the same category