java - Store Jtextfield input in Arraylist then print contents of Arraylist -


this first time creating gui. can create text fields fine, having trouble collecting user input , storing input arraylist.

import java.awt.flowlayout; import java.awt.event.actionlistener; import javax.swing.jtextfield;  textfield1 = new jtextfield("enter resident name",20);    add(textfield1);     textfield2 = new jtextfield("how many accounts enter data for?",20);   add(textfield2);     textfield3 = new jtextfield("enter account #",20);   add(textfield4);     textfield3 = new jtextfield("enter data account",20);   add(textfield5);  
  1. if user enters "4" textfield2, "enter account #" , "enter data account" appear 4 times (resulting in 8 new textfields)

  2. i user able click "store" button places textfields 1-5 (and more if want enter lot of accounts) arraylist can later retrieved , print all stored array info (show resident , accounts info), similar below:

    arraylist accounts = new arraylist<>();

    account exampleaccount = new account(); exampleaccount.setfirstname("john"); exampleaccount.setlastname("doe"); exampleaccount.setbalance(101.13d);

    accounts.add(exampleaccount);

    for(account account : accounts) { system.out.println("name: " + account.getfirstname() + " " + account.getlastname() + ". balance: " + account.getbalance()); // or override account's tostring() method , system.out.println(account.tostring()) }

  3. would form "reset" after each time user clicks "store" can enter new resident's information.

how go doing this?

you need this..

    textfield1 = new jtextfield("enter resident name", 20);     add(textfield1);      textfield2 = new jtextfield("how many accounts enter data for?", 20);     add(textfield2);      textfield2.addactionlistener(new actionlistener() {          @override         public void actionperformed(actionevent e) {              int count = integer.valueof(textfield2.gettext());              (int = 1; <= count; i++) {                 textfield3 = new jtextfield("enter account #", 20);                 add(textfield4);                  textfield3 = new jtextfield("enter data account", 20);                 add(textfield5);             }          }     }); 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -