SwingWorker in Java

I have a problem.

I have a JFrame . He will create a JDialog .

When the JDialog button is JDialog , it must be removed and an email sent. At the same time, I still JDialog show JDialog with an undefined JProgressBar . When email is sent, JDialog must either be deleted (and the new one crossed), or the contents should change.

I have failed with this for several hours, so I ask enyone if he (or she) is kind enough to write me a pseudo code that will do what I want.

Just to find out what should be included in the SwingWorker class (or use multithreading if you think it is better) when JDialog should be created / deleted, and where to embed email ...

I know that I am asking for a complete solution here, but I am on endurance and have failed so many times ... This was my last resort ...

-3
source share
2 answers

I made a short example because you hope this helps. Basically shown a JFrame with a button:

enter image description here

when the JButton button on the frame is pressed, a JDialog will appear with another JButton (Send Email) - this will be the email dialog:

enter image description here

When JButton clicked on emailDialog , it removes emailDialog and creates a new JDialog that will hold the progress indicator (or in this case a simple JLabel ):

enter image description here

and then creates and runs SwingWorker to send the email and dispose() JDialog when it is done, and displays a JOptionPane message showing the success of the send:

enter image description here

 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Test { public static void main(String[] args) throws Exception { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Test().createAndShowUI(); } }); } private void createAndShowUI() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initComponents(frame); frame.setPreferredSize(new Dimension(300, 300));//testing purposes frame.pack(); frame.setVisible(true); } private void initComponents(final JFrame frame) { final JDialog emailDialog = new JDialog(frame); emailDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); emailDialog.setLayout(new BorderLayout()); JButton sendMailBtn = new JButton("Send Email"); sendMailBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //get content needed for email from old dialog //get rid of old dialog emailDialog.dispose(); //create new dialog final JDialog emailProgressDialog = new JDialog(frame); emailProgressDialog.add(new JLabel("Mail in progress")); emailProgressDialog.pack(); emailProgressDialog.setVisible(true); new Worker(emailProgressDialog).execute(); } }); emailDialog.add(sendMailBtn, BorderLayout.SOUTH); emailDialog.pack(); JButton openDialog = new JButton("Open emailDialog"); openDialog.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { emailDialog.setVisible(true); } }); frame.getContentPane().add(openDialog); } } class Worker extends SwingWorker<String, Object> { private final JDialog dialog; Worker(JDialog dialog) { this.dialog = dialog; } @Override protected String doInBackground() throws Exception { Thread.sleep(2000);//simulate email sending return "DONE"; } @Override protected void done() { super.done(); dialog.dispose(); JOptionPane.showMessageDialog(dialog.getOwner(), "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE); } } 
+12
source

@David Kroukamp

withdrawal from the L&F substance (you have any uncertainties regarding EDT, which is for testing purposes)

 run: JButton openDialog >>> Is there EDT ??? == true Worker started >>> Is there EDT ??? == false waiting 30seconds Worker endeded >>> Is there EDT ??? == false before JOptionPane >>> Is there EDT ??? == false org.pushingpixels.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread 

and another 200 lines about the details

output "correct container created out of EDT"

enter image description here

I will check this on another L&F, there will be problems with Nimbus, SystemLokkAndFeel in most cases does not care about big errors in EDT (very different sensitivity to EDT), Metal by default does not have a release on Windows and Java6, then your example works on second bases.

EDIT

Nimbus doesn't care either

enter image description here

from code

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.plaf.FontUIResource; public class Test { public static void main(String[] args) throws Exception { try { for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); UIManager.getLookAndFeelDefaults().put("nimbusOrange", (new Color(127, 255, 191))); break; } } } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (javax.swing.UnsupportedLookAndFeelException ex) { } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { /*try { UIManager.setLookAndFeel( "org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel"); UIManager.getDefaults().put("Button.font", new FontUIResource(new Font("SansSerif", Font.BOLD, 24))); UIManager.put("ComboBox.foreground", Color.green); } catch (Exception e) { }*/ new Test().createAndShowUI(); } }); } private void createAndShowUI() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initComponents(frame); frame.setPreferredSize(new Dimension(300, 300));//testing purposes frame.pack(); frame.setVisible(true); } private void initComponents(final JFrame frame) { final JDialog emailDialog = new JDialog(frame); emailDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); emailDialog.setLayout(new BorderLayout()); JButton sendMailBtn = new JButton("Send Email"); sendMailBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //get content needed for email from old dialog //get rid of old dialog emailDialog.dispose(); //create new dialog final JDialog emailProgressDialog = new JDialog(frame); emailProgressDialog.add(new JLabel("Mail in progress")); emailProgressDialog.pack(); emailProgressDialog.setVisible(true); new Worker(emailProgressDialog, frame).execute(); } }); emailDialog.add(sendMailBtn, BorderLayout.SOUTH); emailDialog.pack(); JButton openDialog = new JButton("Open emailDialog"); openDialog.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("JButton openDialog >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread()); emailDialog.setVisible(true); } }); frame.getContentPane().add(openDialog); } } class Worker extends SwingWorker<String, Object> { private final JDialog dialog; private final JFrame frame; Worker(JDialog dialog, JFrame frame) { this.dialog = dialog; this.frame = frame; } @Override protected String doInBackground() throws Exception { System.out.println("Worker started >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread()); System.out.println("waiting 30seconds "); Thread.sleep(30000);//simulate email sending System.out.println("Worker endeded >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread()); dialog.dispose(); System.out.println("before JOptionPane >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread()); JOptionPane.showMessageDialog(frame, "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE); System.out.println("before JOptionPane >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread()); return null; } } 
+4
source

All Articles