pmro
pmro

Reputation: 1

Centering labels with MigLayout

I'm trying to center labels using MigLayout and I'm having a difficult time. Here's what I'd like the labels to look like:

I'd like to:

  1. Limit the number of labels to five per row and I think I can use wrap 5.
  2. Center the labels inside the JPanels

I have labeled sections on the main JPanel with subpanels, like this:

There's no problem with centering if there's only a single label because I do this:

myPanel.add(label, "align center, wrap");

Unfortunately, if I add more than one they just don't center. I've gone through the MigLayout Cheat Sheet but it's like going through an API without fully understanding how it all works and I haven't been able to find any good tutorials out there. I'd like to become proficient with MigLayout, so a good tutorial would probably do the trick.

I'd be grateful for your help.

Upvotes: 0

Views: 1107

Answers (2)

Gaurav vijayvargiya
Gaurav vijayvargiya

Reputation: 513

you can make miglayout with center column constraint and you can add it by cell no.

JPanel panel = new JPanel(new MigLayout("","[center][center][center][center][center]","[]"));

JLabel label1 = new JLabel("label1");
JLabel label2 = new JLabel("label2");
JLabel label3 = new JLabel("label3");
JLabel label4 = new JLabel("label4");
JLabel label5 = new JLabel("label5");

panel.add(label1, "cell 0 0");
panel.add(label2, "cell 1 0");
panel.add(label3, "cell 2 0");
panel.add(label4, "cell 3 0");
panel.add(label5, "cell 4 0");
f.getContentPane().add(panel);

Upvotes: 1

ndfkj asdkfh
ndfkj asdkfh

Reputation: 316

I am not exactly sure what you want but this is my guess:

panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5, "wrap");
panel.add(label6, "al 100%");
panel.add(label7, "align center");
panel.add(label8);

frame.pack();

Upvotes: 1

Related Questions