How can I make a sequence with entered values by button in Java?

I want write program who in t1(TextField) enter how many numbers user want enter and confirm by b1(Button). In t2(TextField) user give a first value and enter it by b2(Button), in next time user give a second value and again enter it by b2(Button). It happens so much times as high is n (from first listener).

How must I change code that program to do it?

Now when i give to t2 first value and hit the button then b2 is still pressed and user can't do anything.

final AtomicInteger n = new AtomicInteger();
ActionListener lis5 = new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        String a = t1.getText();
        n.set(Integer.parseInt(a));
    } 
};
b1.addActionListener(lis5); 

ActionListener lis6 = new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        //String b = t2.getText();     ??
        //n.set(Integer.parseInt(b));  ??

        int nn = n.get();
        int [] tab = new int[nn];
        for(int i=0;i<nn;i++){
            tab[i] = in.nextInt();
        }
    } 
};
b2.addActionListener(lis6);

Answers


First of all, get rid of the for loop in the ActionListener. For loops work for linear console programs where you enter data using a Scanner but not for event-driven GUI's. Here instead of the for loop use a counter int variable that you increment within the ActionListener and that you use to help you decide where to place the data. Also get the int array out of the ActionListener and into the class so that it can be available to the rest of the class. In pseudocode:

in the class
    int array declared here BUT not initialized here.

    first Actionlistener
        get total count value
        initialize array using this value
    end first action listener

    second ActionListener
        int counter variable set to 0.
        action performed method
            if counter < total count
                get value from text field
                convert it to an int
                place value into intArray[counter]
                increment counter
            end if
        end action performed
    end second ActionLisener    

Need Your Help

Java Annotations Processor: Check if TypeMirror implements specific interface

java reflection annotations annotation-processing

I'm working with on a Java annotations processor. My annotation, @foo is used to mark field variables that can be read to a file or from a file during runtime. However, I would like to check if the

Struts 2 XML validation doesn`t work

java spring jsp validation struts2

Hello everybody I'm trying use validation on my form and I did all things in the samples and googled many times to find solution but unfortunately I couldn't solve my problem.