To right justified a JTextField
contents we can call the setHorizontalAlignment(JTextField.RIGHT)
method of the JTextField
class.
package org.kodejava.swing;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
public class TextFieldRightJustify extends JFrame {
public TextFieldRightJustify() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TextFieldRightJustify().setVisible(true));
}
private void initComponents() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500, 500);
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEFT));
JTextField textField = new JTextField(15);
textField.setPreferredSize(new Dimension(100, 20));
// Right justify the JTextField contents
textField.setHorizontalAlignment(JTextField.RIGHT);
container.add(textField);
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024