The answer to the following question could not be found: some JFrame is created inside which there are several panels, an ActionListener is created:

public class PushingListener implements ActionListener{ public void actionPerformed (ActionEvent e) { if (e.getActionCommand().equals("Начать бой")){ Graphics.setButton("Продолжить бой", false); battleTank(); } } } 

The button and all fields are updated only after the battleTank function has fully completed. Is it possible to somehow force the updated fields to be updated after each tick inside the battleTank () program?

    3 answers 3

    Try updating your window by timer:

     Timer timer = new Timer(50, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.repaint; } }); 
    • I remembered that repaint () is an asynchronous process. Those. in fact the redraw will happen is not known when. Need a way to immediate peresovki. - Sergey
    • Here's a repaint and does not work, there is still an update, but I don’t understand how to implant it into the program - Alexander

    While in the event dispatch (EDT) thread, call the paintImmediately(...) method
    Argument - a rectangle inside the component to be drawn, in the form of coordinates or Rectangle (you must somehow get this rectangle)

     component.paintImmediately(component.getBounds(null)); 

    Outside the EDT, use SwingUtilities.invokeAndWait(Runnable doRun) to run the code in the EDT synchronously. for example

     SwingUtilities.invokeAndWait(() -> { component.paintImmediately(component.getBounds(null)); }); 

      The problem was solved nontrivially ... after each tick battleTank () throws the command on the corresponding panels centerPanel.update (centerPanel.getGraphics ()).

      component.paintImmediately (component.getBounds (null)) - does not work correctly, as it takes the size of the rectangle, but does not take into account that it is not at the point 0.0 and therefore only updates the piece at the point 0.0 + the size of the rectangle and as a result everything badly)