Search code examples
javaswingjframejtextfield

Add Textfields on Jframe at Runtime


I am trying to create text-fields on frame by getting input at run-time. Is it possible? Or I have to create another frame for that. I tried this code, but it's not working. Please Help me out, and tell me what's wrong with this code.

import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Check extends JFrame implements ActionListener
{
JTextField txtqty;
JTextField[] tfArr;
JPanel p1,p2;
JButton bsmbt;

public Check()
{
    GUIDesign();
}

public void GUIDesign()
{
    p1 = new JPanel();
    txtqty = new JTextField(10);
    JButton bsmbt= new JButton("OK");
    p1.add(txtqty);
    p1.add(bsmbt);

    p2=new JPanel();
    p2.setLayout(null);
    add(p1,BorderLayout.NORTH);
    setSize(500, 500);
    setVisible(true);
    setLocation(100, 100);

    bsmbt.addActionListener(this);




}

public static void main(String[] args)
{
    new Check();
}

public void TFArray(JTextField[] temp)
{
    int x,y,width,height;
    x=10;y=30;width=50;height=20;
    int no_of_textboxes = Integer.parseInt(txtqty.getText());
    temp=new JTextField[no_of_textboxes];

    for(int i=0;i<no_of_textboxes;i++)
    {
        temp[i]= new JTextField(10);
        temp[i].setBounds(x, y, width, height);
        x+=(width+10);
        p2.add(temp[i]);

    }
    add(p2);

}

@Override
public void actionPerformed(ActionEvent ae) {

        JOptionPane.showMessageDialog(this, txtqty.getText());
        TFArray(tfArr);
}


}

->Method TFArray() isn't working.


Solution

  • You have many errors in your code:

    1. public void TFArray(JTextField[] temp): method names should start with lowerCamelCase

    2. You're extending JFrame, you shouldn't extend JFrame, because when you extend it your class is a JFrame, JFrame is rigid so you can't place it inside anything else, instead you might consider creating a JFrame instance and if you ever need to extend JComponent extend from JPanel.

    3. JButton bsmbt= new JButton("OK"); the variable bsmbt is a local variable inside your constructor, your global variable bsmbt is not used anywhere, and if you try to use it later you'll get a NullPointerException, instead change that line to:

      bsmbt= new JButton("OK");
      
    4. You're using null layout for p2, instead use a proper Layout manager and read Null layout is evil and Why is it frowned upon to use a null layout in swing?. Swing was designed to work with different PLAFs, screen sizes and resolutions, while pixel perfect GUIs (with setBounds()) might seem like the best and faster way to create a complex GUI in Swing, the more GUIs you make, the more errors you'll get due to this.

    To solve your problem call revalidate() and repaint()

    The above code creates 2 textfields. but when I again put some value and submit it, it doesn't seem to reflect any changes.

    That might be because you're overriding x, y, height and width variables each time you enter TFArray method. But that is a guess, if you want a real answer, follow the suggestions above and post a proper and valid Minimal, Complete, and Verifiable example