Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

alternative to making a static JList?  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am currently writing a chess application in Java swing. For the GUI part, I have one MainWindow class, and one ChessBoard class. The MainWindow has a main panel, in which is contained 1) a panel containing the chessboard from the chessboard class (a grid layout array of 64 JLabels), 2) a JLabel status bar, and 3) a JList to hold the moves of the game. I have a movePiece method in the chessboard class, which I would like to add the move and question to the moves list, which is contained in the main window class. However, since JList is some special type, I can't make it static ( that gives me an error ). I also tried creating a method in the main window class to add a move to the list, which I would then access via the chessboard class. However, I can't make the method static, because it is referencing a non-static variable ( the JList ), and since the Main window class already has an object of the chessboard class, trying to create a main window object in the chessboard class creates a stack overflow error.

Does anyone know a way around this dilemma?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:I can't make it static ( that gives me an error ). I also tried creating a method in the main window class to add a move to the list, which I would then access via the chessboard class. However, I can't make the method static, because it is referencing a non-static variable ( the JList ), and since the Main window class already has an object of the chessboard class, trying to create a main window object in the chessboard class creates a stack overflow error.
Does anyone know a way around this dilemma?

Yes. Create a ChessGame object in your main() method and run its play() method.

However, I worry very much that you may be trying too much too soon. Chess is NOT a simple game, and even if you're only programming a "referee" for a chess game, I'd say that if you're worrying about static and instance methods, and how they work, then you're not quite ready for a chess game.

That's not to say you won't be. Just not YET.

Winston
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, although I think I'm still going to pursue it, or at least for a while. Perhaps in the end I'll conclude that you were right I should start with something simpler, but I'd like to at least try first. That being said, I don't quite understand your proposed solution. I have three relevant classes, the first of which, main, currently just creates an object from the next class, MainWindow, which serves as the main (and currently only) window for the app. The MainWindow class then creates the main GUI interface, using an object of the ChessBoard class for the board part. So I don't understand what class the ChessGame object you propose would be from (and if a new class, I don't understand exactly what this class would contain). Similarly, I don't understand generally what the plan method would do. And finally, I don't understand how the two together will bypass my specific static JList problem.

Thanks for your help!
 
Paul Clapham
Sheriff
Posts: 22493
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any code, so I'm just going to guess. But it's apparent that you're putting code in a static method and that's causing you problems.

You should have only one static method, the public static void main(String[]) method which Java runs when it starts. And it should do what Winston suggests, create a ChessBoard object, and only that. It shouldn't be creating the GUI, that should be part of the ChessBoard's constructor.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to make JList static? Is it because you want to pass something to main(String[] args)? If so, get everything you can out of main(). See Main is a Pain.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't post code because it would have to be a lot of different snippets from a lot of different classes. I think the problem is the general design of my program, which is why I described it instead of posting code. The central problem I'm encountering is that I'd like to have a movePiece method in my chessboard class, and I would like the method, in addition to actually moving the piece, to also add an item to my moves list, which is a JList. But the JList is part of the main window class, and I am having trouble accessing it from another class. I can't use an object of the main window class, because the main window class already has an object of the chessboard class, so this creates an infinite loop and a stack overflow error. But I also can't make the JList static and access it that way because JList aren't allowed to be static for whatever reason. I still don't quite understand how you guys are suggesting I solve this problem.
 
Paul Clapham
Sheriff
Posts: 22493
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading the whole thread again, I think Winston isn't quite correct. Your static main method should simply create a MainWindow object and nothing else. Since you said that the JList is #3 of the things which the MainWindow contains, it should be an attribute of the MainWindow class, not a static anything. Presumably you put it somewhere else?
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that is currently what the Main method does. So you're suggesting that I make another class to contain the JList? Sounds like that might work. I'll have to try it. Thank you.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should that List be part of the window class? Should it be part of a Game class? I would definitely prefer the latter.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Reading the whole thread again, I think Winston isn't quite correct. Your static main method should simply create a MainWindow object and nothing else...

@Louis: Paul may well be right; I'm afraid I'm no GUI expert. All I was trying to point out is that it seems highly doubtful to me that anything in your game should be static - apart from maybe some basic constants - so you need to be creating objects to play the game for you.

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