I am trying to make it so the methode makes a window with buttons and returns a Player corresponding to the pushed button but it always gets stuck on the wait without showing the buttons.
static Player proxy = new Player("Proxy");
private static Player pReturn = proxy;
public static Player playerSelection(ArrayList<Player> players, String title) {
int playerNum = players.size();
final Object o = new Object();
JFrame frame = new JFrame(title);
JPanel panel = new JPanel(new GridLayout(-1, 1, 5, 5));
JButton[] btn = new JButton[playerNum];
frame.add(panel);
for (int i = 0;i < playerNum;i++) {
btn[i] = new JButton(players.get(i).getName());
panel.add(btn[i]);
btn[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
synchronized (o) {
for (int i = 0;i < playerNum;i++) {
if (e.getSource().equals(btn[i])) {
pReturn = players.get(i);
}
}
o.notify();
// also supposed to close the window
}
}
});
}
frame.pack();
frame.setVisible(true);
synchronized (o) {
while (pReturn.equals(proxy)) {
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return pReturn;
}
without the wait it just returns the proxy.