0

How can I automatically put other TextFields on a panel when I get a number of them in another TextField?! I try to use repaint(), but I have not understood.

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

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

public class Example extends JFrame {

    private Container container;
    private JPanel panel;
    private JTextField number; //get a number of TextFields
    private JTextField text;  // this to put in gui automatically 

    public Example() {
        super("Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200, 400);
        setLocationRelativeTo(null);
        openView();
    }

    public void openView() {
        container = this.getContentPane();

        panel = new JPanel();
        panel.setSize(new Dimension(200, 400));
        panel.setLayout(null);

        number = new JTextField();
        number.setBounds( 50, 10, 100, 40);
        number.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
                // put others TextFields on gui
                int y= 50;
                int count = Integer.parseInt(number.getText());
                for(int i=0; i<count; i++) {
                    text = new JTextField();
                    text.setBounds(50, y, 100, 40);
                    panel.add(text);
                    y=y+50;
                }

            }

            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

        });

        panel.add(number);
        container.add(panel);

    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Example example = new Example();
        example.setVisible(true);
    }

}

As you see I am trying to put other TetxFields on the panel, but when I put a some number on Textfield I have an exception, like
java.lang.NumberFormatException: For input string: "".

But I actually have changed String to int.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
toDay
  • 1
  • 2
  • @toDay You added an onKeyListener. This means as soon as you hit a number or any other key on your keyboard your code will be accessed. So try `Character.getNumericValue(e.getKeyChar());` instead of `Integer.parseInt(number.getText());` Also call `panel.updateUI();`after the for loop to make the added views visible. – Vall0n Jan 08 '19 at 13:29
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Rather than using a `KeyListener`, a `DocumentListener` would be more appropriate for a `JTextComponent`. 3) But consider instead using a `JFormattedTextField` or a `JSpinner` with a `SpinnerNumberModel`. – Andrew Thompson Jan 08 '19 at 14:26
  • 1
    'keyPressed' doesn't seem the proper event for your use case, this event listener will execute on each key press, typing '12' will result in only 3 text fields, one for the first key press and two for the second key press, I will suggest that you add a button, where you enter the number of text fields then click the button. in 'on click' event listener of the button you do input validation (it should be a number) and add the textFields to GUI – Elgayed Jan 08 '19 at 15:17
  • thank you all for help! – toDay Jan 10 '19 at 14:44

0 Answers0