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.
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
A better solution might be to add the primary fields to a single panel and use a
GridBagLayoutinstead