GUI JPanel & frame layout

I want to ask some question about Java GUI, especially on JPanel syntax. I'm really confused how to configure the panel exactly, try to use BorderLayout, and GridLayout, but it still doesn't meet my requirement.

I want to make form like below:

Current Result:

From the image, I need to configure that 2 points

  1. The padding / margin between Input Panel and the Button Panel
  2. The Padding / margin between tablePanel and the inputPanel

Code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;


public class manageForm extends JFrame {
    //Vector<String> header=new Vector<String>();
    //Vector<Vector<String>>data=new Vector<Vector<String>>();

    DefaultTableModel defaultTableModel;
    JTable table;
    JScrollPane scrollPane;

    JLabel titleLabel=new JLabel("Manage Handphone");

    JTable hpTable = new JTable();

    JLabel idLabel = new JLabel("ID");
    JLabel nameLabel = new JLabel("Name");
    JLabel priceLabel = new JLabel("Price");
    JLabel weightLabel = new JLabel("Weight");
    JLabel cableLabel = new JLabel("Cable Length");
    JLabel typeLabel = new JLabel("Type");

    JTextField idtxt = new JTextField();
    JTextField nametxt = new JTextField();
    JTextField pricetxt = new JTextField();
    JTextField weighttxt = new JTextField();
    JTextField cabletxt = new JTextField();

    JComboBox typeBox = new JComboBox();

    JButton insertBtn = new JButton("INSERT");
    JButton updateBtn = new JButton("UPDATE");
    JButton deleteBtn = new JButton("DELETE");
    JButton confirmBtn = new JButton("CONFIRM");
    JButton cancelBtn = new JButton("CANCEL");

    String header[] = {"ID","Name","Type","Price","Weight","Cable Length"};
    String data[][] = {
            {"2","Bose Quite","In-Ear","Price","Weight","Cable Length"},
            {"2","Bose Quite","In-Ear","Price","Weight","Cable Length"},
            {"2","Bose Quite","In-Ear","Price","Weight","Cable Length"}

    };


    public manageForm() {

        JPanel headerPanel = new JPanel();
        JPanel tablePanel = new JPanel();
        tablePanel.setBorder(new EmptyBorder(0,0,0,0));
        JPanel inputPanel = new JPanel(new GridLayout(6,2));
        inputPanel.setBorder(new EmptyBorder(20,10,20,10));
        //inputPanel.setBorder(new LineBorder(Color.black));
        JPanel buttonPanel = new JPanel(new GridLayout(1,5));
        buttonPanel.setBorder(new EmptyBorder(100,20,100,20));
        JPanel footerPanel = new JPanel(new GridLayout (2,1,0,0));


        headerPanel.add(titleLabel);

        inputPanel.add(idLabel);
        inputPanel.add(idtxt);
        inputPanel.add(nameLabel);
        inputPanel.add(nametxt);
        inputPanel.add(priceLabel);
        inputPanel.add(pricetxt);
        inputPanel.add(weightLabel);
        inputPanel.add(weighttxt);
        inputPanel.add(cableLabel);
        inputPanel.add(cabletxt);
        inputPanel.add(typeLabel);
        inputPanel.add(typeBox);

        buttonPanel.add(confirmBtn);
        buttonPanel.add(cancelBtn);
        buttonPanel.add(insertBtn);
        buttonPanel.add(updateBtn);
        buttonPanel.add(deleteBtn);


        footerPanel.add(inputPanel);
        footerPanel.add(buttonPanel);

/*
        JPanel panel0=new JPanel();
        JPanel panel1=new JPanel();
        JPanel panel2=new JPanel();
        JPanel panel3=new JPanel();
        JPanel panel4=new JPanel();
        JPanel panel5=new JPanel();
        JPanel panel6 = new JPanel();
        JPanel panel7 = new JPanel();
        JPanel panel8 = new JPanel();



        panel1.setLayout(new GridLayout(1, 6));



        panel2.setLayout(new GridLayout(1, 2));
        panel2.add(idLabel);
        panel2.add(idtxt);

        panel3.setLayout(new GridLayout(1, 2));
        panel3.add(nameLabel);
        panel3.add(nametxt);

        panel4.setLayout(new GridLayout(1, 2));
        panel4.add(priceLabel);
        panel4.add(pricetxt);

        panel5.setLayout(new GridLayout(1, 2));
        panel5.add(weightLabel);
        panel5.add(weighttxt);

        panel6.setLayout(new GridLayout(1, 2));
        panel6.add(cableLabel);
        panel6.add(cabletxt);

        panel7.setLayout(new GridLayout(1, 2));
        panel7.add(typeLabel);
        panel7.add(typeBox);

        panel8.setLayout(new GridLayout(1, 5));


        mainPanel.add(panel0);
        mainPanel.add(panel1);
        mainPanel.add(panel2);
        mainPanel.add(panel3);
        mainPanel.add(panel4);
        mainPanel.add(panel5);
        mainPanel.add(panel6);
        mainPanel.add(panel7);
        mainPanel.add(panel8);
    */  

        /*
        header.add("ID");
        header.add("Name");
        header.add("Type");
        header.add("Price");
        header.add("Weight");
        header.add("Cable Length");
        */

        defaultTableModel=new DefaultTableModel(data, header);
        hpTable =new JTable(defaultTableModel);
        scrollPane=new JScrollPane(hpTable);
//      tablePanel.add(scrollPane);


        setLayout(new BorderLayout(0,5));
        setTitle("Manage Form");
        setSize(800,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        add(headerPanel,BorderLayout.NORTH);
        add(scrollPane,BorderLayout.CENTER);
        add(footerPanel,BorderLayout.SOUTH);
        pack();
    }


    public static void main(String[] args) {
    new manageForm();   
    }

}

How exactly to manage the panel layout to meet our requirement?

I have tried EmptyBorder, but the result, it's only making a big space between the other panel.

Upvotes: 0

Views: 1478

Answers (2)

Blasanka
Blasanka

Reputation: 22427

Use GridBagLayout it is a powerful and best fit to your requirement. It is arranges the components in a horizontal and vertical manner.

You can put component in a grid using cells and rows. As a example:

enter image description here

read more - GridBagLayout

Upvotes: 2

CarlG
CarlG

Reputation: 1666

Strongly disagree with the suggestion to use GridBagLayout. That layout is basically a punchline for any Swing programmer.

If you're going to be programming Swing, do yourself a huge favor and use MiGLayout. It's the layout manager that Swing should have built-in.

Upvotes: 1

Related Questions