How do I right justified JTextField contents?
Category: javax.swing, viewed: 1861 time(s).
To right justified a JTextField contents we can call the setHorizontalAlignment(JTextField.RIGHT) method of the JTextField class.
package org.kodejava.example.swing; import javax.swing.*; import java.awt.*; public class TextFieldRightJustify extends JFrame { public TextFieldRightJustify() { initComponents(); } private void initComponents() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200, 200); 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); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TextFieldRightJustify().setVisible(true); } }); } } |
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I format JLabel using HTML?
- How do I display an image in JButton?
- How do I use JFormattedTextField to format user input?
- How do I handle mouse button click event?
- How do I handle mouse wheel event?
- How do I handle mouse motion event?
- How do I handle mouse event in swing?
- How do I get the system look and feel?
- How do I set the look and feel for swing application?
- How do I get the available swing look and feel?