List of usage examples for javax.swing SwingUtilities invokeAndWait
public static void invokeAndWait(final Runnable doRun) throws InterruptedException, InvocationTargetException
doRun.run()
to be executed synchronously on the AWT event dispatching thread. From source file:Main.java
/** * A wrapper around SwingUtilities.invokeAndWait() that makes sure that * SwingUtilities.invokeAndWait() is only called when the current thread is * not the AWT event dispatching thread, as required by the documentation * of SwingUtilities.invokeAndWait(); plus catches exceptions thrown by * SwingUtilities.invokeAndWait()./*from ww w. j a v a 2s. c o m*/ * @param runnable The Runnable to call in the event dispatch thread. */ public static void invokeAndWait(Runnable runnable) { try { if (SwingUtilities.isEventDispatchThread()) runnable.run(); else SwingUtilities.invokeAndWait(runnable); } catch (InvocationTargetException ex) { ex.printStackTrace(); } catch (InterruptedException ex) { ex.printStackTrace(); } }
From source file:Main.java
/** * A wrapper around SwingUtilities.invokeAndWait() that makes sure that * SwingUtilities.invokeAndWait() is only called when the current thread is * not the AWT event dispatching thread, as required by the documentation * of SwingUtilities.invokeAndWait(); plus catches exceptions thrown by * SwingUtilities.invokeAndWait().//from w ww .ja v a 2 s . com * @param runnable The Runnable to call in the event dispatch thread. */ public static void invokeAndWait(Runnable runnable) { try { if (SwingUtilities.isEventDispatchThread()) runnable.run(); else SwingUtilities.invokeAndWait(runnable); } catch (InvocationTargetException | InterruptedException ex) { ex.printStackTrace(); } }
From source file:Main.java
/** * Safe handling for the {@link SwingUtilities#invokeAndWait(Runnable)} * method. It is unsafe to call invokeAndWait on the dispatch thread due to * deadlocks. This method simply runs the given {@link Runnable} if this is * called in the dispatch thread./*from w w w . j av a2 s. com*/ * * @param r * - the runnable to run * @throws Exception * - any exceptions propagate, the possible * {@link InvocationTargetException} is unwrapped. */ public static void runOnDispatch(Runnable r) throws Exception { if (SwingUtilities.isEventDispatchThread()) { r.run(); } else { try { SwingUtilities.invokeAndWait(r); } catch (InvocationTargetException iie) { if (iie.getCause() instanceof Exception) { throw (Exception) iie.getCause(); } else { throw iie; } } } }
From source file:Main.java
/** * In EDT just runs the runnable (so in that thread the pending AWT events * are _not_ dispatched before running the runnable). *///w w w . j a v a 2 s . c o m public static void invokeAndWaitFromAnyThread(Runnable r) throws InterruptedException, InvocationTargetException { if (SwingUtilities.isEventDispatchThread()) { try { r.run(); } catch (RuntimeException e) { throw new InvocationTargetException(e); } } else { SwingUtilities.invokeAndWait(r); } }
From source file:Main.java
/** * Execute the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT) * And waits for completion//from w w w . j a va 2 s . c o m * @param runnable runnable code dedicated to Swing * @throws IllegalStateException if any exception occurs while the given runnable code executes using EDT */ public static void invokeAndWaitEDT(final Runnable runnable) throws IllegalStateException { if (isEDT()) { // current Thread is EDT, simply execute runnable: runnable.run(); } else { // If the current thread is interrupted, then use invoke later EDT (i.e. do not wait): if (Thread.currentThread().isInterrupted()) { invokeLaterEDT(runnable); } else { try { // Using invokeAndWait to be in sync with the calling thread: SwingUtilities.invokeAndWait(runnable); } catch (InterruptedException ie) { // propagate the exception because it should never happen: throw new IllegalStateException( "SwingUtils.invokeAndWaitEDT : interrupted while running " + runnable, ie); } catch (InvocationTargetException ite) { // propagate the internal exception : throw new IllegalStateException( "SwingUtils.invokeAndWaitEDT : an exception occured while running " + runnable, ite.getCause()); } } } }
From source file:Main.java
public static JButton newJButton() { final JButton[] jButton = new JButton[1]; try {/*ww w. j a v a 2s .co m*/ SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { jButton[0] = new JButton(); } }); } catch (InterruptedException | InvocationTargetException e) { } return jButton[0]; }
From source file:MainClass.java
public MainClass() { try {/* w w w.j av a2s. c om*/ SwingUtilities.invokeAndWait(new Runnable() { public void run() { JLabel jl = new JLabel(new MyIcon(), JLabel.CENTER); add(jl); } }); } catch (Exception exc) { System.out.println("Can't create because of " + exc); } }
From source file:Main.java
public static int getX(final Component component) { if (component != null) { if (SwingUtilities.isEventDispatchThread()) { return component.getX(); } else {//from www . ja v a2s. c o m final int[] x = new int[1]; try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { x[0] = component.getX(); } }); } catch (InterruptedException | InvocationTargetException e) { } return x[0]; } } else { return 0; } }
From source file:Main.java
public static int getWidth(final Component component) { if (component != null) { if (SwingUtilities.isEventDispatchThread()) { return component.getWidth(); } else {//from w ww. jav a 2 s .c om final int[] width = new int[1]; try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { width[0] = component.getWidth(); } }); } catch (InterruptedException | InvocationTargetException e) { } return width[0]; } } else { return 0; } }
From source file:Main.java
public static int getHeight(final Component component) { if (component != null) { if (SwingUtilities.isEventDispatchThread()) { return component.getHeight(); } else {//from w w w . j a v a 2 s .co m final int[] height = new int[1]; try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { height[0] = component.getHeight(); } }); } catch (InterruptedException | InvocationTargetException e) { } return height[0]; } } else { return 0; } }