• Post Reply Bookmark Topic Watch Topic
  • New Topic

Splitting a Java swing GUI into multiple classes?

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm trying to create a Java swing chess application, but would like to divide the GUI part of it into at least two different classes (possibly more later, but I'm not sure yet). Currently I have a mainGUI class and a ChessBoard class, both of which extend JFrame. I want the main GUI class to contain a JPanel which will house several components, including a JPanel originating from the chessboard class that contains the actual board.

Is there any way to do this? I tried just creating a ChessBoard object in my main GUI class and then adding it to the component, but I got an illegal argument exception, because apparently you can't add one JFrame to another. Does anyone know how to do what I'm trying to do, or just how to split GUIs into multiple classes in general?

Thanks!
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JFrames are entire windows. You can't put a window in a window. Maybe you'd like your ChessBoard to be a JPanel.

Another thing that people will tell you is that it's often better for a class to have a JPanel instead of extending a JPanel. This is because Java is single-inheritance (i.e. you can only extend one class.) What if you wanted your ChessBoard to extend some class GameBoard? You could do this if ChessBoard didn't already extend a GUI component. You could simply return the ChessBoard's GUI representation in some method (e.g. getJPanel()).
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest the following

1) Main class. This is your application entry point. It does nothing except kick start the application.
2) The application frame, i.e. one JFrame instance
3) The chess board (a JPanel with a proper layout manager which will layout the children in a grid (hint))
4) Chess pieces. They only look different (different image?) but all of them are still chess pieces

These would be the high level GUI classes. In addition you will need other things such as the chess engine (which determines legal loves etc)

I will move this topic over to a more appropriate forum for you.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To continue from what Maneesh says: you should have the engine working correctly before you put any GUI atop it. The chess classes should deal with the moves, and the GUI is simply a display.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thanks guys! I think I'm going to proceed as suggested with a Main class, a main GUI class, and a chessboard class that extends JPanel.

As a question for Joel though, what would it look like if I didn't want my chessboard class to extend JPanel, and I instead wanted to create a JPanel inside it? I tried this briefly -- ChessBoard didn't extend anything, but I created a JPanel within it, but then when I tried to instancize it in my main GUI class, I got an illegal argument exception (presumably because it didn't extend anything, but I'm not sure how to fix this)

thanks again.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to show us the code before we can comment about the Exception.

Surely a chessboard would have sixty‑four panels, some black and some white.
Did you know you can find Unicode characters representing chessmen?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:As a question for Joel though, what would it look like if I didn't want my chessboard class to extend JPanel, and I instead wanted to create a JPanel inside it? I tried this briefly -- ChessBoard didn't extend anything, but I created a JPanel within it, but then when I tried to instancize it in my main GUI class, I got an illegal argument exception (presumably because it didn't extend anything, but I'm not sure how to fix this)


Yes, if your Chessboard object doesn't extend a GUI component, you will not be able to add it to another GUI component.

Instead of gui.add(chessboard), you would create a getJPanel() method in Chessboard and do gui.add(chessboard.getJPanel()).
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With regards to the actual chess board itself... I would suggest to make a generic game board instead which can be used to represent any board game...



The point here is that you should always strive to write reusable code...
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thank you all!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!