java - Using TextFields in Two Methods without Declaring Universally -


hi new using jframe & action listener,

i want input more 20 fields , need store in database.

i perform storing operation in actionevent

class dialog_box     {     jtextfield name; //1st textfield     jtextfield place; //2nd textfield     jtextfield city; //3rd textfield     jtextfield country; //4th     .     .     .     jtextfield pincode; //20th textfield      dialog_box()         {         jbutton add = new jbutton(             {             public void action performed()                 {                 string name_data=name.gettext();//getting first input                 string place_data=place.gettext();                 .                 .                 .                 string pincode=pincode.gettext();//getting 20th input                 //storing these data in database                 }             });          }     } 

here textfield data needed used in 2 methods (constructor method , actionperformed method), declaring textfields universally.

but don't want declare universally. there other way declare textfields?

if need query jtextfield's state in more 1 instance method in class, needs in scope allows this.

one way solve make instance field in class. other options make instance field of class held field in class.

also other viable options include using collection of jtextfields such arraylist<jtextfield> or map<string, jtextfield>, , whether want use depend on how plan reference jtextfield of interest. regardless, collection still needs instance field if visible in multiple methods / constructors throughout class.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -