Recommended Free Tools
Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
To move a JButton’s label to the left, call button.setHorizontalAlignment(SwingConstants.LEFT). The setting positions the button’s content within its bounds; it does not make the parent layout give the button more width.
Use the button’s horizontal-alignment property
There is no standard Swing component named JText in this context; this answer assumes you mean the text displayed by a JButton. JButton inherits setHorizontalAlignment(int) from AbstractButton. Its documented default horizontal alignment is SwingConstants.CENTER. The Java SE 26 API lists LEFT, CENTER, RIGHT, LEADING and TRAILING as valid values: AbstractButton API.
import javax.swing.JButton;
import javax.swing.SwingConstants;
JButton button = new JButton("Open");
button.setHorizontalAlignment(SwingConstants.LEFT);
SwingConstants.LEFT is explicit and readable. JButton.LEFT is also available through the inherited constants, but using SwingConstants makes the setting’s meaning clear.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteRunnable example with visible left alignment
This example puts the button in a BorderLayout center region, where it receives the available width. That extra space makes the alignment apparent.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class LeftAlignedButton {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JButton button = new JButton("Open document");
button.setHorizontalAlignment(SwingConstants.LEFT);
JPanel panel = new JPanel(new BorderLayout());
panel.add(button, BorderLayout.CENTER);
JFrame frame = new JFrame("Left-aligned JButton text");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(300, 120);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
Why the text may still look centered
Horizontal alignment only affects where the content sits inside the button’s allocated bounds. If the button is only as wide as its label, there is little empty space in which to distinguish left alignment from centered alignment. The parent layout controls the button’s size; setHorizontalAlignment does not enlarge it.
- Check whether the parent layout gives the button extra horizontal space.
- Confirm the alignment method is called on the button you actually display.
- Make sure you have not changed text position relative to an icon instead of overall alignment.
- Account for the button margin and the look and feel’s border or insets.
Prefer configuring the parent layout to allocate the intended width rather than setting a hard-coded preferred size solely to expose the alignment.
Choose LEFT or LEADING
Use LEFT when the requirement is the physical left edge. Use LEADING when the control should follow the interface’s reading direction: leading is left in a left-to-right orientation and right in a right-to-left orientation.
Rank #2
button.setHorizontalAlignment(SwingConstants.LEADING);
This distinction matters for internationalized interfaces. LEFT and LEADING are separate documented alignment choices, so select according to the desired behavior rather than treating them as interchangeable.
Separate content alignment from icon and text positioning
setHorizontalAlignment positions the button’s icon-and-text content within the button. setHorizontalTextPosition instead determines where text appears relative to an icon; it is not the fix for a text-only button that should align within its bounds.
JButton button = new JButton("Save", saveIcon);
button.setHorizontalAlignment(SwingConstants.LEFT); // position the group in the button
button.setHorizontalTextPosition(SwingConstants.RIGHT); // text to the icon's right
button.setIconTextGap(8); // gap between icon and text
The API documents these as separate properties, along with setIconTextGap(int) for spacing when both icon and text are present: AbstractButton API.
Adjust space between the border and content
Use the button margin to control space between its border and label or content. For example, to add left inset while keeping the other margins at zero:
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →import java.awt.Insets;
button.setMargin(new Insets(0, 12, 0, 0));
For padding on all sides, use values for each edge, such as new Insets(4, 12, 4, 12). Alignment determines placement; the margin sets the border-to-content spacing. Look-and-feel painting can also affect the visible border and insets.
Avoid adding leading spaces to the label, such as new JButton(" Open"). Their width depends on the font and they are unreliable across fonts, look and feels, accessibility settings and translations.
Rank #4
Make the layout allocate enough width
The alignment setting works inside the width supplied by the parent. These common layouts can give a button a wider region or cell:
BorderLayout
JPanel panel = new JPanel(new BorderLayout());
panel.add(button, BorderLayout.CENTER);
The center component expands into available space in the panel.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →GridLayout
JPanel panel = new JPanel(new GridLayout(1, 1));
panel.add(button);
The button fills its grid cell, so its internal alignment can be seen when the cell is wider than the label.
Best Value
GridBagLayout
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
panel.add(button, gbc);
Horizontal fill and a positive horizontal weight let the button use available width. Changing the button’s own layout manager is not the normal way to align its label; Swing’s button UI delegate renders the icon and text.
Check the value or troubleshoot a custom look and feel
To confirm which alignment value is set, inspect it directly:
System.out.println(button.getHorizontalAlignment());
For a physical left alignment, the returned value should equal SwingConstants.LEFT. The corresponding text-position getter, getHorizontalTextPosition(), reports a different property: text placement relative to an icon.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteStandard Swing look and feels generally honor the documented alignment properties, though borders and painting can vary. If the setting appears ineffective after confirming the button has extra width and the correct property is set, test with a standard look and feel and inspect any custom ButtonUI or renderer code. The API documents the alignment methods and behavior in AbstractButton; an older Java SE 8 reference is also available at Java SE 8 AbstractButton API.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

