1,105,254 Community Members

Alignment problems of objects within JPanel using Gridbag Layout

M-rhodes
Newbie Poster
1 post since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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
        getContentPane().add(TabGroup);

        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(); 
        lblBlank.setText("1"); 


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

        //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;
        panelAddEmployee.add(lblAddEmployee,gbc);

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

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

        panelAddEmployee.add(lblBlank,gbc);

        //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);

    }









}
Member Avatar
JamesCherrill
... trying to help
10,363 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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! ;)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: