0

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.

kikkaz69
  • 13
  • 6
  • 3
    What part isn't working? What is the result you are getting? How does it differ from what you want? We need more visibility, friend! – KyleKW Dec 07 '16 at 17:38
  • This code is showing just one textfield with one button which I created above in panel1 in GUIDesign() method. I wanted to create as many textfields given at input. but This code doesn't works. – kikkaz69 Dec 07 '16 at 17:41
  • 1
    1) `public void TFArray` method names should be lowerCamelCase, 2) `extends JFrame` don't extend `JFrame` instead if you need to extend something, extend from `JPanel` and create an instance of `JFrame`. 3) `JButton bsmbt= new JButton("OK");` You're aware that your global variable `JButton bsmbt;` isn't used because of that line right?. 4) What part exactly doesn't work? – Frakcool Dec 07 '16 at 17:41
  • Method TFArray() isn't working. – kikkaz69 Dec 07 '16 at 17:43
  • 1
    You need to call [`revalidate()`](https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#revalidate()) and [`repaint()`](https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#repaint(java.awt.Rectangle)), [See this Q&A](http://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint) but please use a [Layout manager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – Frakcool Dec 07 '16 at 17:48
  • Thanks Bro, revalidate() did the trick, but it appears to be refreshing the GUI for one time only. Do you know why? Suppose I run the project, and enter 2 in textfield and click on button. The above code creates 2 textfields. but when I again put some value and submit it, it doesn't seem to reflect any changes. – kikkaz69 Dec 07 '16 at 17:55

1 Answers1

3

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

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89