Button#setAlignment() has no effect when Text and Image are set on Mac OS
Alignment is not being honored when text and image are set on a button on Mac OS.
Standalone snippet to recreate the issue
package org.eclipse.swt.bugs3;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class ButtonAlignmentExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Button Alignment Example");
shell.setLayout(new GridLayout(1, false));
// Load an image (you should replace "imagePath" with your image file path)
//Image image = ImageDescriptor.createFromFile(Snippet.class, "error_tsk.png").createImage();
Image image = new Image(display, "C:\\Users\\02236C744\\Desktop\\error_tsk.png");
// Create a button with text and image
Button button = new Button(shell, SWT.PUSH);
button.setText("Button Text");
button.setImage(image);
button.setSize(200, 50);
// Set the alignment to CENTER
button.setAlignment(SWT.RIGHT);
//button.setAlignment(SWT.LEFT);
//button.setAlignment(SWT.CENTER);
// Set layout data to take up the whole shell
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.setSize(200, 50);
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// Dispose of the image and resources
image.dispose();
display.dispose();
}
}
I picked the image from "..eclipse.platform.ui\bundles\org.eclipse.ui\icons\full\obj16\error_tsk.png" and pasted in my src folder then setting image path as above.
On Mac, the image is always on the left and the text is aligned as expected when set to left, center or right.
Here, when SWT.RIGHT is set, both button and icon needs to be right-aligned is the expected behavior.
- Select the platform(s) on which the behavior is seen:
-
- [ ] Mac OS
On Windows, it is addressed via pr => https://github.com/eclipse-platform/eclipse.platform.swt/pull/895
On Linux, issue is still open => https://github.com/eclipse-platform/eclipse.platform.swt/issues/927
Original issue => https://github.com/eclipse-platform/eclipse.platform.swt/issues/519