Java Swing How to - Combine GridLayout and BorderLayout








Question

We would like to know how to combine GridLayout and BorderLayout.

Answer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
/*w  ww  .j a v  a 2  s. c o m*/
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class Main {

  public static void main(String[] args) {
    JPanel ui = new JPanel(new BorderLayout(20, 20));

    ui.setBorder(new LineBorder(Color.RED, 1));

    JTextField fileName = new JTextField();
    ui.add(fileName, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 30));
    ui.add(buttonPanel, BorderLayout.CENTER);

    JButton creater = new JButton("Create File");
    buttonPanel.add(creater);
    JButton deleter = new JButton("Delete File");
    buttonPanel.add(deleter);

    JOptionPane.showMessageDialog(null, ui);

  }
}