Question: I am trying to achieve the following effect in Java:

![input panel](https://i.imgur.com/cXgdxG6.jpg)

However, I am not sure what layout to use and how. `FlowLayout` obviously doesn't work. `GridLayout` won't work either because the first 4 rows are supposed to be 1 column rows, but the 5th row needs to have 2 columns.

This is my code so far:

```
public class DepositPanel extends JPanel
{
    private JLabel cashL, checksL;
    private JTextField cashTF, checksTF;
    private JButton ok, cancel;

    DepositPanel()
    {
        JPanel depositP = new JPanel();
        depositP.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
        depositP.setPreferredSize(new Dimension(250, 85));

        JTextField cashTF = new JTextField(22);
        JTextField checksTF = new JTextField(22);

        JLabel cashL = new JLabel("Cash:");
        JLabel checksL = new JLabel("Checks:");

        ok = new JButton("OK");
        cancel = new JButton("CANCEL");

        depositP.add(cashL);
        depositP.add(cashTF);
        depositP.add(checksL);
        depositP.add(checksTF);
        depositP.add(ok);
        depositP.add(cancel):
    }
}

```

Answer: 
In this case you can use a `JOptionPane` to build a simple panel for you:

```
JTextField firstName = new JTextField(10);
JTextField lastName = new JTextField(10);

Object[] msg = {"First Name:", firstName, "Last Name:", lastName};

result = JOptionPane.showConfirmDialog(
    frame,
    msg,
    "Use default layout",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.PLAIN_MESSAGE);

if (result == JOptionPane.YES_OPTION)
{
    System.out.println(firstName.getText() + " : " + lastName.getText());
}
else
{
    System.out.println("Canceled");
}

```

The only problem with this approach is that the focus will be on a button, not the first name text field.

So to solve this problem you can check out the `RequestFocusListener` found in [Dialog Focus](https://tips4java.wordpress.com/2010/03/14/dialog-focus/) which will cause focus to be placed on the first name text field once the dialog is displayed.

```
JTextField firstName = new JTextField(10);
firstName.addAncestorListener( new RequestFocusListener() );

```

Although for more complex layouts it is better to create one or more panels each using an appropriate layout manager for the requirement.
Try this:

```
public class Window extends JFrame{
....
}

JLabel example;
//Constructor
public Window(){
    example = new JLabel("Sample text");
    example.setBounds(x,y,width,height)
    //JComponent...
    setLayout(null);
    setSize(width,height);
    setVisible(true);
}

```

Without the JPanel you can specify the x and y coordinates