JaperTIA
JaperTIA

Reputation: 129

Repaint JPanel from actionlistener

I know there are multiple threats on how to repaint a JPanel. I've been trying to repaint a JPanel from inside an actionlistener by applying the revalidate() and repaint() methods (as found at stackoverflow). Sadly enough this isn't working. But when i just change the text of a button, it's repainting!

public SimulationPanel()
{
      //configure panel.....

      /* ActionListener */
      btnStepsIntoFuture.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) 
        {               
            //Do something
            /* refresh grid panel */
            worldPanel.createGrid();
            simPanel.revalidate(); //= not working
            simPanel.repaint(); //= not working
            //btnEndSim.setText("End world simulation "); = working
            //btnEndSim.setText("End world simulation"); = working          
        }
      });

     /* Add components to Simulationpanel */
     simPanel.add(buttonsOnTopPanel, BorderLayout.NORTH);
     simPanel.add(worldPanel.getWorldPanel(), BorderLayout.CENTER);
     simPanel.add(stepsIntoFuturePanel, BorderLayout.SOUTH);
}

extra info: the worldpanel is a grid inside the simulationpanel but I guess this doesn't matter because the repaint works by changing the text of the buttons..

EDIT:

    public void createGrid(){ 
    /* Set constraint on GridBagLayout */
    GridBagConstraints gbc = new GridBagConstraints();
    /* Create world grid with panels */
    for (int row = 0; row < SettingsPanel.getNrOfRows(); row++) {
        for (int col = 0; col < SettingsPanel.getNrOfColumns(); col++) {
            /* Add constraint for correct dimension (row ; column) */
            gbc.gridx = col;
            gbc.gridy = row;

            /* Initialize new cell in world grid */
            CellPanel cellPanel = new CellPanel();

            /* Draw elements of grid */
            drawBackgroundIcons(cellPanel, row, col);
            drawBorders(cellPanel, row, col);

            worldPanel.add(cellPanel, gbc);
        }
    }
    /* Print overview */
    printOverviewOfWorld();
 }


/*  draw background icon of object */
public void drawBackgroundIcons(CellPanel cellPanel, int row, int col)
{
    /*  Set person, zombie weapon icon as background image */
    if(personArray[row][col] != null)
    {
        Image img = new ImageIcon(this.getClass().getResource("/resources/"+personArray[row][col].getName()+".png")).getImage();
        cellPanel.setImg(img);
    }

}

I let the objects move 1 step on the grid (right, left, up, diagonal, ...) by changing the position in the twodimensional array.

How can I make it work with the revalidate/repaint methods?

Cheers

Upvotes: 0

Views: 611

Answers (1)

Pedram
Pedram

Reputation: 21

You may use the method update(Graphics g) like below :

jPanel.update(jPanel.getGraphics());

worked for me without having to use any Worker or Runnable.

still can't figure out why it repaints the Minimum Size of JFrame , but It works.

Upvotes: 2

Related Questions