You shouldn't depend on GUI code (the View) to give you information about program data (the model). The best solution would be to "know" which component is where from the start--maybe you should have a data structure (2D array? ) that holds the components and is updated whenever something's added to the grid.
You shouldn't depend on GUI code (the View) to give you information about program data (the model). The best solution would be to "know" which component is where from the start--maybe you should have a data structure (2D array? ) that holds the components and is updated whenever something's added to the grid.
If you want a quick and very-dirty fix, though, you could start playing games with JPanel.getComponentAt(). This requires pixel coordinates, though, so you'd need to do some reverse-engineering to figure out how much space a given grid square takes up. The space between grid squares is given by your GridLayout object.
This is not recommended whatsoever though. I'm just including it in the interest of completeness (and since it's a more literal response to your question).
Okay, I think I know what I was doing wrong. I was just adding a bunch of JLabels to fill the grid with X's and then as players guess certain coordinates the X's turn into numbers. I was not storing the JLabels in any data structure though.
I'll try that now, thank-you! – KubaSub Dec 6 at 2:44 2 @KubaSub, don't store the JLabels...you'd be mixing the model and the view again. Make the game playable without a GUI, then add the GUI to the game...that way you won't be tempted to use UI components as data or data stores.
– Paul Dec 6 at 5:57 1 @Paul raises a good point; see also this MCV example. – trashgod Dec 6 at 13:38 Thanks for your help, unfortunately I already completed my MCV assignment before reading not to store the labels. Just so I understand though, what exactly did you mean by having a 2D array which holds my components?
I must have misunderstood. – KubaSub Dec 10 at 1:06 Let's say that you have the Mark class, which stores an X or an O. To represent a given game state, you could then have a 2D array like this: {{new Mark("O"), new Mark("X"), new Mark()}, {new Mark("X"), ...} ...} To display it, you would iterate through the array and do something like label.
SetText(marksrc.toString()), where r and c are indices into the above array, and the toString() method returns the appropriate symbol. So you have the model--the collection of Marks--and the view--the JLabels. – Tim Macdonald Dec 10 at 18:50.
In GridLayout, "The container is divided into equal-sized rectangles. " You can add an empty, transparent component in places you want to appear empty, e.g. New JLabel(""). See also GridBagLayout and Using Layout Managers.
You might get some ideas from this game that uses a grid of toggle buttons. – trashgod Dec 6 at 2:47.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.