0

I am working on Deal or No Deal with a user interface. The first problem I ran into was how to wait for a button action to continue, and I used Count Down Latches and it worked perfectly. But whenever, I click a button, everything in my JFrame disappears and comes back when you mouse over, it all of a sudden reappears when I press another buttton (This never happened before I used Count Down Latches, and this also happens with Semaphores, etc.) I'll try to keep my code as relevant as possible.

    public CountDownLatch cdl = new CountDownLatch(1);
    pickFirst();
    try {
        cdl.await();
    } catch (Exception E) {
    }
    while (banker.findCasesLeft() > 2) {
        banker = new Banker(Main.f.values);

        for (i = casesToPick; i >= 1; i--) {
            cdl = new CountDownLatch(1);
            pickCase();
            picked = false;
            try {
                cdl.await();
            } catch (Exception E) {
            }
        }

^^^ That was my class that deals with picking cases Below is class with actionlisteners

public void actionPerformed(ActionEvent ae) {
    if (!Main.me.pickedFirst) {
        Main.me.pickedCase = caseNo;
        Main.f.log += "You picked to keep case " + caseNo + ".\n";
        setText(caseNo + "\np");
        Main.f.changeLog();
        Main.me.pickedFirst = true;
        Main.me.cdl.countDown();
    } else {
        int value = Main.me.values[caseNo-1];
        Main.me.values[caseNo] = 0;
        Main.f.values[getIndex(value)].setSelected(true);
        Main.f.log += "You picked to get rid of case " + caseNo + ". It contained $" + value + ".\n";
        Main.f.changeLog();
        Main.me.picked = true;
        Main.me.cdl.countDown();
    }
    setEnabled(false);
}
  • 3
    WIthout a [mcve](https://stackoverflow.com/help/mcve) it's impossible to know exactly what the problem might be. However `Main.x` scares me as it suggests that much of your program is based on the use of `static`, not to mention a complete lack of access control... – MadProgrammer Feb 18 '14 at 02:29
  • Will it still be a problem if I'm only using one? – user3183865 Feb 18 '14 at 02:31
  • Also, what else do I need to add to show the problem? – user3183865 Feb 18 '14 at 02:31
  • I have no idea? What happens if you change the reference to one of those `static` variables? What's stopping any other part of your program from doing the same? – MadProgrammer Feb 18 '14 at 02:32
  • 1
    Check the [mcve](https://stackoverflow.com/help/mcve) link, it will tell you everything you need to know – MadProgrammer Feb 18 '14 at 02:33
  • I have fixed the problem. All I had to do was unload the butttons from my JFrame and add them back again. Thanks for the help. – user3183865 Feb 18 '14 at 02:36
  • That would be raising the dead in my mind if I found my self faced with the solution, something is very wrong... – MadProgrammer Feb 18 '14 at 02:38
  • 1
    Updates to the UI shouldn't require to have to re-build the output, the screen going blank isn't a normal state of the UI to go through, there is something seriously wrong with how your UI is working – MadProgrammer Feb 18 '14 at 02:45
  • Maybe its because the bottom class extended a JButton and that is being added to the JFrame? Sorry if I let that out to confuse you. – user3183865 Feb 18 '14 at 02:48
  • *"Sorry if I let that out to confuse you."* If you had posted a MCTaRE as advised by @MadProgrammer in their first comment, we could have known that since it was posted. – Andrew Thompson Feb 18 '14 at 04:26

1 Answers1

2

Note that the await() method of CountDownLatch "Causes the current thread to wait until the latch has counted down to zero." If that thread is the event dispatch thread, GUI updates will be blocked until the wait condition changes. In general, CountDownLatch is meant to allow separate threads to rendezvous; it should't be used to within the event dispatch thread. This complete example, which coordinates multiple SwingWorker instances, may help clarify the usage.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • As an alternative, consider the MVC approach examined [here](http://stackoverflow.com/a/3072979/230513). – trashgod Feb 18 '14 at 10:30