Example usage for javax.swing SwingWorker execute

List of usage examples for javax.swing SwingWorker execute

Introduction

In this page you can find the example usage for javax.swing SwingWorker execute.

Prototype

public final void execute() 

Source Link

Document

Schedules this SwingWorker for execution on a worker thread.

Usage

From source file:Main.java

public static void main(String[] args) throws UnsupportedEncodingException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JPanel buttons = new JPanel();
    JScrollPane pane = new JScrollPane(buttons);
    pane.getViewport().addChangeListener(e -> {
        System.out.println("Change in " + e.getSource());
        System.out.println("Vertical visible? " + pane.getVerticalScrollBar().isVisible());
        System.out.println("Horizontal visible? " + pane.getHorizontalScrollBar().isVisible());
    });/*w  ww  .j  av  a 2 s .c o  m*/
    panel.add(pane);
    frame.setContentPane(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(800);
                buttons.add(new JButton("Hello " + i));
                buttons.revalidate();
            }
            return null;
        }
    };
    worker.execute();
}

From source file:Main.java

public static void main(String[] args) {
    JProgressBar progressBar = new JProgressBar();
    progressBar.setOpaque(false);//from   ww  w . j a  v a2 s  .c  o m
    progressBar.setUI(new GradientPalletProgressBarUI());

    JPanel p = new JPanel();
    p.add(progressBar);
    p.add(new JButton(new AbstractAction("Start") {
        @Override
        public void actionPerformed(ActionEvent e) {
            SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                @Override
                public Void doInBackground() {
                    int current = 0, lengthOfTask = 100;
                    while (current <= lengthOfTask && !isCancelled()) {
                        try {
                            Thread.sleep(50);
                        } catch (Exception ie) {
                            return null;
                        }
                        setProgress(100 * current / lengthOfTask);
                        current++;
                    }
                    return null;
                }
            };
            worker.addPropertyChangeListener(new ProgressListener(progressBar));
            worker.execute();
        }
    }));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(p);
    frame.setSize(320, 240);
    frame.setVisible(true);
}

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   w  w  w .  jav  a2  s.com*/
 * @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:groovesquid.Main.java

public static void saveConfig() {
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override//from  ww  w. j a va2s.  c  om
        protected Void doInBackground() throws Exception {
            File configFile = new File(configDir + File.separator + "config.json");
            try {
                FileUtils.writeStringToFile(configFile, gson.toJson(config));
            } catch (IOException ex) {
                log.log(Level.SEVERE, null, ex);
            }
            return null;
        }
    };
    worker.execute();
}

From source file:es.emergya.ui.gis.HistoryMapViewer.java

public static void enableSaveGpx(final boolean enabled) {
    SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
        @Override//from ww w . j av  a2 s.c om
        protected Object doInBackground() throws Exception {
            return null;
        }

        @Override
        protected void done() {
            if (saveGpx != null) {
                saveGpx.setEnabled(enabled);
                saveGpx.updateUI();
            }
        }
    };
    sw.execute();
}

From source file:ProgressMonitorInputStreamTest.java

/**
 * Prompts the user to select a file, loads the file into a text area, and sets it as the content
 * pane of the frame.//from   w  ww . j  a  v a 2s .c  o m
 */
public void openFile() throws IOException {
    int r = chooser.showOpenDialog(this);
    if (r != JFileChooser.APPROVE_OPTION)
        return;
    final File f = chooser.getSelectedFile();

    // set up stream and reader filter sequence

    FileInputStream fileIn = new FileInputStream(f);
    ProgressMonitorInputStream progressIn = new ProgressMonitorInputStream(this, "Reading " + f.getName(),
            fileIn);
    final Scanner in = new Scanner(progressIn);

    textArea.setText("");

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        protected Void doInBackground() throws Exception {
            while (in.hasNextLine()) {
                String line = in.nextLine();
                textArea.append(line);
                textArea.append("\n");
            }
            in.close();
            return null;
        }
    };
    worker.execute();
}

From source file:edu.ku.brc.dbsupport.DBConnection.java

/**
 * Shuts down the Embedded process.//from   ww w  .j a  v  a  2s . com
 */
public static void shutdownFinalConnection(final boolean doExit, final boolean doImmediately) {
    if (!finalShutdownComplete.get()) {
        if (shutdownUI != null) {
            shutdownUI.displayShutdownMsgDlg();
        }

        if (doImmediately) {
            doingShutdownFinalConnection(doExit);

        } else {
            javax.swing.SwingWorker<Object, Object> worker = new javax.swing.SwingWorker<Object, Object>() {
                @Override
                protected Object doInBackground() throws Exception {
                    try {
                        Thread.sleep(1000);

                    } catch (Exception ex) {
                    }

                    return null;
                }

                @Override
                protected void done() {
                    super.done();

                    doingShutdownFinalConnection(doExit);
                }
            };
            worker.execute();
        }
    }
}

From source file:info.sugoiapps.xoserver.XOverServer.java

/**
 * Start the file client./*from  w  w w .j a va 2  s.  c  om*/
 */
private void startFileClient() {
    SwingWorker fileclient = new SwingWorker<Void, Integer>() {
        @Override
        protected Void doInBackground() {
            System.out.println("File client about to start with host IP set to " + hostAddress);
            fc = new FileClient(hostAddress);
            return null;
        }
    };
    fileclient.execute();

}

From source file:edu.ku.brc.af.ui.ProcessListUtil.java

/**
 * Check for and kills and existing embedded MySQl processes.
 * @return a status as to whether any were found and whether they were killed.
 *//*w w w  . j a v  a 2  s . co m*/
public static void checkForMySQLProcesses(final ProcessListener listener) {
    ProgressDialog progressDlg = null;
    if (listener != null) {
        progressDlg = new ProgressDialog("Specify EZDB", false, false);
        progressDlg.setDesc("Checking for MySQL Processes...");
        progressDlg.getProcessProgress().setIndeterminate(true);

        final ProgressDialog dlg = progressDlg;
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                UIHelper.centerAndShow(dlg);
            }
        });
    }

    final ProgressDialog dlg = progressDlg;
    javax.swing.SwingWorker<Integer, Integer> worker = new javax.swing.SwingWorker<Integer, Integer>() {
        private PROC_STATUS status = PROC_STATUS.eNone;
        private List<Integer> ids = null;

        @Override
        protected Integer doInBackground() throws Exception {
            status = PROC_STATUS.eOK;
            ids = checkForMySQLPrc();
            if (ids.size() > 0) {
                status = PROC_STATUS.eFoundNotKilled;
            }
            return null;
        }

        @Override
        protected void done() {
            super.done();

            if (status == PROC_STATUS.eFoundNotKilled) {
                if (UIHelper.promptForAction("CONTINUE", "CANCEL", "WARNING",
                        getResourceString("Specify.EMBD_KILL_PROCS"))) {
                    for (Integer id : ids) {
                        killProcess(id);
                    }
                    status = PROC_STATUS.eFoundAndKilled;
                }
            }

            if (listener != null) {
                listener.done(status);
            }

            if (dlg != null)
                dlg.setVisible(false);
        }
    };
    worker.execute();
}

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);

    Box info = Box.createVerticalBox();
    info.add(new Label("Please wait 3 seconds"));
    final JButton continueButton = new JButton("Continue");
    info.add(continueButton);//from  ww w  .j  a v a  2  s . c  om

    JDialog d = new JDialog();
    d.setModalityType(ModalityType.APPLICATION_MODAL);
    d.setContentPane(info);
    d.pack();

    continueButton.addActionListener(e -> d.dispose());
    continueButton.setVisible(false);

    SwingWorker sw = new SwingWorker<Integer, Integer>() {
        protected Integer doInBackground() throws Exception {
            int i = 0;
            while (i++ < 30) {
                System.out.println(i);
                Thread.sleep(100);
            }
            return null;
        }

        @Override
        protected void done() {
            continueButton.setVisible(true);
        }

    };
    JButton button = new JButton("Click Me");
    button.addActionListener(e -> {
        sw.execute();
        d.setVisible(true);
    });
    add(button);
}