Hi, I'am creating a single JFrame UI, that uses multiple JPanels (implemented through JTabbedPane). The problem I'am having is aligning the objects within each JPanel. In the first JPanel all of the objects are centrally aligned. I'am unsure of what to do to solve the problem. I have read the Javadoc regarding Gridbag Layout and I've had no luck.

Here's a link to a screen shot of the UI problem: http://www.dreamincode.net/forums/uploads/monthly_11_2012/post-422498-13540326851245.png

All help is appreciated,

This is the UI class:

import java.awt.*;

import javax.swing.*;

public class Main_Window_Frame extends JFrame{

    public Main_Window_Frame() {

        super("Employee Record System");

        setSize(750,450); //set the size of the form
        //The tabbed pane object is constructed
        JTabbedPane TabGroup = new JTabbedPane();

        //This creates the template for the multiple tabs using JTabbedPane

        JPanel panelAddEmployee = new JPanel((LayoutManager)new FlowLayout(FlowLayout.LEFT));//This will create the first tab "Add Employee"

        JPanel panelSearchEmployee = new JPanel();//This will create the second tab "Search for an Employee"

        JPanel panelRemoveEmployee = new JPanel();//This will create the third tab "Remove an Employee"

        JPanel panelListAllEmployees = new JPanel();//This will create the fourth tab "List all Employees"

        JPanel panelSysOptions = new JPanel();//This will create the fifth tab "System options"

        //Add objects that are related to the Add employee panel

        //Create the Add Employee label
        JLabel lblAddEmployee = new JLabel(); //Construct label "Add Employee"
        lblAddEmployee.setText("Add Employee"); //Set the text to "Add Employee"
        lblAddEmployee.setFont(new Font("Arial",Font.BOLD,24)); //Sets the font type to Arial bold, size 24

        //Create the Employee ID label
        JLabel lblBlank = new JLabel(); 

        //Create the Employee ID label
        JLabel lblEmpID = new JLabel(); 
        lblEmpID.setText("Employee ID:"); 
        lblEmpID.setFont(new Font("Arial",Font.PLAIN,12));

        //Implements the grid bag layout for the Add employee panel
        panelAddEmployee.setLayout(new GridBagLayout());
        //gbc is used to specify a components display area and defines positioning of objects
        GridBagConstraints gbc = new GridBagConstraints();

        //Setup external padding of objects

        gbc.insets = new Insets(10,10,10,10);
        gbc.anchor = (gbc.gridx == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.NORTHWEST;

        gbc.anchor = (gbc.gridx == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
        gbc.gridx = 0;
        gbc.gridy = 1;

        gbc.anchor = (gbc.gridx == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
        gbc.gridx = 1;
        gbc.gridy = 1;


        //This adds the first, second, third and final tab to the tabbedPanel Object
        TabGroup.addTab("Add Employee", panelAddEmployee);
        TabGroup.addTab("Search for an Employee", panelSearchEmployee);
        TabGroup.addTab("Remove an Employee", panelRemoveEmployee);
        TabGroup.addTab("List all Employees", panelListAllEmployees);
        TabGroup.addTab("System Options", panelSysOptions);


4 Years
Discussion Span
Last Post by JamesCherrill

You have not explained how you would like the panel laid out - you just said that centrally aligned is a "problem". Nobody can tell you how to write the code for an unknown layout! ;)

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.