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

public static boolean isResizable(final Frame frame) {
    if (frame != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            return frame.isResizable();
        } else {//from w  w  w . ja  v  a 2  s  .co m
            final boolean[] isResizable = new boolean[1];

            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        isResizable[0] = frame.isResizable();
                    }
                });
            } catch (InterruptedException | InvocationTargetException e) {

            }

            return isResizable[0];
        }
    } else {
        return false;
    }
}

From source file:Main.java

public static void add(final Component component, final Container container) {
    if (component != null && container != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            container.add(component);/*  www  .  ja va  2 s .c  o  m*/
        } else {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        container.add(component);
                    }
                });
            } catch (InterruptedException | InvocationTargetException e) {

            }
        }
    }
}

From source file:Main.java

public static boolean isVisible(final Component component) {
    if (component != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            return component.isVisible();
        } else {//  www  .  ja v a  2  s.c  o m
            final boolean[] isVisible = new boolean[1];

            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        isVisible[0] = component.isVisible();
                    }
                });
            } catch (InterruptedException | InvocationTargetException e) {

            }

            return isVisible[0];
        }
    } else {
        return false;
    }
}

From source file:Main.java

public static String getText(final AbstractButton abstractButton) {
    if (abstractButton != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            return abstractButton.getText();
        } else {//ww w  . jav  a2 s  .com
            final String[] text = { "" };

            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        text[0] = abstractButton.getText();
                    }
                });
            } catch (InterruptedException | InvocationTargetException e) {

            }

            if (text[0] == null) {
                text[0] = "";
            }

            return text[0];
        }
    } else {
        return "";
    }
}

From source file:JTop.java

public static void main(String[] args) throws Exception {
    // Validate the input arguments
    if (args.length != 1) {
        usage();//from www  . j a va  2s .co  m
    }

    String[] arg2 = args[0].split(":");
    if (arg2.length != 2) {
        usage();
    }
    String hostname = arg2[0];
    int port = -1;
    try {
        port = Integer.parseInt(arg2[1]);
    } catch (NumberFormatException x) {
        usage();
    }
    if (port < 0) {
        usage();
    }

    // Create the JTop Panel
    final JTop jtop = new JTop();
    // Set up the MBeanServerConnection to the target VM
    MBeanServerConnection server = connect(hostname, port);
    jtop.setMBeanServerConnection(server);

    // A timer task to update GUI per each interval
    TimerTask timerTask = new TimerTask() {
        public void run() {
            // Schedule the SwingWorker to update the GUI
            jtop.newSwingWorker().execute();
        }
    };

    // Create the standalone window with JTop panel
    // by the event dispatcher thread
    SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
            createAndShowGUI(jtop);
        }
    });

    // refresh every 2 seconds
    Timer timer = new Timer("JTop Sampling thread");
    timer.schedule(timerTask, 0, 2000);

}

From source file:Main.java

/**
 * Runs the specified runnable in the EDT using
 * <code>SwingUtilities.invokeAndWait(...)</code>.
 * Note: This method 'supresses' the method's thrown exceptions
 * - InvocationTargetException and InterruptedException.
 *
 * @param runnable - the runnable to be executed
 *//*from w  w  w. jav  a2  s  . c  o  m*/
public static void invokeAndWait(Runnable runnable) {

    if (!SwingUtilities.isEventDispatchThread()) {

        try {

            //System.err.println("Not EDT");
            SwingUtilities.invokeAndWait(runnable);

        } catch (InterruptedException e) {

            // nothing to do here

        } catch (InvocationTargetException e) {

            // nothing to do here
        }

    } else {

        runnable.run();
    }

}

From source file:MyApplet.java

public void init() {
    try {/* www.j  a v a2s . c o  m*/
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                guiInit(); // initialize the GUI 
            }
        });
    } catch (Exception exc) {
        System.out.println("Can't create because of " + exc);
    }
}

From source file:Main.java

public static final void invokeInAWTThread(Runnable r, boolean wait) {
    if (SwingUtilities.isEventDispatchThread()) {
        r.run();// w w w .jav  a2s . co  m
    } else {
        if (wait) {
            try {
                SwingUtilities.invokeAndWait(r);
            } catch (Exception ex) {
            }
        } else {
            SwingUtilities.invokeLater(r);
        }
    }
}

From source file:Main.java

/**
 * Shows an error dialog.//from  w  ww  . j a v  a  2 s . c om
 * 
 * <p>This can be called from a different thread from the event dispatch
 * thread, and it will be made thread-safe.</p>
 * 
 * @param component component
 * @param title title
 * @param message message
 */
public static void showError(final Component component, final String title, final String message) {
    if (!SwingUtilities.isEventDispatchThread()) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    showError(component, title, message);
                }
            });
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return;
    }

    String newMessage = message.replace(">", "&gt;").replace("<", "&lt;").replace("&", "&amp;");
    newMessage = "<html>" + newMessage;

    JOptionPane.showMessageDialog(component, newMessage, title, JOptionPane.ERROR_MESSAGE);
}

From source file:Main.java

public void init() {
    try {// w  w w .  j  a  va  2  s . c  om
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                makeGUI();
            }
        });
    } catch (Exception exc) {
        System.out.println("Can't create because of " + exc);
    }
}