Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java swing setLayout method does not override or implement a method from a super type?  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to make a ChessBoard class composed of an array of JLabels inside a JPanel with a grid layout. I am also trying to override the getPreferredSize method so that the board will change size when I resize the main window (in another class in which I will instancize this class as part of a larger GUI). I got this kind of layout working before, but now I am trying to get it to work with multiple classes. However, after copying in the part of the previous code corresponding to the panel's layout, I am encountering some errors that I don't know how to solve. Specifically, when I try to override the getPreferredSize method, the compiler tells me "method does not override or implement a method from a super type, " and that it can't find the method "getPreferredSize" Here's my code:



Any idea what I could be doing wrong? I would just think that I was overriding the method incorrectly, but the weird thing is that I got that specific section of code to work before -- the only thing different now is that there are multiple classes, so my ChessBoard class itself is extending JPanel.
 
Paweł Baczyński
Bartender
Posts: 1997
42
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Override public final Dimension getPreferredSize()
This declaration is illegal in a direct subclass of GridLayout because GridLayout does not define getPreferredSize() method.
You can only use @Override on a method that is actually overriden.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does your chess board class have to extend anything? Use a plain panel and add sixty‑four panels to it. You will probably have to override their paintComponent methods. You may have to add setPiece and getPiece methods and piece as a field.

Beware: since at least half the squares on a chessboard are vacant, they will have null for their piece. Nulls are dangerous, but here unavoidable. You must allow for nulls in any methods using the piece field.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use JLabels with ImageIcons? No need to override a painting method, and setIcon(null) doesn't cause any problems.

An enum that implements Icon and wraps an ImageIcon might be a good choice for the chess pieces.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell Ritchie suggested, I switched my code so that the ChessBoard class itself doesn't extend to JPanel, but rather has a JPanel within it and a get JPanel method. This solves the problem, as I replaced this code:



with the following:



Any idea why this change in syntax of declaration removed my error? Oh, and thank you other guys for the general design advice, it's just that I'd like to solve this problem/understand how I've solved it first.
 
Tony Docherty
Bartender
Posts: 3195
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel has already explained why you get the error.
The difference between your two code samples are: in your first example you are providing an anonymous class extending GridLayout whereas in the second example your anonymous class extends JPanel.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!