How to update a component from a grid panel through a Listener?
I have the following problem: I create a grid table in this way
public class gui {
JPanel gridPanel = new JPanel();
JPanel background = new JPanel();
JLayeredPane layeredPanel = new JLayeredPane();
...
public gui{
...
for(int i=0; i<2; i++){
JPanel panel= new JPanel(new BorderLayout());
panel.setOpaque(false);
gridPanel.add(panel);
}
gridPanel.setOpaque(false);
layered.add(background,new Integer(1));
layered.add(gridPanel, new Integer(2));
JButton piece = new JButton( new ImageIcon("an image"));
JPanel panel = (JPanel)gridPanel.getComponent(0);
panel.add(piece);
...
}
Ok, this works good, but I want to add an Action Listener to JButton that allow to update the gridPanel
, I thought of addign this in the builder of my GUI:
piece.addActionListener(new Listener(this));
I create a new class ActionListener in this way:
public class Listener implements ActionListener{
private gui gui1;
public movimentoListener(gui gui1){
gui1=gui;
}
public void actionPerformed(ActionEvent e){
JButton piece = new JButton( new ImageIcon("an other image"));
JPanel panel = (JPanel)getGridPanel().getComponent(1); //obviously I've created getGridPanel
panel.add(piece);
gui.getGridPanel().repaint()
}
}
I want when I press the button my actionPerformed
change the component 1 of my gridPanel
with a new image but this code don't work, I try to search on the web but I've not found a solution.