mike157
mike157

Reputation: 63

jButton array in java

I have this statement:

private JButton button_array [] = {
    jButton1, jButton2, jButton3, 
    jButton4, jButton5, jButton6, 
    jButton7, jButton8, jButton9
};

This does not seem to work though and gives me an error of "illegal forward reference". How do I fix the statement?

Upvotes: 0

Views: 1605

Answers (2)

Juvanis
Juvanis

Reputation: 25950

Your error is not related with your syntax. I guess your jButton1 and other buttons are declared after this statement. Take them upper part of your array declaration. Errors will disappear hopefully.

Legal:

private JButton jButton1, jButton2;
private JButton button_array [] = {jButton1, jButton2};

This one is illegal and gives "Illegal forward reference" error.

private JButton button_array [] = {jButton1, jButton2};
private JButton jButton1, jButton2;

Upvotes: 8

Samir Mangroliya
Samir Mangroliya

Reputation: 40426

JButton[] jBtns= {new JButton("1"),new JButton("2")};

Upvotes: 1

Related Questions