0

Ok, so I made a class called Tile. This class extends JLabel.

public Tile()
{
    super();
}

Should behave the same as JLabel, no? but when I try to use it in my code, it does not show up in the correct position. If I do a call to the bounds of the Tile, it gives what I would expect, yet it is not AT that position. http://puu.sh/58eUg.jpg with the tile used, http://puu.sh/58eVT.png if I used JLabel in the places where Tile was called.

Container cPane = getContentPane();

    for(int x = 0;x<8;x++)
    {
        for(int y = 0;y<8;y++)
        {
            grid[x][y] = new Tile();
            grid[x][y].setBorder(BorderFactory.createLineBorder(Color.black));
            grid[x][y].setBounds(50*x,50*y,100,100);
            getContentPane().add(grid[x][y]);
            cPane.add(grid[x][y]);
        }
    }

Any help would be appreciated.

  • I'm assuming you're using a `null` layout... – MadProgrammer Nov 04 '13 at 03:30
  • 4
    Let me guess: you've overridden the `getX()` and `getY()` methods without realizing that your methods were overrides, maybe? Regardless, consider showing us more code and more information about your problem. An [sscce](http://sscce.org) would be perfect, if you have the ability to make and post one. – Hovercraft Full Of Eels Nov 04 '13 at 03:30
  • Can you post either all of `Tile` and any other code that sets properties on the container (and its content pane), or a complete but minimal test that shows this behavior? – Jason C Nov 04 '13 at 03:32
  • @MadProgrammer Maybe he is setting the size of the `JFrame` instead of `pack()` – An SO User Nov 04 '13 at 03:32
  • Based on you example code, I can get it to work just fine (draws a 8x8 grid)...not the way I would do it, but that's me – MadProgrammer Nov 04 '13 at 03:33
  • 1
    Oh god, I did override the getX and Y methods. How could I forget the horror stories of my peers. Thanks everyone! Love this community. – user2951334 Nov 04 '13 at 03:34
  • 2
    @HovercraftFullOfEels Wow, good call. – Jason C Nov 04 '13 at 03:36
  • @JasonC: thanks, but only because this very problem has smacked me in the face once (and never forgotten). – Hovercraft Full Of Eels Nov 04 '13 at 03:37
  • And to add on to that, how would you do it? I've never been taught a better way to do that part. – user2951334 Nov 04 '13 at 03:37
  • This issue of unknowingly overriding methods could be mitigated by using IDEs that will subtly remind you to put an `@Override` before a method override (e.g. Eclipse or Netbeans). – SimonT Nov 04 '13 at 03:44
  • Yeah, Eclipse didn't tell me this time. The one time I needed it. Oh well. – user2951334 Nov 04 '13 at 03:48
  • @SimonT: for some IDE's such as Eclipse I think, the default behavior is not to remind you of this, but rather they require you to set them up to do this. – Hovercraft Full Of Eels Nov 04 '13 at 03:49
  • @user2951334 In Eclipse: Window -> Preferences -> Java -> Compiler -> Errors/Warnings. Then in the tab on the right expand "annotations" and enable a warning for "missing `@Override` annotation". Somewhat related, you might also want to enable all warnings under the "name shadowing and conflicts" section. Note that project-specific settings will override your settings here (Projects -> Properties -> Java Compiler). – Jason C Nov 04 '13 at 23:38

1 Answers1

5

Aha, my comment to your question was right --

Let me guess: you've overridden the getX() and getY() methods without realizing that your methods were overrides, maybe? ...

You are in fact overriding getX() and getY(). These methods are key methods of JComponent that the layout managers and GUI use to set location of components. If overridden, your component's behavior on the GUI will be completely messed up.

This is one reason to avoid extending complex classes like components unless necessary. Favor composition over inheritance.

For instance you could create a little factory method to create your tile/JLabels, and you could back the component with a non-GUI model class that holds all the smarts of the GUI. An example of this would be something like Conway's Game of Life, where there exists a logical grid LifeSquares as well as a separate GUI that is little more than a "view" of the logical grid as well as has wiring to pass user interactions such as mouse presses to a "control" class.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373