java swing design to create a textfield

kayal picture kayal · Nov 2, 2012 · Viewed 7.1k times · Source

I want to create a jtext field with a definite size, but I would like it to expand like a scroll pane when the input grows. Here is what I have so far:

JButton b,b1;

JLabel j1,j2,j3;


JTextField t1,t2;
public Window(){

    super("frame");
    //new Thread(this).start();
    b=new JButton("Click");
    j1=new JLabel("VALUE1");
    t1=new JTextField(30);
    j2=new JLabel("VALUE2");j3=new JLabel(" ");
    t2=new JTextField(30);
    j1.setBounds(100, 50, 150, 50);
    t1.setBounds(150, 50, 200, 50);
    j2.setBounds(200, 150, 250, 150);
    t2.setBounds(250, 150, 200, 50);j3.setBounds(300, 150, 250, 150);
    b.setBorder(new LineBorder(Color.black));
    b.setBackground(Color.black);
    b.setForeground(Color.white);
    b1=new JButton("Exit");
    super.setSize(300,300);
    super.setLocation(250,150);
    super.setLayout(new FlowLayout());
    super.setVisible(true);
    add(j1);add(t1);add(j2);add(t2); add(j3);
    add(b);add(b1);
    b.addActionListener(this);
    b1.addActionListener(this);

Answer

Mohsin picture Mohsin · Nov 2, 2012

If you dont want to use JScrollPane this is something you are looking for -

I have created a sample program to auto resize the JTextField size i.e if text is added its size increases and if text is deleted its size is reduced.

I have used KeyListener's - keyTyped event to detect and update the text field size. You can add document listener too to do the same thing.

Here is my sample code -

import java.awt.Dimension; 
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame; 
import javax.swing.JTextField; 
public class AutoResizeTest 
{      
    public AutoResizeTest(){
        JFrame frame = new JFrame("Auto-Resizable TextField");         
        frame.setLayout(null);

        MyCustomTextField expandableText = new MyCustomTextField(20);
        expandableText.setBounds(10, 10, 200, 30);
        expandableText.setPreferredSize(new Dimension(200,30));
        expandableText.setMaximumSize(new Dimension(600,30));

        frame.add(expandableText);
        frame.setBounds(100,100,700,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.setVisible(true);
    }

    public static void main(String[] args) 
    {         
        AutoResizeTest test = new AutoResizeTest();
    }      

    class MyCustomTextField extends javax.swing.JTextField{

        private int originallimit;
        int previousLength;
        int length;

        public MyCustomTextField(int limit){

            previousLength=0;
            originallimit = limit;

            this.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) { 

                    JTextField textField = (JTextField) e.getSource();                 
                    length = textField.getText().length();

                    if(length >= originallimit){

                        if(length > previousLength){
                            textField.setSize(new Dimension(textField.getWidth()+10, textField.getHeight()));
                        }
                        else{
                            if(length < previousLength)
                            textField.setSize(new Dimension(textField.getWidth()-10, textField.getHeight()));
                        }
                        previousLength = length;

                    }else{
                        textField.setSize(textField.getPreferredSize());
                    }
                }
            });
        }

    }
}

In this code I have increamented/reduced the size by 10. You can put your desired value.

Hope that helps you.