Andrew V
Andrew V

Reputation: 522

JLabel positioning into JPanel

I have this code written to make a database connection and add a client:

//adding the left panel
JPanel left = new JPanel();
left.setPreferredSize(new Dimension(250, 500));
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
add(left);

//adding the right panel
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(250, 500));
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
add(right);

//adding the jlabel title to the left panel
JLabel leftTitle = new JLabel("Add a client");
leftTitle.setAlignmentX(CENTER_ALIGNMENT);
left.add(leftTitle);

//adding the jlabel title to the right panel
JLabel rightTitle = new JLabel("Make a reservation");
rightTitle.setAlignmentX(CENTER_ALIGNMENT);
right.add(rightTitle);

//adding the jlabel "name"
JLabel nameL = new JLabel("Name:");
left.add(nameL);

and I want to move this JLabel here: move that "Name:" into the left

I've tried doing nameL.setAlignmentX(LEFT_ALIGNMENT); but it's still not working

Upvotes: 0

Views: 112

Answers (1)

gobernador
gobernador

Reputation: 5739

Your problem is that you've used a BoxLayout.

left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
                   ^^^^^^^^^

Your BoxLayout is set to align things centered along the y-axis, so no amount of setting alignment is going to change that. In order to fix your problem, you need a different layout manager like GroupLayout or CardLayout.

Upvotes: 2

Related Questions