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:Sender.java

public void init() {
    //Execute a job on the event-dispatching thread; creating this applet's GUI.
    try {//from www . j a v  a2  s.  co m
        final ActionListener al = this;
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                JButton btn = new JButton("Click To Increment Counter");
                add(btn);
                btn.addActionListener(al);
            }
        });
    } catch (Exception e) {
        System.err.println("createGUI didn't complete successfully");
    }
}

From source file:au.org.ala.delta.editor.slotfile.directive.DirInItemDescriptions.java

private void askWhatToDo(Item item) {
    final String description = item.getDescription();

    try {//  w  w  w. j  av a  2  s .  c  o m
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                DuplicateItemWarningDialog dialog = new DuplicateItemWarningDialog(description);
                DeltaEditor editor = (DeltaEditor) Application.getInstance();
                dialog.pack();
                editor.show(dialog);
                _overwrite = dialog.getOverwriteItem();
                if (dialog.getApplyToAll()) {
                    if (_overwrite) {
                        _alwaysOverwriteItem = true;
                        _alwaysRetainItem = false;
                    } else {
                        _alwaysOverwriteItem = false;
                        _alwaysRetainItem = true;
                    }
                } else {
                    _alwaysOverwriteItem = false;
                    _alwaysRetainItem = false;
                }
            }
        });
    } catch (Exception e) {
        _alwaysOverwriteItem = false;
        _alwaysRetainItem = false;
        _overwrite = false;
    }

}

From source file:AppletTakesParams.java

public void init() {
    final String inputStr = getParameter("paramStr");
    final int inputInt = Integer.parseInt(getParameter("paramInt"));
    final String inputOutsideJNLPFile = getParameter("paramOutsideJNLPFile");

    try {//from w ww  .j  av  a 2 s .  com
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                createGUI(inputStr, inputInt, inputOutsideJNLPFile);
            }
        });
    } catch (Exception e) {
        System.err.println("createGUI didn't successfully complete");
    }
}

From source file:PropertiesArgsDemoApplet.java

public void init() {
    final String javaVersion = System.getProperty("java.version");
    final String swing2dNoDrawProperty = System.getProperty("sun.java2d.noddraw");
    final String jnlpMyProperty = System.getProperty("jnlp.myProperty");

    try {//from   w w w . ja v a 2s.  com
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                createGUI(javaVersion, swing2dNoDrawProperty, jnlpMyProperty);
            }
        });
    } catch (Exception e) {
        System.err.println("createGUI didn't successfully complete");
    }
}

From source file:ja.centre.gui.concurrent.TaskSequence.java

private static ITask wrapGuiTask(final ITask task) {
    return new ITask() {
        public void run() throws Exception {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    try {
                        task.run();//from w  ww. j av  a  2s  .  c  o  m
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }

        public String toString() {
            return task.toString();
        }
    };
}

From source file:com.aw.swing.mvp.focus.ConcurrentFocusManager.java

public void invokeAndWait(final JComponent component) {
    final Runnable requestFocusRunner = new Runnable() {
        public void run() {
            logger.debug("Calling request focus in " + Thread.currentThread());
            component.requestFocus();//from w  ww .  ja  v  a 2s  .  c  o  m
        }
    };

    Thread requestFocusThread = new Thread() {
        public void run() {
            try {
                SwingUtilities.invokeAndWait(requestFocusRunner);
            } catch (Exception e) {
                e.printStackTrace();
            }
            logger.debug("Finished on invoke and wait in " + Thread.currentThread());
        }
    };
    requestFocusThread.start();

}

From source file:com.mirth.connect.plugins.globalmapviewer.GlobalMapClient.java

@Override
public void prepareData(List<DashboardStatus> statuses) throws ClientException {
    final Set<String> channelIds = new HashSet<String>();
    // Use this map to look up channel names from channel Ids
    final Map<String, String> channelNameMap = new HashMap<String, String>();

    // Determine which global channel maps to retrieve
    try {/*from   ww  w. ja  v a2s.  c om*/
        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                Set<DashboardStatus> channelStatuses = PlatformUI.MIRTH_FRAME.dashboardPanel
                        .getSelectedChannelStatuses();
                if (channelStatuses != null) {
                    for (DashboardStatus channelStatus : channelStatuses) {
                        channelIds.add(channelStatus.getChannelId());
                        channelNameMap.put(channelStatus.getChannelId(), channelStatus.getName());
                    }
                }
            }

        });
    } catch (Exception e) {
    }

    try {
        data = new Vector<Object>();
        Serializer serializer = ObjectXMLSerializer.getInstance();
        Map<String, String> globalMaps = null;

        selectedRow = 0;
        String currentlySelectedMap = globalMapPanel.getSelectedMap();
        String currentlySelectedVar = globalMapPanel.getSelectedVar();
        try {
            globalMaps = (Map<String, String>) PlatformUI.MIRTH_FRAME.mirthClient
                    .getServlet(GlobalMapServletInterface.class).getAllMaps(channelIds, true);
        } catch (ClientException e) {
            if (e instanceof ForbiddenException) {
                // Don't error. Let an empty map be processed
                parent.alertThrowable(parent, e, false);
            } else {
                throw e;
            }
        }

        if (globalMaps != null) {
            Map<String, String> sortedGlobalMaps = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
            String serializedGlobalMap = null;
            // Sort the maps in order of channel name for better readability
            for (Entry<String, String> channelEntry : globalMaps.entrySet()) {
                if (channelEntry.getKey() == null) {
                    /*
                     * Since the global map's name is null, it cannot be used as a key in the
                     * Tree Map. We also want to display it last, so we'll just store a
                     * reference to it and use it later
                     */
                    serializedGlobalMap = channelEntry.getValue();
                } else {
                    sortedGlobalMaps.put(channelNameMap.get(channelEntry.getKey()), channelEntry.getValue());
                }
            }

            // For each global channel map, display each of its keys alphabetically
            for (Entry<String, String> channelEntry : sortedGlobalMaps.entrySet()) {
                String channelName = channelEntry.getKey();
                String serializedMap = channelEntry.getValue();

                Map<String, Object> sortedMap = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
                sortedMap.putAll(MapUtil.deserializeMap(serializer, serializedMap));

                for (Entry<String, Object> entry : sortedMap.entrySet()) {
                    Vector<Object> row = new Vector<Object>();
                    String entryKey = StringUtil.valueOf(entry.getKey());
                    row.add(channelName);
                    row.add(StringUtil.valueOf(entryKey));
                    row.add(StringUtil.valueOf(entry.getValue()));

                    data.add(row);

                    if (StringUtils.equals(entryKey, currentlySelectedVar)
                            && StringUtils.equals(channelName, currentlySelectedMap)) {
                        selectedRow = data.size();
                    }
                }
            }

            // Now we add the global map if necessary
            if (serializedGlobalMap != null) {
                Map<String, Object> sortedMap = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
                sortedMap.putAll(MapUtil.deserializeMap(serializer, serializedGlobalMap));

                for (Entry<String, Object> entry : sortedMap.entrySet()) {

                    Vector<Object> row = new Vector<Object>();
                    String entryKey = StringUtil.valueOf(entry.getKey());
                    row.add("<Global Map>");
                    row.add(entryKey);
                    row.add(StringUtil.valueOf(entry.getValue()));

                    data.add(row);

                    if (StringUtils.equals(entryKey, currentlySelectedVar)
                            && StringUtils.equals("<Global Map>", currentlySelectedMap)) {
                        selectedRow = data.size();
                    }
                }
            }
        }

    } catch (ClientException e) {
        throw e;
    }
}

From source file:ShowDocument.java

public void init() {
    //Execute a job on the event-dispatching thread:
    //creating this applet's GUI.
    try {// www.  ja  va 2s  .com
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    } catch (Exception e) {
        System.err.println("createGUI didn't successfully complete");
    }
}

From source file:com.qspin.qtaste.testapi.impl.generic.UtilityImpl.java

public void showMessageDialog(final String title, final String message) throws QTasteException {
    try {//  w w  w.jav a2s .  c o m
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(null, message, title, JOptionPane.PLAIN_MESSAGE);
            }
        });
    } catch (Exception e) {
        throw new QTasteException("Error while showing message dialog", e);
    }
}

From source file:com.codecrate.shard.ui.transfer.progress.EventDispatcherThreadProgressMonitor.java

private void updateProgressBar(Runnable task) {
    if (!SwingUtilities.isEventDispatchThread()) {
        try {// w  w  w .  j  a v a2s  .  c om
            SwingUtilities.invokeAndWait(task);
        } catch (Exception e) {
            LOG.warn("Error updating progress bar", e);
        }
    } else {
        task.run();
    }
}