I'm not sure I understand your problem entirely, but, there are some tricks you can use to change the order in which components are rendered, for example, you can change the componentZOrder
, which will change the order in which the components are laid out and rendered.
If you want to include key board interaction, I highly recommend using the Key Bindings API, for example...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel player;
private List<JLabel> tiles;
public TestPane() {
setLayout(new GridLayout(3, 3));
player = makeLabel("P");
add(player);
tiles = new ArrayList<>(8);
for (int index = 0; index < 8; index++) {
JLabel tile = makeLabel(" ");
tiles.add(tile);
add(tile);
}
addKeyBinding("left", KeyEvent.VK_LEFT, new MoveAction(-1, 0));
addKeyBinding("right", KeyEvent.VK_RIGHT, new MoveAction(1, 0));
addKeyBinding("up", KeyEvent.VK_UP, new MoveAction(0, -1));
addKeyBinding("down", KeyEvent.VK_DOWN, new MoveAction(0, 1));
}
protected void addKeyBinding(String name, int keyCode, Action action) {
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(KeyStroke.getKeyStroke(keyCode, 0), name);
actionMap.put(name, action);
}
protected JLabel makeLabel(String text) {
JLabel label = new JLabel(text);
label.setBorder(new CompoundBorder(
new LineBorder(Color.GRAY),
new EmptyBorder(4, 4, 4, 4)));
return label;
}
public class MoveAction extends AbstractAction {
private final int xDelta, yDelta;
public MoveAction(int xDelta, int yDelta) {
this.xDelta = xDelta;
this.yDelta = yDelta;
}
@Override
public void actionPerformed(ActionEvent e) {
int index = getComponentZOrder(player);
index += xDelta;
index += (yDelta * 3);
if (index < 0) {
index = 0;
} else if (index >= getComponentCount()) {
index = getComponentCount() - 1;
}
setComponentZOrder(player, index);
revalidate();
repaint();
}
}
}
}
Now, the Action
I use handles both vertical and horizontal changes, you might want to consider using two or more, which could better constrain the movement, so if the user tried moving beyond the start or end of the row, it would restrict them, as it stands right now, it allows the component to flow up or down to the next row
You might also like to have a look at How to Use Actions