Search code examples
javaswingruntimejtextfield

How to set the width of a JTextField at runtime?


Can someone please help me how to set the width of a JTextField at runtime? I want my text field to be resized on runtime. It will ask the user for the length, then the input will change the width of the text field.

if(selectedComponent instanceof javax.swing.JTextField){
    javax.swing.JTextField txtField = (javax.swing.JTextField) selectedComponent;
    //txtField.setColumns(numInput); //tried this but it doesn't work
    //txtField.setPreferredSize(new Dimension(numInput, txtField.getHeight())); //also this
    //txtField.setBounds(txtField.getX(), txtField.getY(), numInput, txtField.getHeight()); 
    //and this
    txtField.revalidate();
}

I am using null layout for this, since I'm on edit mode.


Solution

  • You simply need to use jTextFieldObject.setColumns(int columnSize). This will let you increase it's size at runtime. The reason why you couldn't do it at your end is the null Layout. That is one of the main reasons why the use of null Layout/Absolute Positioning is discouraged. Here is a small example for trying your hands on :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JTextFieldExample
    {
        private JFrame frame;
        private JPanel contentPane;
        private JTextField tfield;
        private JButton button;
        private int size = 10;
    
        private ActionListener action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                String input = JOptionPane.showInputDialog(
                                    frame, "Please Enter Columns : "
                                                    , String.valueOf(++size));
                tfield.setColumns(Integer.parseInt(input));                 
                contentPane.revalidate();
                contentPane.repaint();
            }
        };
    
        private void createAndDisplayGUI()
        {
            frame = new JFrame("JTextField Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           
    
            contentPane = new JPanel();
            contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    
            tfield = new JTextField();
            tfield.setColumns(size); 
    
            JButton  button = new JButton("INC Size");
            button.addActionListener(action);
    
            contentPane.add(tfield);
            contentPane.add(button);
    
            frame.getContentPane().add(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new JTextFieldExample().createAndDisplayGUI();
                }
            });
        }
    }
    

    For absolute positioning you need to call setSize() on the JTextField in order to attain the result, though you should always keep in mind the reason why this approach is discouraged, as given in the Java Doc's first paragraph:

    Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales. Layout managers also can be reused easily by other containers, as well as other programs.