0

Is there any simple way to center a button on a row with a loading indicator on the left side of the button without moving the button itself?

I want the button to be always be centered and the loading indicatior (JLabel) should be right on the side of the button.

This solution seems way too complicated and doesn't actually work for what I want to do.

What I have so far is this:

setLayout(new MigLayout("align center center"));

add(_loadingIndicator, "center, split 2");
add(_applyButton, "center");

but that center the 2 components together so the button is never really centered.

dguay
  • 1,447
  • 13
  • 23

1 Answers1

0

A way to have your button exactly at the center, is to have your layout have 3 columns. You will place the button in the center one, and the loading indicator in the right one. Suppose the loading indicator is 32x32 pixels.

setLayout(new MigLayout("debug", "[grow]32px[]0[grow]")); // The [][][]s are columns, meaning 3 columns. The numbers between them are the insets.

add(_applyButton, "cell 1 0"); // Place the button in 2nd column, 1st row.
add(_loadingIndicator, "cell 2 0"); // Place the indicator in 3rd column, 1st row.

Note: If you don't specify the insets, the button will be approx. centered, but not exactly in the center. The debug parameter helps you see the cells' size. You can simply omit it.

Bill Tsagkas
  • 460
  • 1
  • 4
  • 14