Reputation: 14049
I am trying to learn Swing Programming.
I have read the Java Documentation on different kinds of layouts. I have read a few tutorials also. But I am not really able to figure out what Layout is to be used for anything more than a very simple Dialog. I want to do this by code (not through WindowBuilder Pro) just so that I get a hang of it.
This is a Dialog I want to build.
Nothing except the Notes is editable.
What is best Layout to use for this kind of Dialog?
Upvotes: 1
Views: 886
Reputation: 11657
MigLayout
first, GroupLayout
second.
A sample solution with MigLayout
:
package com.zetcode;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
/**
* A practical example of MigLayout manager.
* @author Jan Bodnar
* Website: zetcode.com
*/
public class CustomerDetailsMigLayoutEx extends JFrame {
public CustomerDetailsMigLayoutEx() {
initUI();
}
private void initUI() {
JLabel custId1 = new JLabel("Cust Id");
JLabel custId2 = new JLabel("A52501235");
JLabel name1 = new JLabel("Name");
JLabel name2 = new JLabel("Joe Beer");
JLabel address1 = new JLabel("Address");
JLabel address2 = new JLabel("112, 1st Street, City, State, Country");
JLabel orders = new JLabel("<html><u style='font-size:13px'>Last 3 Orders</u></html>");
JLabel date1 = new JLabel("11 Dec 2015");
JLabel date2 = new JLabel("17 Dec 2015");
JLabel date3 = new JLabel("19 Dec 2015");
JTextArea area1 = new JTextArea(7, 28);
area1.setBorder(BorderFactory.createEtchedBorder());
JTextArea area2 = new JTextArea(7, 28);
area2.setBorder(BorderFactory.createEtchedBorder());
JTextArea area3 = new JTextArea(7, 28);
area3.setBorder(BorderFactory.createEtchedBorder());
JTextArea area4 = new JTextArea(7, 28);
area4.setBorder(BorderFactory.createEtchedBorder());
JButton btn1 = new JButton("Submit");
JButton btn2 = new JButton("Cancel");
createLayout(custId1, custId2, name1, name2, address1, address2,
orders, date1, area1, date2, area2, date3, area3,
area4, btn1, btn2);
setTitle("MigLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
setLayout(new MigLayout("insets dialog, align 50% 50%, gap 5lp 7lp"));
add(arg[0], "split 2, sgx");
add(arg[1], "gapx 15lp, wrap");
add(arg[2], "split 2, sgx");
add(arg[3], "gapx 15lp, wrap");
add(arg[4], "split 2, sgx");
add(arg[5], "gapx 15lp, wrap");
add(arg[6], "gapy unrel, wrap");
add(arg[7], "gapy rel, split 2");
add(arg[8], "wrap");
add(arg[9], "split 2");
add(arg[10], "wrap");
add(arg[11], "split 2");
add(arg[12], "wrap");
add(arg[13], "growx");
add(arg[14], "split 2, flowy");
add(arg[15]);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
CustomerDetailsMigLayoutEx ex = new CustomerDetailsMigLayoutEx();
ex.setVisible(true);
});
}
}
Screenshot:
Upvotes: 2
Reputation: 363
MigLayout - for Java developers writing GUI layouts by hand that wants simplicity and power.
Upvotes: 5
Reputation: 701
In order to perform your kind of layout, you can use GridBagLayout or GroupLayout.
Here a usefull link to understand layouts: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
As seen on the diagram, you'll need to have some columns (2 by row).
In order to do this,
GridBagLayout aligns components by placing them within a grid of cells.
Whereas GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently.
So for your example, you'll have to define 3 Panels (CustInfo, Lastorders and Notes), defined by a Group or GridBagLayout
Upvotes: 5