5

I am trying to achieve the following effect in Java:

input panel

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):
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
user2218567
  • 94
  • 1
  • 9
  • 1
    There are many ways you might achieve this, have a look at [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) for more details – MadProgrammer Dec 09 '15 at 22:57

4 Answers4

6

You could try with combinations of Layouts, 2 JPanels, 1 for buttons and 1 for fields, button panel with FlowLayout and fields panel with BoxLayout. And adding them to the frame. (I did a JFrame for testing, but you can change it to a JPanel and add that panel to your JFrame). Just be sure to have only 1 JFrame, see The use of multiple JFrames, Good / Bad Practice.

enter image description here

For example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DepositExample {
    JFrame frame;
    JPanel buttonPane, fieldsPanel;
    JLabel cash, checks;
    JTextField cashField, checksField;
    JButton ok, cancel;

    DepositExample() {
        frame = new JFrame("Deposit");
        buttonPane = new JPanel();
        fieldsPanel = new JPanel();
        cash = new JLabel("Cash");
        checks = new JLabel("Checks");
        cashField = new JTextField("");
        checksField = new JTextField("");
        ok = new JButton("OK");
        cancel = new JButton("Cancel");

        fieldsPanel.setLayout(new BoxLayout(fieldsPanel, BoxLayout.PAGE_AXIS));
        buttonPane.setLayout(new FlowLayout());

        fieldsPanel.add(cash);
        fieldsPanel.add(cashField);
        fieldsPanel.add(checks);
        fieldsPanel.add(checksField);
        buttonPane.add(ok);
        buttonPane.add(cancel);
        frame.add(fieldsPanel, BorderLayout.PAGE_START);
        frame.add(buttonPane, BorderLayout.PAGE_END);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String args[]) {
        new DepositExample();
    }
}

To get some more spacing between components you can add EmptyBorders as recommended by @LuxxMiner in his comment below.

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1
    (+1), the only thing that bothers me is that you add `buttonPane` to `BorderLayout.CENTER`, because I feel like the standard place for an option panel with buttons would be `BorderLayout.PAGE_END` / `BorderLayout.SOUTH`. But I guess it doesn't make a difference anyway, because the OP wants it as a panel. @DavidBarroso: Also you could achieve the 'padding' with an `EmptyBorder` ([tutorial for borders](https://docs.oracle.com/javase/tutorial/uiswing/components/border.html)). – Lukas Rotter Dec 09 '15 at 19:48
  • @LuxxMiner yeah I'll fix it, I wasn't sure on where to place it. I forgot to place the padding part too, thanks :) – Frakcool Dec 09 '15 at 19:52
4

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 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.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

There are many ways to achieve a layout like this. The first thing you need to get used to, is that its often simpler to split up different requirements into different containers using different layout managers.

If you separate the two buttons into their own panel and treat that panel with the buttons as "just another line" in the window, you can basically just use a GridLayout with a single column. The panel with the buttons could then use a FlowLayout to place the buttons side by side.

Durandal
  • 19,919
  • 4
  • 36
  • 70
-2

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

  • Thanks but this won't work. I need to create a JPanel Class that I can use to instantiate Jpanel objects in main and add them to JFrames. – user2218567 Dec 09 '15 at 18:51
  • 3
    See: [Why is it frowned upon to use a null layout in Swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and [Null Layout is Evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) to read the reasons why I downvoted and you shouldn't use and / or recommend the use of Null Layout. – Frakcool Dec 09 '15 at 19:02