3

how can I make on a button press a new deep copy of a 2 dimensional array?

Basically I created a game field with buttons. The game is called sokoban and it's a puzzle. The player is moving from one button to the other with arrow keys on a fixed map (8x8 buttons). I want to implement an undo function. So I thought that I just create a deep copy of the JButton array before each move and save it into a stack. So when I press the undo button it calls the pop function of my stack. The problem is that I need to declare and initialize another JButton[][] where I can save the game field to before each move. Since I want infinite possible moves and also undos it seems impossible to me. I can't declare and initalize infite diffrent JButton[][] arrays. Any idea on how I can solve that?

That's how I copy a 2d object array:

 JButton[][] tempArray = new JButton[jbArray.length][jbArray[0].length]; for (int i = 0; i < getJbArray().length; i++) { for (int j=0;j<getJbArray()[0].length;j++) { tempArray[i][j]=jbArray[i][j]; } } movesStack.push(tempArray); 
1
  • Maybe you can go simpler: Store the actions , not the state . Revert the action every undo step. Commented Jun 29, 2015 at 14:13

2 Answers 2

2

Unfortunately you can't clone swing components in general, as they do not implement the Cloneable interface. As I see it you have two options:

  1. Create a new JButton inside your double loop and copy whatever properties (like alignment, color etc.) you have set to the new JButton

  2. Write your own class that extends JButton and implement the Cloneable interface

The first way is somewhat of a hack and not very robust or reusable. The second way is much better practice. In this case you'll have to define how the deep copy is supposed to happen, and ensure that all relevant properties are copied over.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I will give it a try and report back.
Your number 1 did the trick! Thanks! I tried nr. 2 as well but it would consume too much time right now. I did not figure out how to do it.
I prefer constructor copy instead of cloning. It is a better practice.
0

You've got the right idea. You're not quite going deep enough.

 public JButton[][] copy(JButton[][] jbArray) { JButton[][] tempArray = new JButton[jbArray.length][jbArray[0].length]; for (int i = 0; i < jbArray.length; i++) { for (int j = 0; j < jbArray[0].length; j++) { tempArray[i][j] = new JButton(jbArray[i][j].getText()); } } return tempArray; } 

Rather than copying JButtons, you should have a model that you use to set the JButtons. Maybe a ModelClass[][] array?

4 Comments

I have to agree with Abishek Manoharan. Every single one of my buttons has a diffrent Icon and Name. Do you think you can change your answer somehow to also cover that?
@K Erlandsson: I'm not designing the class for the OP. I think the OP is asking an XY question.
@Cappuccino90: Nope. Use a model to create / update the JButtons, and save copies of the model.
@GilbertLeBlanc I am sorry if I have said something wrong. I get the impression, that you are a little mad with me. I tried all the ways out that I know and I am still quite a beginner in java. I thank you for the hints you gave me. I will try to figure it out by myself with the hints you gave me, but it's probably gonna take a few hours.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.