0

I know this is a race condition, and I've tried a few methods to solve it, including an unsuccessful use of invokeAndWait to no avail. All I want to do is have it change the text on the button to "Connecting", however before the UI updates, it starts the ts = new TallyState(); call which freezes the program for up to 20 seconds while it tries to reach the controller.

JButton btnConnectSerial = new JButton("Connect Controller");
    btnConnectSerial.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            btnConnectSerial.setText("Connecting");
            try {
                ts = new TallyState();
            } catch (IOException tse) {
                JOptionPane.showMessageDialog(null, "Failed to Connect to Controller");
                return;
            } catch (Exception tse2) {
                
            }

I'm sure this has been answered before but I can't seem to phrase my question correctly to Google to get the answer I need.

Also tried this but the rest of the ActionListener executes. It needs wait to know if "ts" was able to be built (e.g. the connection doesn't time out and thus throw exception/message).

    JButton btnConnectSerial = new JButton("Connect Controller");
    btnConnectSerial.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            btnConnectSerial.setText("Connecting");
            SwingWorker myWorker= new SwingWorker<String, Void>()  {
                @Override
                protected String doInBackground() throws Exception {
                    try {
                        ts = new TallyState();
                    } catch (IOException tse) {
                        JOptionPane.showMessageDialog(null, "Failed to Connect to Controller");
                        return null;
                    } catch (Exception tse2) {
                        
                    }
                    return null;
                }
            };
            myWorker.execute();
            

Referenced: Swing, how to properly update the UI, invokeAndWait method in SwingUtilities and links off of them.

Beeker
  • 383
  • 4
  • 20
  • 1
    No, it's not a "race" condition. You're blocking the Swing event thread with the long-running task and what you need to do is to run that task in a background thread, a SwingWorker, as per the "[Lesson: Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)" tutorial. – Hovercraft Full Of Eels Jan 19 '23 at 21:34
  • 2
    If you need to change the state of the GUI once the long-running task has been completed, then use one of the call-back tools that the SwingWorker offers, either by responding to the `done()` method or by using a PropertyChangeListener and listening for the SwingWorker's state to change to `StateValue.DONE`. – Hovercraft Full Of Eels Jan 19 '23 at 21:35
  • 1
    And no, don't use invokeAndWait here, since that does not solve the problem that you're running into. – Hovercraft Full Of Eels Jan 19 '23 at 21:36
  • @HovercraftFullOfEels Excellent information, it's overriding the "done" method was what to execute the rest of the ActionListener code that I was missing. Love the name BTW, here's a box of matches. – Beeker Jan 19 '23 at 21:47
  • 2
    Glad that that worked. For my money, I usually use the PropertyChangeListener route, especially if my worker code is isolated from the gui code, since this allows for looser coupling, since the worker has to have no knowledge about the calling GUI. Also, don't forget to call `get()` from the worker once it has completed its task, if only to capture and handle any exceptions that might occur. – Hovercraft Full Of Eels Jan 19 '23 at 22:19
  • @HovercraftFullOfEels good advice for get(). Not that there's much that could go wrong with enabling buttons/changing text, but it *IS* a computer lol. – Beeker Jan 19 '23 at 22:29
  • No, no, the get will capture any exceptions thrown from the worker's background thread. Often it is tricky to get those exceptions into the GUI. – Hovercraft Full Of Eels Jan 19 '23 at 22:31

0 Answers0