• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Already Initialized JPanels to an array causing a whole button of errors?  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a chess program in java swing, using the netbeans GUI creator. I visually added 64 jPanels back to back, so they are initialized in generated code in the following manner:

jPanel1 = new javax.swing.JPanel();

I want to add each of these panels to a multidimensional array of jPanels so that I can access them programatically in the code later on. However, when I try to to create such an array and add them to it with an array initializer, suddenly the whole program is filled with errors. I put in the following line, and suddenly every reference to any jPanel in the entire program (including the generated code) comes up with the error "cannot find symbol: variable jPanel(whatever number)", not to mention the fact that seemingly random lines of code will have the error "class interface or enumeration expected". What did I do wrong? How do I fix it? Help would be much appreciated.

JPanel[][] chessBoard = {{jPanel1,jPanel2,jPanel3,jPanel4,jPanel5,jPanel6,jPanel7,jPanel8}{jPanel9,jPanel10,jPanel11,jPanel12,jPanel13,jPanel14,jPanel15,jPanel16}{jPanel17,jPanel18,jPanel19,jPanel20,jPanel21,jPanel22,jPanel23,jPanel24}{jPanel25,jPanel26,jPanel27,jPanel28,jPanel29,jPanel30,jPanel31,jPanel32}{jPanel33,jPanel34,jPanel35,jPanel36,jPanel37,jPanel38,jPanel39,jPanel40}{jPanel41,jPanel42,jPanel43,jPanel44,jPanel45,jPanel46,jPanel47,jPanel48}{jPanel49,jPanel50,jPanel51,jPanel52,jPanel53,jPanel54,jPanel55,jPanel56}{jPanel57,jPanel58,jPanel59,jPanel60,jPanel61,jPanel62,jPanel63,jPanel64}};
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:I am creating a chess program in java swing, using the netbeans GUI creator.
I visually added 64 jPanels back to back, so they are initialized in generated code in the following manner:

Ooof.

I want to add each of these panels to a multidimensional array of jPanels so that I can access them programatically in the code later on....

I'll preface this by saying that I'm no GUI expert; but it seems to me like you're attacking this problem from the wrong end - even if you only want a chess board that can register pieces and moves without any 'expertise'.

Get your Board (or your chess game) working first, and then visualise it. And if you've already tackled the first step, but can't get the GUI bit to work, I suspect you're still putting far too much "smarts" into the GUI.

And why 64 separate named Panels? Sounds like the School of Masochist Programming to me. You plainly know that arrays exist, so why not just set up an array of them? (if, indeed, they're the right class to be using; I reckon a BoxLayout might be what you want)

I'm quite sure that people far better than me will be able to advise you on the GUI side of things, but I can tell you this:
Your chess board, and any 'smarts' it has, should be completely separate from your GUI.

All these Panels and Layouts and 'J' classes are DISPLAY (and possibly input). They have nothing whatsoever to do with the game of chess or the chess board itself.

HIH

Winston
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm not sure I fully understand you. I understand that having an array of these JPanels is important, but since I'm using the GUI creator, I can't modify their original declaration, which by default declares them as 64 separately named JPanels. I'm merely attempting to do damage control by adding them to an array after declaration, but I can't figure out how to do this without causing a bunch of errors. Do you know how to add them to an array after the fact? I think it may be possible since that is what they advise to do on the netbeans wiki:

http://wiki.netbeans.org/FaqGuiControlArray
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, someone else gave me the following advice on the problem:

"I'ts important to remember that an array holds references, not objects.
All you need to do is to assign the value of the concerned variable to a slot in the array. "

Could anyone give me an example of how to do this in code? I already have 64 declared JPanels, like this:

JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
etc,

and I want to somehow "add their values" to slots in an array. I don't know exactly how to do this though. Does anyone know how? I already tried an array initializer:

JPanel chessBoard[][] = {{jPanel1,jPanel2,jPanel3...}{jPanel9,jPanel10,...}{}{}{}...}

and adding them one by one, like this:

JPanel chessBoard[][] = new JPanel[8][8]
chessBoard[0][0] = jPanel1;
chessBoard[0][1] = jPanel2;

but both return errors like this: "cannot find symbol: variable jPanel1, jPanel2, etc"
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:and I want to somehow "add their values" to slots in an array. I don't know exactly how to do this though.

What about:I suspect you're overthinking things at the moment.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!