1

Hi i am new to Using JFrame & Action Listener,

I want to get input from more than 20 fields and then I need to store it in a database.

I Perform those storing operation in an 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 as my TextField data needed to be used in two methods (Constructor method and ActionPerformed method), I am declaring those Textfields universally.

But i don't want to declare it universally. Is there any other way to declare those TextFields?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Yuvi
  • 41
  • 1
  • 9
  • Perhaps you want to explain what is driving your request, why you don't want to declare fields in your class. – Hovercraft Full Of Eels Jan 16 '16 at 20:42
  • @HovercraftFullOfEels i wanted to declare it in my class ,but i dont wanted to declare it universal due to memory constraints,i wanted it to be declared between those two methods alone.Is there any other way than List or Map – Yuvi Jan 16 '16 at 20:46
  • That makes no sense since 20 fields will have minimal "memory" constraints. And what do you mean by "I want it declared in my class" vs. "declare it universal"? Universal has no meaning in this context. – Hovercraft Full Of Eels Jan 16 '16 at 20:51
  • @HovercraftFullOfEels Here i am referring 20 fields as an example ,i wanted to create a Project with many text fields and as its gonna be used in production environment with large data ,so thats why i wanted it to be more optimised – Yuvi Jan 16 '16 at 21:08
  • @HovercraftFullOfEels here universal refers to global variable – Yuvi Jan 16 '16 at 21:09
  • There are no global variables in Java, and you appear to be doing premature optimization at this point. I would first create your classes using decent OOP principles. If your code uses good OOP practices, your objects will be GC'd when no longer needed and referred to. If you're running into memory problems, then profile your program and fix it. – Hovercraft Full Of Eels Jan 16 '16 at 21:21

1 Answers1

2

If you need to query the JTextField's state in more than one instance method in this class, then it needs to be in a scope that allows this.

One way to solve this is to make it an instance field in the class. Other options are to make it an instance field of another class that is held as a field in this class.

Also other viable options include using a collection of the JTextFields such as an ArrayList<JTextField> or a Map<String, JTextField>, and whether you want to use this will depend on how you plan to get a reference to the JTextField of interest. But regardless, this collection still needs to be an instance field if it is to be visible in multiple methods / constructors throughout the class.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373