How can the parallel modification exception be overcome in media graphics?

FYI I adopted a mediation template for my GUI in Swing in Java.

Unfortunately, a parallel modification exception is always thrown if the user needs a new window.

This is because my code is trying to add a new colleague (new window) to the list of colleague intermediaries in the process of processing user input from an existing colleague (window).

eg.

public MainScreenColleague implements GuiColleague, ActionListener {
    private GuiMediator mediator;
    public MainScreenColleague(GuiMediator medi) {
        mediator = medi;
        // implement JFrame with JButtons
    }
    public conveyInputToMediator(EventObject event) {
        mediator.conveyInputToColleagues(event);
    }
    public receiveInputFromMediator(EventObject event) {
        if (event.getSource() = particularBtn) {
            GuiColleague particularColleague = new ParticularConcreteColleague(mediator);
            //THIS IS THE CODE THAT THROWS CONCURRENCY EXCEPTION
            mediator.addGuiColleague(particularColleague);
        }       
}

Is there any other management structure for adding new colleagues that I can accept? Thanks in advance for any suggestions or ideas.

+3
source share
2 answers

, Swing-esque, " " , , . , - , . " , ", , .

, , , , .

. , , , for, :

for (int i = 0; i < elems.size(); ++i)
    /* ... */

.

+1

, datastructure -- CopyOnWriteArrayList.

+1

Source: https://habr.com/ru/post/1790107/


All Articles