Example usage for javax.swing SwingUtilities invokeAndWait

List of usage examples for javax.swing SwingUtilities invokeAndWait

Introduction

In this page you can find the example usage for javax.swing SwingUtilities invokeAndWait.

Prototype

public static void invokeAndWait(final Runnable doRun) throws InterruptedException, InvocationTargetException 

Source Link

Document

Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.

Usage

From source file:com.clank.launcher.swing.SwingHelper.java

/**
 * Asks the user a binary yes or no question.
 *
 * @param parentComponent the component/*w w  w  .  j  av a 2s .  c o m*/
 * @param message the message to display
 * @param title the title string for the dialog
 * @return whether 'yes' was selected
 */
public static boolean confirmDialog(final Component parentComponent, @NonNull final String message,
        @NonNull final String title) {
    if (SwingUtilities.isEventDispatchThread()) {
        return JOptionPane.showConfirmDialog(parentComponent, message, title,
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
    } else {
        // Use an AtomicBoolean to pass the result back from the
        // Event Dispatcher Thread
        final AtomicBoolean yesSelected = new AtomicBoolean();

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    yesSelected.set(confirmDialog(parentComponent, title, message));
                }
            });
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        return yesSelected.get();
    }
}

From source file:SwingTypeTester9.java

private void setText(final String s) {
    try {/*from w  w  w. ja  v  a 2  s .  c  o  m*/
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                label.setText(s);
            }
        });
    } catch (InterruptedException ie) {
        error();
    } catch (InvocationTargetException ite) {
        error();
    }
}

From source file:edu.mit.fss.examples.member.gui.CommSubsystemPanel.java

@Override
public void objectDiscovered(final ObjectChangeEvent event) {
    for (ObjectChangeListener l : listenerList.getListeners(ObjectChangeListener.class)) {
        l.objectDiscovered(event);// w  w w.jav  a 2 s.  c  o m
    }
    // update in event dispatch thread for thread safety
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                if (event.getObject() != subsystem.getTransmitter() && event.getObject() instanceof Transmitter
                        && !connectSeriesMap.containsKey(event.getObject())) {
                    // add new series for transmitter
                    Transmitter tx = (Transmitter) event.getObject();
                    logger.trace("Adding series for transmitter " + tx.getName() + " .");
                    TimeSeries series = new TimeSeries(tx.getName());
                    connectSeriesMap.put(tx, series);
                    connectDataset.addSeries(series);
                }
            }
        });
    } catch (InvocationTargetException | InterruptedException e) {
        logger.error(e);
    }
}

From source file:com.hartveld.commons.test.swing.AbstractSwingFrameTest.java

private void createAndShowFrame() {
    try {/* www.  j av  a 2s.  c o m*/
        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                try {
                    frame = createTestableFrame();
                } catch (final Exception ex) {
                    LOG.error("Failed to create testable frame: {}", ex.getMessage(), ex);
                    throw new RuntimeException(ex.getMessage(), ex);
                }

                frame.addWindowListener(new WindowAdapter() {

                    @Override
                    public void windowClosing(final WindowEvent e) {
                        LOG.trace("Unlocking ...");
                        lock.unlock();
                    }
                });

                frame.pack();
                frame.setVisible(true);
            }
        });
    } catch (InterruptedException | InvocationTargetException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}

From source file:SwingTypeTester8.java

private void setScore() {
    if (SwingUtilities.isEventDispatchThread())
        setText(Integer.toString(score));
    else/*from  w  w w.  j  a  va  2 s  . c  o  m*/
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    setText(Integer.toString(score));
                }
            });
        } catch (InterruptedException ie) {
        } catch (InvocationTargetException ite) {
        }
}

From source file:de.codesourcery.flocking.ui.NumberInputField.java

/**
 * To be invoked when this input components
 * underlying {@link IModel} has changed
 * and thus this component needs repainting.
 */// www .j  a va 2s.  c o  m
public void modelChanged() {

    final Number n = model.getObject();

    final int sliderValue = calcSliderValue(n);

    final Runnable r = new Runnable() {

        @Override
        public void run() {
            selfTriggeredEvent = true;
            slider.setValue(sliderValue);
            textField.setText(numberToString(n));
            selfTriggeredEvent = false;
        }
    };

    if (SwingUtilities.isEventDispatchThread()) {
        r.run();
    } else {
        try {
            SwingUtilities.invokeAndWait(r);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:es.darkhogg.hazelnutt.EditorFrame.java

/**
 * Create the frame. The GUI will be initialized on the Event Dispatch
 * thread. If the initialization is not completed, this constructor throws
 * an unspecified RuntimeException with its cause set to the exception that
 * originally caused the initialization error.
 * /*from ww w .  j a va2  s. co m*/
 * @throws InvocationTargetException 
 * @throws InterruptedException 
 */
public EditorFrame() {
    config = Hazelnutt.getConfiguration();
    logger = Hazelnutt.getLogger();

    //initializeGui();
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                initializeGui();
            }
        });
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.aw.swing.mvp.action.Action.java

public void updateUIStatus() {
    if (needVisualComponent) {
        if (SwingUtilities.isEventDispatchThread()) {
            jComponent.setEnabled(isEnabled());
        } else {/*from w w w  . j  ava 2s .c  o m*/
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        jComponent.setEnabled(isEnabled());
                    }
                });
            } catch (Throwable e) {
                throw new AWSystemException("Problems updating UI:" + this, e);
            }
        }

    }
}

From source file:com.raphfrk.craftproxyclient.gui.CraftProxyGUI.java

public long getCapacity() {
    final AtomicLong capacity = new AtomicLong();
    try {//from w  w w  .  j  a  va 2s  .  c  om
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                long size;
                try {
                    size = Long.parseLong(desiredSize.getText()) * 1024 * 1024;
                } catch (NumberFormatException e) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            GUIManager.messageBox("Unable to parse desired file cache size, using maximum");
                        }
                    });
                    size = Long.MAX_VALUE;
                }
                capacity.set(size);
            }
        });
    } catch (InvocationTargetException e) {
        return Long.MAX_VALUE;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return capacity.get();
}

From source file:edu.mit.fss.examples.member.gui.CommSubsystemPanel.java

@Override
public void objectRemoved(final ObjectChangeEvent event) {
    for (ObjectChangeListener l : listenerList.getListeners(ObjectChangeListener.class)) {
        l.objectRemoved(event);/*from   w  ww.  j  a v a2 s. co  m*/
    }
    // update in event dispatch thread for thread safety
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                if (connectSeriesMap.containsKey(event.getObject())) {
                    TimeSeries series = connectSeriesMap.remove(event.getObject());
                    connectDataset.removeSeries(series);
                    logger.trace("Removing series " + series.getKey() + ".");
                }
            }
        });
    } catch (InvocationTargetException | InterruptedException e) {
        logger.error(e);
    }
}