Wednesday, May 21, 2014

how to change the width of a textfield dynamically in java?

JTextField textField = new JTextField("Default input", 10);

 After changing the size of a text box with the setColumns method, you need to call the validate method of the surrounding container. The validate method recomputes the size and layout of all components in a container. After you use the validate method, the layout manager repaints the container, and the changed size of the text field will be visible.

In general, you want to let the user add text (or edit the existing text) in a text field. Quite often these text fields start out blank. To make a blank text field, just leave out the string as a parameter for the JTextField constructor:

JTextField textField = new JTextField(20);

You can change the contents of the text field at any time by using the setText method from the TextComponent parent class. For example:


You can find out what the user typed by calling the getText method. This method returns the exact text that the user typed. To trim any extraneous leading and trailing spaces from the data in a text field, apply the trim method to the return value of getText:

String text = textField.getText().trim();

To change the font in which the user text appears, use the setFont method.

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class Jtextfld extends JFrame {

            private JPanel contentPane;

            private JTextField textField;


             * Launch the application.


            public static void main(String[] args) {

                        EventQueue.invokeLater(new Runnable() {

                                    public void run() {

                                                try {

                                                            Jtextfld frame = new Jtextfld();


                                                } catch (Exception e) {







             * Create the frame.


            public Jtextfld() {



                        setTitle("TextFiled ");


                        setBounds(100, 100, 425, 256);

                        contentPane = new JPanel();

                        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));




                        JLabel lblNewLabel = new JLabel("            Change TextFiled width");

                        lblNewLabel.setBounds(94, 11, 197, 38);



                        textField = new JTextField(); //new JTextField(10); or textField.setColumns(10); or textField.setSize(int width,int height);


                        textField.setBounds(144, 86, 86, 20);




                        JButton btnNewButton = new JButton("Original width");

                        btnNewButton.addActionListener(new ActionListener() {

                                    public void actionPerformed(ActionEvent arg0) {

                                                textField.setSize(86, 20);



                        btnNewButton.setBounds(70, 171, 117, 23);



                        JButton btnChangeWidth = new JButton("Change width");

                        btnChangeWidth.addActionListener(new ActionListener() {

                                    public void actionPerformed(ActionEvent arg0) {

                                                textField.setSize(140, 20);




                        btnChangeWidth.setBounds(246, 171, 117, 23);





No comments:

Post a Comment