I have a JPanel with a GridLayout(1,0) set to a JFrame Borderlayout.SOUTH, there are placed a couple of buttons in here (when pressed they change a variables value to the index of the created button).
Everything is added correctly, but when I try to update the JPanel it does work at first (only when window is maximized). But then when I go over a button (or all the buttons) it goes back to what the original content of the panel.
I’ve tried invalidate, validate, repaint, jframe.setvisible(true) – none of these seem to work properly.
Any ideas?
private void toonHand(){
JPanel pnlSouth = new JPanel(new GridLayout(1,0));
JButton[] btnArr =new JButton[50];
ArrayList<Kaart> Thand=uno.getSpeler(0).getHand();
for(int i=0;i<Thand.size();i++){
final int T=i;
btnArr[i]=new JButton();
btnArr[i].setIcon(Thand.get(i).getImg());
btnArr[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
iKaart=T;
}
});
pnlSouth.add(btnArr[i]);
}
Hoofdvenster.remove(pnlSouth);
Hoofdvenster.add(pnlSouth, BorderLayout.SOUTH);
}
The problem was that i was just removing the panel, changing the content and adding it again.
Apparently this was giving some issues with displaying the content.
I fixed it by putting the panels as global variables, and splitting up the making and updating of the panels. So in every update i remove the panel, make the panel a new Panel and then i make it again.