ciao!
alla pressione di un bottone, vorrei lanciare un dialog (il classico loading dialog) e poi una operazione in background.
questo il codice messo nell'evento legato al bottone:
questo invece il metodo createConnection:codice:btnConnect.addActionListener((ActionEvent e) -> { final JDialog loading = new JDialog(); JPanel p1 = new JPanel(new BorderLayout()); p1.add(new JLabel("Please wait..."), BorderLayout.CENTER); loading.getContentPane().add(p1); loading.pack(); loading.setLocationRelativeTo(null); loading.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); loading.setModal(true); SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { @Override protected Void doInBackground() throws Exception { createConnection(); return null; } }; worker.execute(); loading.setVisible(true); try { worker.get(); } catch (InterruptedException | ExecutionException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } });
alla fine delle operazioni mi esce la scritte FATTO in console, ma il jdialog non si chiude.codice:private void createConnection() { try { if (CheckConnection.check()) { // OPERAZIONI System.out.println("FATTO"); } else { JOptionPane.showMessageDialog(null, "No connection"); } } catch (IOException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } }
dove sto sbagliando??

Rispondi quotando