Example usage for javax.swing SwingUtilities invokeLater

List of usage examples for javax.swing SwingUtilities invokeLater

Introduction

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

Prototype

public static void invokeLater(Runnable doRun) 

Source Link

Document

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

Usage

From source file:Main.java

public static final void invoke(Runnable r) {
    if (SwingUtilities.isEventDispatchThread())
        r.run();/*from  w ww.java 2 s  .  c  o m*/
    else
        SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void runInEdt(Runnable r) {
    if (SwingUtilities.isEventDispatchThread()) {
        r.run();//from  ww  w .  ja va 2s.  co m
    } else {
        SwingUtilities.invokeLater(r);
    }
}

From source file:Main.java

/**
 * If runOnEDT is true will execute the thread on the Event Dispatch Thread
 * Otherwise it will use a SwingWorker//from  ww  w.j a  v  a  2 s  . c  o m
 * @param r <code>Runnable</code> to execute
 * @param runOnEDT run on Event Dispatching Thread
 *
 * @see javax.swing.SwingWorker
 * @see java.lang.Runnable
 * @deprecated
 */
@Deprecated
public static void runTask(final Runnable r, boolean runOnEDT) {
    if (runOnEDT) {
        SwingUtilities.invokeLater(r);
        return;
    }

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        @Override
        protected Void doInBackground() throws Exception {
            r.run();
            return null;
        }
    };
    worker.execute();
}

From source file:Main.java

public static void safeSwing(@Nonnull final Runnable runnable) {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();//from  w  w w.ja v  a 2  s  . c om
    } else {
        SwingUtilities.invokeLater(runnable);
    }
}

From source file:Main.java

public static Runnable invokeLater(Runnable r) {
    return () -> {
        SwingUtilities.invokeLater(r);
    };
}

From source file:com.diversityarrays.dal.server.DalServer.java

public static void main(String[] args) {

    String host = null;/* w  w  w .  j av a2  s. c om*/
    int port = DalServerUtil.DEFAULT_DAL_SERVER_PORT;

    int inactiveMins = DalServerUtil.DEFAULT_MAX_INACTIVE_MINUTES;

    File docRoot = null;

    String serviceName = null;

    for (int i = 0; i < args.length; ++i) {
        String argi = args[i];
        if (argi.startsWith("-")) {
            if ("--".equals(argi)) {
                break;
            }

            if ("-version".equals(argi)) {
                System.out.println(DAL_SERVER_VERSION);
                System.exit(0);
            }

            if ("-help".equals(argi)) {
                giveHelpThenExit(0);
            }

            if ("-docroot".equals(argi)) {
                if (++i >= args.length || args[i].startsWith("-")) {
                    fatal("missing value for " + argi);
                }
                docRoot = new File(args[i]);
            } else if ("-sqllog".equals(argi)) {
                SqlUtil.logger = Logger.getLogger(SqlUtil.class.getName());
            } else if ("-expire".equals(argi)) {
                if (++i >= args.length || args[i].startsWith("-")) {
                    fatal("missing value for " + argi);
                }

                try {
                    inactiveMins = Integer.parseInt(args[i], 10);
                    if (inactiveMins <= 0) {
                        fatal("invalid minutes: " + args[i]);
                    }
                } catch (NumberFormatException e) {
                    fatal("invalid minutes: " + args[i]);
                }
            } else if ("-localhost".equals(argi)) {
                host = "localhost";
            } else if ("-port".equals(argi)) {
                if (++i >= args.length || args[i].startsWith("-")) {
                    fatal("missing value for " + argi);
                }
                try {
                    port = Integer.parseInt(args[i], 10);
                    if (port < 0 || port > 65535) {
                        fatal("invalid port number: " + args[i]);
                    }
                } catch (NumberFormatException e) {
                    fatal("invalid port number: " + args[i]);
                }
            } else {
                fatal("invalid option: " + argi);
            }
        } else {
            if (serviceName != null) {
                fatal("multiple serviceNames not supported: " + argi);
            }
            serviceName = argi;
        }
    }

    final DalServerPreferences preferences = new DalServerPreferences(
            Preferences.userNodeForPackage(DalServer.class));

    if (docRoot == null) {
        docRoot = preferences.getWebRoot(new File(System.getProperty("user.dir"), "www"));
    }

    DalServer server = null;

    if (serviceName != null && docRoot.isDirectory()) {
        try {
            DalDatabase db = createDalDatabase(serviceName, preferences);
            if (db.isInitialiseRequired()) {
                Closure<String> progress = new Closure<String>() {
                    @Override
                    public void execute(String msg) {
                        System.out.println("Database Initialisation: " + msg);
                    }
                };
                db.initialise(progress);
            }
            server = create(preferences, host, port, docRoot, db);
        } catch (NoServiceException e) {
            throw new RuntimeException(e);
        } catch (DalDbException e) {
            throw new RuntimeException(e);
        }
    }

    Image serverIconImage = null;
    InputStream imageIs = DalServer.class.getResourceAsStream("dalserver-24.png");
    if (imageIs != null) {
        try {
            serverIconImage = ImageIO.read(imageIs);

            if (Util.isMacOS()) {
                try {
                    MacApplication macapp = new MacApplication(null);
                    macapp.setDockIconImage(serverIconImage);
                } catch (MacApplicationException e) {
                    System.err.println(e.getMessage());
                }
            }

        } catch (IOException ignore) {
        }
    }

    if (server != null) {
        server.setMaxInactiveMinutes(inactiveMins);
    } else {
        AskServerParams asker = new AskServerParams(serverIconImage, null, "DAL Server Start", docRoot,
                preferences);
        GuiUtil.centreOnScreen(asker);
        asker.setVisible(true);

        if (asker.cancelled) {
            System.exit(0);
        }

        host = asker.dalServerHostName;
        port = asker.dalServerPort;
        inactiveMins = asker.maxInactiveMinutes;

        server = create(preferences, host, port, asker.wwwRoot, asker.dalDatabase);
        // server.setUseSimpleDatabase(asker.useSimpleDatabase);
    }

    final DalServer f_server = server;
    final File f_wwwRoot = docRoot;
    final Image f_serverIconImage = serverIconImage;

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            DalServerFactory factory = new DalServerFactory() {
                @Override
                public DalServer create(String hostName, int port, File wwwRoot, DalDatabase dalDatabase) {
                    return DalServer.create(preferences, hostName, port, wwwRoot, dalDatabase);
                }
            };
            ServerGui gui = new ServerGui(f_serverIconImage, f_server, factory, f_wwwRoot, preferences);
            gui.setVisible(true);
        }
    });

}

From source file:Main.java

public static void focusLater(final Component component) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override/*from  w w w .j  av a2 s  .  c  o m*/
        public void run() {
            if (component instanceof JTextComponent) {
                ((JTextComponent) component).selectAll();
            }
            component.requestFocusInWindow();
        }
    });
}

From source file:com.bryan.gui.HotelBrokerApp.java

public static void main(String[] args) {
    try {//from  ww  w.ja v a2s. c o  m

        final HotelBrokerApp hotelBrokerApp = new HotelBrokerApp("Monash Hotel Broker");

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                hotelBrokerApp.initView();
            }
        });

    } catch (Exception ex) {
        System.out.println("Failed to run application: " + ex.getMessage());
    }
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 *
 * @param spinner// w ww  .  j  av  a 2  s.c o m
 *            The spinner to fix
 */

public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = (JTextComponent) e.getSource();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 * // ww  w  . j av  a2  s .c o  m
 * @param spinner
 *            The spinner to fix
 */
public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = ((JTextComponent) e.getSource());
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}