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:SwingThreadingWait.java
public void actionPerformed(ActionEvent e) { start = System.currentTimeMillis(); new Thread(new Runnable() { public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { }/*from w w w . j a va 2s. c o m*/ final int elapsed = (int) ((System.currentTimeMillis() - start) / 1000); SwingUtilities.invokeLater(new Runnable() { public void run() { counter.setText("Time elapsed: " + elapsed + "s"); } }); if (elapsed == 4) { try { final int[] answer = new int[1]; SwingUtilities.invokeAndWait(new Runnable() { public void run() { answer[0] = JOptionPane.showConfirmDialog(SwingThreadingWait.this, "Abort long operation?", "Abort?", JOptionPane.YES_NO_OPTION); } }); if (answer[0] == JOptionPane.YES_OPTION) { return; } } catch (InterruptedException e1) { } catch (InvocationTargetException e1) { } } } } }).start(); }
From source file:org.usfirst.frc.team2084.neuralnetwork.RobotHeadingTest.java
/** * //from w ww . j a va 2 s .c om */ @Override public void run() { try { final DefaultValueDataset headingData = new DefaultValueDataset(0); final DefaultValueDataset desiredHeadingData = new DefaultValueDataset(0); final CompassPlot headingPlot = new CompassPlot(); headingPlot.addDataset(headingData); headingPlot.addDataset(desiredHeadingData); final JFreeChart headingChart = new JFreeChart("Heading", headingPlot); final XYSeries headingTimeSeries = new XYSeries("Heading"); final XYSeriesCollection headingTimeData = new XYSeriesCollection(); headingTimeData.addSeries(headingTimeSeries); final JFreeChart headingTimeChart = ChartFactory.createXYLineChart("Heading vs. Time", "Time", "Heading", headingTimeData, PlotOrientation.VERTICAL, true, true, false); final XYSeries errorTimeSeries = new XYSeries("Error"); final XYSeriesCollection errorTimeData = new XYSeriesCollection(); errorTimeData.addSeries(errorTimeSeries); final JFreeChart errorTimeChart = ChartFactory.createXYLineChart("Error vs. Time", "Time", "Error", errorTimeData, PlotOrientation.VERTICAL, true, true, false); SwingUtilities.invokeAndWait(() -> { final JFrame frame = new JFrame("Charts"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Container content = frame.getContentPane(); content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS)); final JPanel chartPanel = new JPanel(); chartPanel.setLayout(new GridLayout(2, 2)); content.add(chartPanel); final ChartPanel headingPanel = new ChartPanel(headingChart); chartPanel.add(headingPanel); final ChartPanel headingTimePanel = new ChartPanel(headingTimeChart); chartPanel.add(headingTimePanel); final ChartPanel errorTimePanel = new ChartPanel(errorTimeChart); chartPanel.add(errorTimePanel); final JPanel buttonPanel = new JPanel(); content.add(buttonPanel); final JButton startButton = new JButton("Start"); final JButton stopButton = new JButton("Stop"); startButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { stop(); startButton.setEnabled(false); stopButton.setEnabled(true); start(headingData, desiredHeadingData, headingTimeSeries, errorTimeSeries); } }); buttonPanel.add(startButton); stopButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { stop(); startButton.setEnabled(true); stopButton.setEnabled(false); } }); stopButton.setEnabled(false); buttonPanel.add(stopButton); frame.pack(); frame.setVisible(true); }); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:edu.oregonstate.eecs.mcplan.domains.taxi.TaxiVisualization.java
public TaxiVisualization(final TaxiSimulator sim, final int[][] topology, final ArrayList<int[]> locations, final int scale) { if (sim != null) { this.sim = sim; this.state = sim.state(); }/*from w ww.ja v a 2s .c o m*/ this.scale = scale; try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { final JFrame frame = new JFrame(); final Container cp = frame.getContentPane(); cp.setLayout(new BorderLayout()); draw_panel_ = new DrawPanel(topology, locations); final Dimension d = new Dimension(scale * topology.length + 20, scale * topology[0].length + 20); draw_panel_.setPreferredSize(d); draw_panel_.setSize(d); cp.add(draw_panel_, BorderLayout.CENTER); control_panel_ = new ControlPanel(); cp.add(control_panel_, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.pack(); frame.setVisible(true); } }); } catch (final Exception ex) { throw new RuntimeException(ex); } if (sim != null) { updateStateOnEDT(sim.state()); } }
From source file:fr.insalyon.creatis.vip.datamanager.applet.upload.UploadFilesBusiness.java
@Override public void run() { try {// w w w . j a va 2 s .c om SwingUtilities.invokeAndWait(beforeRunnable); File fileToUpload = null; boolean single = true; if (dataList.size() == 1 && new File(dataList.get(0)).isFile()) { fileToUpload = new File(dataList.get(0)); } else { String fileName = System.getProperty("java.io.tmpdir") + "/file-" + System.nanoTime() + ".zip"; FolderZipper.zipListOfData(dataList, fileName); fileToUpload = new File(fileName); zipFileName = fileToUpload.getName(); single = false; } // Call Servlet URL servletURL = new URL(codebase + "/fr.insalyon.creatis.vip.portal.Main/uploadfilesservice"); HttpURLConnection servletConnection = (HttpURLConnection) servletURL.openConnection(); servletConnection.setDoInput(true); servletConnection.setDoOutput(true); servletConnection.setUseCaches(false); servletConnection.setDefaultUseCaches(false); servletConnection.setChunkedStreamingMode(4096); servletConnection.setRequestProperty("vip-cookie-session", sessionId); servletConnection.setRequestProperty("Content-Type", "application/octet-stream"); servletConnection.setRequestProperty("Content-Length", Long.toString(fileToUpload.length())); servletConnection.setRequestProperty("path", path); servletConnection.setRequestProperty("fileName", fileToUpload.getName()); servletConnection.setRequestProperty("single", single + ""); servletConnection.setRequestProperty("unzip", unzip + ""); servletConnection.setRequestProperty("pool", usePool + ""); long fileSize = fileToUpload.length(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileToUpload)); OutputStream os = servletConnection.getOutputStream(); try { byte[] buffer = new byte[4096]; long done = 0; while (true) { int bytes = bis.read(buffer); if (bytes < 0) { break; } done += bytes; os.write(buffer, 0, bytes); progressRunnable.setValue((int) (done * 100 / fileSize)); SwingUtilities.invokeAndWait(progressRunnable); } os.flush(); } finally { os.close(); bis.close(); } BufferedReader reader = new BufferedReader(new InputStreamReader(servletConnection.getInputStream())); String operationID = null; try { String line; while ((line = reader.readLine()) != null) { if (line.startsWith("id=")) { operationID = line.split("=")[1]; } System.out.println(line); } } finally { reader.close(); } if (!single) { fileToUpload.delete(); } if (deleteDataList) { for (String data : dataList) { FileUtils.deleteQuietly(new File(data)); } } result = operationID; SwingUtilities.invokeAndWait(afterRunnable); } catch (Exception ex) { result = ex.getMessage(); SwingUtilities.invokeLater(errorRunnable); } }
From source file:com.qspin.qtaste.testapi.impl.generic.UtilityImpl.java
@Override public boolean getUserConfirmation(final String title, final String message) throws QTasteException { final MutableBoolean confirmed = new MutableBoolean(); try {//from w w w. j a v a 2s .c o m SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { int result = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION); confirmed.setValue(result == JOptionPane.YES_OPTION); } }); } catch (Exception e) { throw new QTasteException("Error while showing user confirmation dialog", e); } return confirmed.booleanValue(); }
From source file:edu.oregonstate.eecs.mcplan.domains.racetrack.RacetrackVisualization.java
public RacetrackVisualization(final Circuit circuit, final RacetrackSimulator sim, final double scale) { if (sim != null) { this.sim = sim; this.state = sim.state(); }/*from w w w . java2s . co m*/ this.circuit = circuit; this.scale = scale; try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { final JFrame frame = new JFrame(); final Container cp = frame.getContentPane(); cp.setLayout(new BorderLayout()); draw_panel_ = new DrawPanel(); final Dimension d = new Dimension((int) scale * circuit.width + 20, (int) scale * circuit.height + 20); draw_panel_.setPreferredSize(d); draw_panel_.setSize(d); cp.add(draw_panel_, BorderLayout.CENTER); control_panel_ = new ControlPanel(); cp.add(control_panel_, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.pack(); frame.setVisible(true); } }); } catch (final Exception ex) { throw new RuntimeException(ex); } if (sim != null) { updateStateOnEDT(sim.state()); } }
From source file:ThreadViewer.java
private void runWork() { Runnable transferPending = new Runnable() { public void run() { transferPendingCellData();/* w w w . j a v a 2 s . c om*/ fireTableDataChanged(); } }; while (noStopRequested) { try { createPendingCellData(); SwingUtilities.invokeAndWait(transferPending); Thread.sleep(5000); } catch (InvocationTargetException tx) { tx.printStackTrace(); stopRequest(); } catch (InterruptedException x) { Thread.currentThread().interrupt(); } } }
From source file:org.jtheque.ui.impl.UIUtilsImpl.java
@Override public boolean askUserForConfirmation(final String text, final String title) { boolean yes = false; Window parent = null;// ww w. ja v a 2 s . com if (SimplePropertiesCache.get(MAIN_VIEW_CACHE, Window.class) != null) { parent = SimplePropertiesCache.get(MAIN_VIEW_CACHE, Window.class); } final Window p = parent; final int[] response = new int[1]; if (SwingUtilities.isEventDispatchThread()) { response[0] = JOptionPane.showConfirmDialog(parent, text, title, JOptionPane.YES_NO_OPTION); } else { try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { response[0] = JOptionPane.showConfirmDialog(p, text, title, JOptionPane.YES_NO_OPTION); } }); } catch (InterruptedException e) { LoggerFactory.getLogger(getClass()).error(e.getMessage(), e); } catch (InvocationTargetException e) { LoggerFactory.getLogger(getClass()).error(e.getMessage(), e); } } if (response[0] == JOptionPane.YES_OPTION) { yes = true; } return yes; }
From source file:net.sourceforge.jabm.view.BarChart.java
public void compute(ReportVariablesChangedEvent event) { computeVariableNames();// ww w .jav a 2 s .c o m final Dataset eventOriginator = this; try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { for (DatasetChangeListener listener : listeners) { listener.datasetChanged(new DatasetChangeEvent(eventOriginator, eventOriginator)); } } }); } catch (InterruptedException e) { logger.warn(e); Thread.currentThread().interrupt(); } catch (InvocationTargetException e) { logger.warn(e); } }
From source file:Main.java
/** * Invokes {@code runnable} in the Event Dispatch Thread and waits for its execution to complete. * <p>/*from w ww . ja v a 2s. c om*/ * This method is similar to the one provided by {@code SwingUtilities}. But it differs in that you don't have to catch its checked {@code Exception}s. It simply ignores them. * <p> * If {@code runnable} is {@code null}, nothing will happen. * * @param runnable the {@code Runnable} to invoke and execute in the Event Dispatch Thread */ public static void invokeAndWait(final Runnable runnable) { if (runnable != null) { try { SwingUtilities.invokeAndWait(runnable); } catch (final InvocationTargetException | InterruptedException e) { // Do nothing. } } }