0

I've been looking all over the net for a code that is something like this:

JTextField[] jt = new JTextField{jTextField1,jTextField2,..};

I had a copy of this code once and I kept it in a hard disk because I know I might use it in the future, but the disk died!

can anyone help me find this code? It gives me error so please do correct this for me. thanks!

by the way, this is an array for an existing jTextField and it runs inside a button when pressed.

EDIT: since this was flagged as possible duplicate, heres my explanation.

The classic initialization has already been tested and what ive seen yet so far in declaring jTextField array is this:

JTextField[] jt = new jTextField[10];

specifying the value then adding it.

jTextField[n] = new JTextField(jTextField1);

if i use that method, I would have to type it all over again.

jTextField[n] = new JTextField(jTextField2);
jTextField[n] = new JTextField(jTextField3);.. and so on and so forth.

now what I am looking for is what i've said on the sample code. I have used this once but I was clumsy enough not to back it up.

Bumpy
  • 97
  • 11
  • *It gives me error* What is the error? – Manu Jan 06 '16 at 15:52
  • 2
    Possible duplicate of [How to initialize an array in Java?](http://stackoverflow.com/questions/1938101/how-to-initialize-an-array-in-java) – Manu Jan 06 '16 at 15:53
  • JTextField[] jt = new JTextField[]{jTextField1,jTextField2}; it says illegal forward reference. netbeans – Bumpy Jan 06 '16 at 15:54
  • @manu well its sort of. but im using this array for controls such as jTextFields. I tried using the classic initialization of array data but nope. no good. – Bumpy Jan 06 '16 at 15:57
  • Can you show your complete code? I tried simulating the situation and I don't get any errors. – Atri Jan 06 '16 at 16:19

3 Answers3

1

This is wrong syntax and will throw a compilation error:

JTextField[] jt = new JTextField{jTextField1,jTextField2};

You need to do:

JTextField[] jt = new JTextField[] {jTextField1,jTextField2}; // or you can do {jTextField1,jTextField2};

I tried this and it works fine:

JTextField j1 = new JTextField();
JTextField j2 = new JTextField();
JTextField[] j = new JTextField[] {j1, j2}; 
//JTextField[] j = {j1, j2};   // This syntax also works 
System.out.println(j);
Atri
  • 5,511
  • 5
  • 30
  • 40
  • so like ive said to @pedro My application is running and it has 2 jTextFields. What im trying to do is put those two jTextFields inside an array with a press of a button. and I clearly remember doing something like this Jt[i].getText(); – Bumpy Jan 06 '16 at 16:27
  • @Bumpy, Are you trying to get the content of the `TextFields` or the `TextFields` themselves? – Jonah Jan 06 '16 at 16:30
  • thanks for the help! JTextField[] jt = new JTextField[] {jTextField1,jTextField2}; works now. its just net beans giving me a hard time again. – Bumpy Jan 06 '16 at 16:34
  • @Bumpy if my answer solved your problem, you can click the big checkbox to accept it as the answer. – Atri Jan 06 '16 at 16:38
0

I think it's not possible to do what you want. Try to code some cycle to create and add JTextFields to you array.

int x = 2;
JTextField[] textFields = new JTextField[x];

for(int i = 0;i < x; i++) {
    JTextField textField = new JTextField(blabla);
    textField.setSomething();
    textFields [i] = textField;
}
  • I wish it was the right one but no, I only use that one when creating dynamic jTextFields. So lets put it this way. My application is running and it has 2 jTextFields. What im trying to do is put those two jTextFields inside an array with a press of a button. and I clearly remember doing something like this Jt[i].getText(); – Bumpy Jan 06 '16 at 16:25
  • The 2 JTextFields are in the same class? JtextField text1; JtextField text2; void clickfunctiion() { getText1() getText2() } – Pedro Martins PT Jan 06 '16 at 16:47
0

Silly netbeans..

I closed my project down and re-opened it just to try it out. well guess what. the code works properly now.

public void arrayoftextboxes(){
     JTextField[] jt = {jTextField1, jTextField2};
}
Bumpy
  • 97
  • 11
  • 1
    Because now you changed the code from `JTextField[] jt = new JTextField{jTextField1,jTextField2,..};` to `JTextField[] jt = {jTextField1, jTextField2};`. This is what I had answered earlier. – Atri Jan 06 '16 at 16:32
  • seriously i was trying alot of codes especially when the post was flagged as possible duplicate. I followed this one too: `Datatype[] variable = new Datatype[] { value1,value2.... }` and ` Datatype variable[] = new Datatype[] { value1,value2.... }` and it didnt work. the code was crystal clear thats why I was really bothered how in the world it didnt work. – Bumpy Jan 06 '16 at 16:36