Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:33:22+00:00 2026-06-14T17:33:22+00:00

I have to create a form that has input values for things such as

  • 0

I have to create a form that has input values for things such as username, password, and other things. here is a link to an image of what it looks like, http://img196.imageshack.us/img196/1727/empdesign.png

I want that all these attributes are alligned to the edge of the screen, and not centered. I tried using the method JComponenet.setHorizontalAlignment(SwingConstants.LEFT), but that also does nothing. I tried that method on JLabels, and JPanels, but neither have an effect.

Here is my code:

public class tada extends GUIDesign{

  //making all the jlabels to be placed

JLabel usernameLabel = new JLabel("username");
JLabel passwordLabel = new JLabel("Name");
JLabel empIDLabel = new JLabel("empID");
JLabel SalaryLabel = new JLabel("Salary");
JLabel EmployerLabel = new JLabel(" Salary");
JLabel hoursLabel = new JLabel("Gender");

JLabel departmentLabel = new JLabel("Department");



 //making all the textfields, combo boxes, and etc. 
JTextField usernameField = new JTextField(20);
JTextField passwordField = new JTextField(20);
JTextField empIDField = new JTextField(20);


JTextField SalaryField = new JTextField(20);
JTextField EmployerField = new JTextField(20);
String[] hourss = {"Fall", "Spring", "Summer"};
JComboBox hoursBox = new JComboBox(hourss);

JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"), 
                                        new JCheckBox("Department 2"), 
                                        new JCheckBox("Department 3"), 
                                        new JCheckBox("Department 4"), 
                                        new JCheckBox("Department 5")};






String[] Salary = {"section", "combo", "box"};
JComboBox sectionBox = new JComboBox(Salary);


//making all the panels to be placed inside the main panel. 

JPanel usernamePanel = new JPanel();
JPanel passwordPanel = new JPanel();
JPanel empIDPanel = new JPanel();
JPanel SalaryPanel = new JPanel();
JPanel EmployerPanel = new JPanel();
JPanel hoursPanel = new JPanel();
JPanel departmentsPanel = new JPanel();

JPanel top, bottom;


JButton submitButton = new JButton("Submit");

public tada(){

//this initializes the panel from superclass. nothing really important inherited from superclass.
super(“employer design”, 10);

    setPreferredSize(new Dimension(400,600));

//adding all the labels, text fields, etc to the sub-panels, and adding subpanels to main pannel, bottom.
setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
top = new JPanel();
top.setSize(getWidth(), 30);
bottom = new JPanel();
bottom.setLayout(new BoxLayout(bottom,BoxLayout.Y_AXIS));

    top.setBackground(Color.red);
    title.setFont(new Font("Helvetica", 1,20));
    top.add(title);
    bottom.setBackground(Color.yellow);

    usernamePanel.add(usernameLabel);
    usernamePanel.add(usernameField);
    bottom.add(usernamePanel);

    passwordPanel.add(passwordLabel);
    passwordPanel.add(passwordField);
    bottom.add(passwordPanel);

    empIDPanel.add(empIDLabel);
    empIDPanel.add(empIDField);
    bottom.add(empIDPanel);

    SalaryPanel.add(SalaryLabel);
    SalaryPanel.add(SalaryField);
    bottom.add(SalaryPanel);

    EmployerPanel.add(EmployerLabel);
    EmployerPanel.add(EmployerField);
    bottom.add(EmployerPanel);

    hoursPanel.add(hoursLabel);
    hoursPanel.add(hoursBox);
    bottom.add(hoursPanel);

    departmentsPanel.add(departmentLabel);
    for(JCheckBox jbc: departmentCheckBoxesBoxs)
    {
        departmentsPanel.add(jbc);
    }
    bottom.add(departmentsPanel);




    add(top);
    add(bottom);


}

//also, since I am using a boxLayout(), and it is alligned to the y_axis, I thought that it would atomatically do the y-axis allignment, but it doesn’t.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:33:23+00:00Added an answer on June 14, 2026 at 5:33 pm

    The simplest method I can see, given your code, is to simply change the main containers layout manager. Here I’ve used grid bag layout, as it allows me to change the individual requirements each component as need

    enter image description here

    public class BadLayout05 {
    
        public static void main(String[] args) {
            new BadLayout05();
        }
    
        public BadLayout05() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new tada());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class tada extends JPanel {
    
            //making all the jlabels to be placed
            JLabel usernameLabel = new JLabel("username");
            JLabel passwordLabel = new JLabel("Name");
            JLabel empIDLabel = new JLabel("empID");
            JLabel SalaryLabel = new JLabel("Salary");
            JLabel EmployerLabel = new JLabel(" Salary");
            JLabel hoursLabel = new JLabel("Gender");
            JLabel departmentLabel = new JLabel("Department");
            //making all the textfields, combo boxes, and etc.
            JTextField usernameField = new JTextField(20);
            JTextField passwordField = new JTextField(20);
            JTextField empIDField = new JTextField(20);
            JTextField SalaryField = new JTextField(20);
            JTextField EmployerField = new JTextField(20);
            String[] hourss = {"Fall", "Spring", "Summer"};
            JComboBox hoursBox = new JComboBox(hourss);
            JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"),
                new JCheckBox("Department 2"),
                new JCheckBox("Department 3"),
                new JCheckBox("Department 4"),
                new JCheckBox("Department 5")};
            String[] Salary = {"section", "combo", "box"};
            JComboBox sectionBox = new JComboBox(Salary);
    //making all the panels to be placed inside the main panel.
            JPanel usernamePanel = new JPanel();
            JPanel passwordPanel = new JPanel();
            JPanel empIDPanel = new JPanel();
            JPanel SalaryPanel = new JPanel();
            JPanel EmployerPanel = new JPanel();
            JPanel hoursPanel = new JPanel();
            JPanel departmentsPanel = new JPanel();
            JPanel top = new JPanel();
            JPanel bottom = new JPanel();
            JButton submitButton = new JButton("Submit");
    
            public tada() {
    
    //this initializes the panel from superclass. nothing really important inherited from superclass. super("employer design", 10);
    
            // This is bad idea...
    //            setPreferredSize(new Dimension(400, 600));
    
    //adding all the labels, text fields, etc to the sub-panels, and adding subpanels to main pannel, bottom. setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS)); top = new JPanel(); top.setSize(getWidth(), 30); bottom = new JPanel(); bottom.setLayout(new BoxLayout(bottom,BoxLayout.Y_AXIS));
    
                top.setBackground(Color.red);
                JLabel title = new JLabel("Employer Design");
                // This is a bad idea - IHMO
                title.setFont(new Font("Helvetica", 1, 20));
                top.add(title);
                bottom.setBackground(Color.yellow);
    
                usernamePanel.add(usernameLabel);
                usernamePanel.add(usernameField);
                bottom.add(usernamePanel);
    
                passwordPanel.add(passwordLabel);
                passwordPanel.add(passwordField);
                bottom.add(passwordPanel);
    
                empIDPanel.add(empIDLabel);
                empIDPanel.add(empIDField);
                bottom.add(empIDPanel);
    
                SalaryPanel.add(SalaryLabel);
                SalaryPanel.add(SalaryField);
                bottom.add(SalaryPanel);
    
                EmployerPanel.add(EmployerLabel);
                EmployerPanel.add(EmployerField);
                bottom.add(EmployerPanel);
    
                hoursPanel.add(hoursLabel);
                hoursPanel.add(hoursBox);
                bottom.add(hoursPanel);
    
                departmentsPanel.add(departmentLabel);
                for (JCheckBox jbc : departmentCheckBoxesBoxs) {
                    departmentsPanel.add(jbc);
                }
    
                GridBagConstraints gbc = new GridBagConstraints();
                setLayout(new GridBagLayout());
                gbc.gridx = 0;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
    
                add(top, gbc);
                gbc.weightx = 0;
                gbc.fill = GridBagConstraints.NONE;
                gbc.anchor = GridBagConstraints.WEST;
                add(usernamePanel, gbc);
                add(passwordPanel, gbc);
                add(empIDPanel, gbc);
                add(SalaryPanel, gbc);
                add(EmployerPanel, gbc);
                add(hoursPanel, gbc);
                add(departmentsPanel, gbc);
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                add(bottom, gbc);
    
            }
        }
    }
    

    A better solution might be to add the primary fields to a single panel and use a GridBagLayout instead

    enter image description here

    public class BadLayout05 {
    
        public static void main(String[] args) {
            new BadLayout05();
        }
    
        public BadLayout05() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new tada());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class tada extends JPanel {
    
            //making all the jlabels to be placed
            JLabel usernameLabel = new JLabel("username");
            JLabel passwordLabel = new JLabel("Name");
            JLabel empIDLabel = new JLabel("empID");
            JLabel SalaryLabel = new JLabel("Salary");
            JLabel EmployerLabel = new JLabel(" Salary");
            JLabel hoursLabel = new JLabel("Gender");
            JLabel departmentLabel = new JLabel("Department");
            //making all the textfields, combo boxes, and etc.
            JTextField usernameField = new JTextField(20);
            JTextField passwordField = new JTextField(20);
            JTextField empIDField = new JTextField(20);
            JTextField SalaryField = new JTextField(20);
            JTextField EmployerField = new JTextField(20);
            String[] hourss = {"Fall", "Spring", "Summer"};
            JComboBox hoursBox = new JComboBox(hourss);
            JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"),
                new JCheckBox("Department 2"),
                new JCheckBox("Department 3"),
                new JCheckBox("Department 4"),
                new JCheckBox("Department 5")};
            String[] Salary = {"section", "combo", "box"};
            JComboBox sectionBox = new JComboBox(Salary);
            JPanel fields = new JPanel();
            JPanel departmentsPanel = new JPanel();
            JPanel top = new JPanel();
            JPanel bottom = new JPanel();
            JButton submitButton = new JButton("Submit");
    
            public tada() {
    
                top.setBackground(Color.red);
                JLabel title = new JLabel("Employer Design");
                title.setFont(new Font("Helvetica", 1, 20));
                top.add(title);
                bottom.setBackground(Color.yellow);
    
                fields.setLayout(new GridBagLayout());
                GridBagConstraints gbcLabels = new GridBagConstraints();
                GridBagConstraints gbcFields = new GridBagConstraints();
    
                gbcLabels.gridx = 0;
                gbcLabels.gridy = 0;
                gbcLabels.anchor = GridBagConstraints.WEST;
                gbcLabels.insets = new Insets(2, 2, 2, 2);
    
                gbcFields.gridx = 1;
                gbcFields.gridy = 0;
                gbcFields.anchor = GridBagConstraints.WEST;
                gbcFields.weightx = 1;
                gbcFields.insets = new Insets(2, 2, 2, 2);
    
                fields.add(usernameLabel, gbcLabels);
                fields.add(usernameField, gbcFields);
    
                gbcFields.gridy = ++gbcLabels.gridy;
    
                fields.add(passwordLabel, gbcLabels);
                fields.add(passwordField, gbcFields);
    
                gbcFields.gridy = ++gbcLabels.gridy;
    
                fields.add(empIDLabel, gbcLabels);
                fields.add(empIDField, gbcFields);
    
                gbcFields.gridy = ++gbcLabels.gridy;
    
                fields.add(SalaryLabel, gbcLabels);
                fields.add(SalaryField, gbcFields);
    
                gbcFields.gridy = ++gbcLabels.gridy;
    
                fields.add(EmployerLabel, gbcLabels);
                fields.add(EmployerField, gbcFields);
    
                gbcFields.gridy = ++gbcLabels.gridy;
    
                fields.add(hoursLabel, gbcLabels);
                fields.add(hoursBox, gbcFields);
    
                departmentsPanel.add(departmentLabel);
                for (JCheckBox jbc : departmentCheckBoxesBoxs) {
                    departmentsPanel.add(jbc);
                }
    
                GridBagConstraints gbc = new GridBagConstraints();
                setLayout(new GridBagLayout());
                gbc.gridx = 0;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
    
                add(top, gbc);
                gbc.weightx = 0;
                gbc.fill = GridBagConstraints.NONE;
                gbc.anchor = GridBagConstraints.WEST;
                add(fields, gbc);
    //            add(passwordPanel, gbc);
    //            add(empIDPanel, gbc);
    //            add(SalaryPanel, gbc);
    //            add(EmployerPanel, gbc);
    //            add(hoursPanel, gbc);
                add(departmentsPanel, gbc);
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                add(bottom, gbc);
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has a few text boxes, you input some values
I have an admin form that lets users create entities that require an image.
I have a create action for a form that potentially generates errors (i.e. first
I have a web form that uses if statements to create a string of
I'm have a fair complex Form that I am trying to create in Play
I have a form. In that form I create an instance of a class
I have a create action in the controller that I use for a form
i have asked to create a module that will record video messages form the
I have a form on a page that a user can use to create
My main form in my application has a datagrid view that can have 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.