0

When I try to initialize an array of javax.swing.JTextField objects in the constructor, I get a warning about an illegal forward reference. I understand that the automatically created JTextField variables don't exist yet. But how can I still have a global array of these objects available in my program?

public class HRDatabase extends javax.swing.JFrame { 
    private javax.swing.JTextField[] fields; 

    public HRDatabase() { 
        fields = {firstNameField, lastNameField, emailField}; 
    }

The variables firstNameField, lastNameField, emailField are basically Swing GUI textfields that are declared automatically by NetBeans.

TeWu
  • 5,928
  • 2
  • 22
  • 36
Jing Ma
  • 183
  • 2
  • 12
  • Can you defer creating the array until the first time it is accessed? Your accessor (`getFields()` or whatever) can hold the logic you're currently putting in your constructor. – Paul Hicks May 19 '16 at 22:55
  • 3
    Show the code that is giving the error. And what do you mean by "initialize an array of JTextField objects"? What are you trying to initialize them to? – FredK May 19 '16 at 23:01
  • For [example](http://stackoverflow.com/a/8703807/230513). – trashgod May 20 '16 at 01:15
  • @Jing Ma What is the purpose of this array. You can get the JTextField components after it got added to the Panel with `getComponents()` though you may have to check `instanceof` on the component to check if it is JTextField. – Beniton Fernando May 20 '16 at 07:22
  • @trashgod. My JTextField variables are automatically created by NetBeans, so I don't think I can initialize them myself in the constructor. I'll try to call the variables through a function. ;) – Jing Ma May 20 '16 at 14:30
  • @JingMa: Don't let the GUI editor dictate your design; limit its scope, for [example](http://stackoverflow.com/a/2561540/230513). – trashgod May 20 '16 at 14:41

0 Answers0