简体   繁体   中英

java swing design to create a textfield

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);

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 ie 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.

The default size of the JTextField will be set and provide a ScrollPane to JTextField which will make field to expand if necessary.

Try the following

JTextField jtext = new JTextField(pane);
JScrollPane scroll = new JScrollPane(jtext);

Place the TextArea in a seperate JPanel with BorderLayout and in the center and the panel automatically expands, when the content increases.

textArea = new JTextArea(2, WIDTH);
textArea.setLineWrap(true);
textArea.getDocument().putProperty("filterNewlines", Boolean.TRUE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM