Results 1 to 13 of 13
  1. #1
    etricky is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default JDialog not showing content

    Hello everyone.

    I'm a newbie in swing and I'm trying to create an application and I'm stuck on a problem.
    Basically, what I intend is to show a modal dialog saying that the main application is doing something in the background, however, the dialog containing a label and a progress bar, does not shows this content. It presents a blank dialog.

    I have created a SSCCE which is in the attached files.

    Please, help me out on this.
    Thanks in advance for your help :)
    Attached Files Attached Files

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Many members here, self included, won't click an external link. Post your SSCCE here, in code tags, to improve your chances of getting useful responses.

    db

    edit
    [code]CODE[/code] will display as
    Java Code:
    CODE

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Also, have you heard of ProgressMonitor? And have you gone through the tutorial on Concurrency in Swing?
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Can you post the code in the forums with code tags? I, and many others would prefer not to dl attachments. To post code tags do this
    Java Code:
    YOUR CODE HERE
    @db or fubar, any idea why my code tags are making code tags even though I'm using ASCII for a letter?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You should post your code in the forum with code tags.
    e.g.,

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.WindowConstants;
    
    public class MainClass {
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				Gui gui = new Gui();
    				gui.createAndShowGUI();
    			}
    		});
    	}
    }
    
    class Gui {
    	protected static JPanel	mainPanel;
    	protected static JFrame	mainFrame;
    
    	public void createAndShowGUI() {
    
    		mainFrame = new JFrame();
    
    		mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		mainFrame.setTitle("DEBUG TEST");
    
    		createMainPanel();
    
    		mainFrame.setVisible(true);
    		mainFrame.pack();
    		mainFrame.setLocationRelativeTo(null);
    	}
    
    	private void createMainPanel() {
    		mainPanel = new JPanel(new BorderLayout());
    		mainPanel.setPreferredSize(new java.awt.Dimension(300, 300));
    
    		JPanel panel = new JPanel(new BorderLayout());
    		panel.add(new JLabel("  Main Panel"), BorderLayout.CENTER);
    
    		JButton dialogButton = new JButton("Show dialog");
    		dialogButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
    				showDialog();
    			}
    		});
    
    		mainPanel.add(panel, BorderLayout.CENTER);
    		mainPanel.add(dialogButton, BorderLayout.SOUTH);
    
    		JScrollPane scrollPane = new JScrollPane();
    		scrollPane.setViewportView(mainPanel);
    		scrollPane.setBorder(BorderFactory.createEmptyBorder());
    		mainFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    	}
    
    	private void showDialog() {
    		SimpleProgressBar dialog = new SimpleProgressBar();
    		dialog.showProgressBar(mainFrame);
    		try {
    			Thread.sleep(4000);
    		} catch (InterruptedException e1) {
    		}
    		dialog.dispose();
    	}
    }

    That Thread.sleep is going to put your Swing application to sleep by stepping on the main
    Swing thread, the EDT. Best to run that in a background thread such as a SwingWorker, and your problems are solved.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sunde887 View Post
    Can you post the code in the forums with code tags? I, and many others would prefer not to dl attachments. To post code tags do this
    Java Code:
    YOUR CODE HERE
    @db or fubar, any idea why my code tags are making code tags even though I'm using ASCII for a letter?
    Am having the same problem, so don't know why. :(

  7. #7
    etricky is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Daryl, thanks for the tip.

    Here's the code for the main class

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.WindowConstants;
    
    public class MainClass {
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				Gui gui = new Gui();
    				gui.createAndShowGUI();
    			}
    		});
    	}
    }
    
    class Gui {
    	protected static JPanel	mainPanel;
    	protected static JFrame	mainFrame;
    
    	public void createAndShowGUI() {
    
    		mainFrame = new JFrame();
    
    		mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		mainFrame.setTitle("DEBUG TEST");
    
    		createMainPanel();
    
    		mainFrame.setVisible(true);
    		mainFrame.pack();
    		mainFrame.setLocationRelativeTo(null);
    	}
    
    	private void createMainPanel() {
    		mainPanel = new JPanel(new BorderLayout());
    		mainPanel.setPreferredSize(new java.awt.Dimension(300, 300));
    
    		JPanel panel = new JPanel(new BorderLayout());
    		panel.add(new JLabel("  Main Panel"), BorderLayout.CENTER);
    
    		JButton dialogButton = new JButton("Show dialog");
    		dialogButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
    				showDialog();
    			}
    		});
    
    		mainPanel.add(panel, BorderLayout.CENTER);
    		mainPanel.add(dialogButton, BorderLayout.SOUTH);
    
    		JScrollPane scrollPane = new JScrollPane();
    		scrollPane.setViewportView(mainPanel);
    		scrollPane.setBorder(BorderFactory.createEmptyBorder());
    		mainFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    	}
    
    	private void showDialog() {
    		SimpleProgressBar dialog = new SimpleProgressBar();
    		dialog.showProgressBar(mainFrame);
    		try {
    			Thread.sleep(4000);
    		} catch (InterruptedException e1) {
    		}
    		dialog.dispose();
    	}
    }

    Here is the code that creates the dialog.
    Java Code:
    import java.awt.Dialog;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.SwingWorker;
    import javax.swing.WindowConstants;
    
    public class SimpleProgressBar {
      private static JDialog  dialog;
      private BackgroundJob background;
    
      private class BackgroundJob extends SwingWorker<Void, Void> {
        @Override
        protected Void doInBackground() {
          dialog.add(createSimpleProgressBar());
          dialog.validate();
          dialog.pack();
          dialog.setVisible(true);
          return null;
        }
      }
    
      public void dispose() {
        background.cancel(true);
        dialog.dispose();
      }
    
      public void showProgressBar(JFrame frame) {
    
        // dialog properties
        dialog = new JDialog(frame, Dialog.ModalityType.DOCUMENT_MODAL);
        dialog.setLocationRelativeTo(null);
    
        dialog.setResizable(true);
        dialog.setPreferredSize(new Dimension(400, 200));
    
        dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        dialog.setTitle("Dialog not working!!!");
    
        background = new BackgroundJob();
        background.execute();
    
      }
    
      private JPanel createSimpleProgressBar() {
    
        JPanel panel = new JPanel();
        GridBagLayout layout = new GridBagLayout();
        layout.rowWeights = new double[]{0.1, 0.1};
        layout.rowHeights = new int[]{7, 7};
        layout.columnWeights = new double[]{0.1, 0.1, 0.1};
        layout.columnWidths = new int[]{7, 7, 7};
        panel.setLayout(layout);
    
        JProgressBar progressBar = new JProgressBar();
    
        progressBar.setStringPainted(false);
        progressBar.setIndeterminate(true);
    
        JLabel label = new JLabel("Please wait...");
        label.setFont(new java.awt.Font("Tahoma", 0, 14));
        
        panel.add(label, new GridBagConstraints(1,
                            0,
                            1,
                            1,
                            0.0,
                            0.0,
                            GridBagConstraints.CENTER,
                            GridBagConstraints.NONE,
                            new Insets(5, 5, 5, 5),
                            0,
                            0));
                            
        panel.add(progressBar, new GridBagConstraints(  1,
                                1,
                                1,
                                1,
                                0.0,
                                0.0,
                                GridBagConstraints.CENTER,
                                GridBagConstraints.NONE,
                                new Insets(5, 5, 5, 5),
                                0,
                                0));
        return panel;
    
      }
    }

  8. #8
    etricky is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    @db I have read the tutorial. I believe that my problem is not related to thread concurrency but something simpler than that (so simple that I cannot see it! :( ). Initially, I tried just to show another frame without a swing worker and had the same problem . The new window wouldn't show the content. I can post the code for this example if you think it's necessary.

    @fubarable The Thread.sleep that's in my example it just there to enable the dialog to remain long enough so you can see that it's not showing anything. In my application, I'm updating and committing files in SVN and that takes some time.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by etricky View Post
    @db I have read the tutorial. I believe that my problem is not related to thread concurrency but something simpler than that (so simple that I cannot see it! :( ). Initially, I tried just to show another frame without a swing worker and had the same problem . The new window wouldn't show the content. I can post the code for this example if you think it's necessary.
    No, your problem is concurrency, plain and simple. We see this problem a lot here.


    @fubarable The Thread.sleep that's in my example it just there to enable the dialog to remain long enough so you can see that it's not showing anything. In my application, I'm updating and committing files in SVN and that takes some time.
    @etricky: It doesn't matter what ties up the event dispatch thread or EDT, be it Thread.sleep or a long-running process, if it's not in a background thread, it will freeze the GUI. Please reread the tutorial as you don't fully understand its concepts yet as evidenced by the Thread.sleep on the EDT, and then trying to call Swing calls inside the doInBackground method of a SwingWorker and thus off of the EDT.
    Last edited by Fubarable; 05-01-2011 at 05:11 AM.

  10. #10
    etricky is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Clearly I must have missed something in the tutorial. I will read it again and more carefully.
    Thank you for your help. :)

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Can you post the code in the forums with code tags? I, and many others would prefer not to dl attachments. To post code tags do this
    Java Code:
    YOUR CODE HERE
    @db or fubar, any idea why my code tags are making code tags even though I'm using ASCII for a letter?
    No idea. I don't use 'ASCII' (HTML entities?), I just insert an opening and closing italics tag inside the demo code tag, like this:

    [[i][/i]code]CODE[[i][/i]/code]

    db

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For e.g.,

    Java Code:
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class MainClass2 extends JPanel {
       private DialogPanel dialogPanel = new DialogPanel();
       private JDialog dialog;
       
       public MainClass2(final JFrame frame) {
          add(new JButton(new AbstractAction("Show Dialog") {
             public void actionPerformed(ActionEvent e) {
                if (dialog == null) {
                   dialog = new JDialog(frame, "Dialog Window", true);
                   dialog.getContentPane().add(dialogPanel);
                   dialog.pack();
                   dialog.setLocationRelativeTo(null);
                   dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                }
                BackgroundWorker backgrndWorker = new BackgroundWorker(dialog, 4000L);
                backgrndWorker.execute();
                dialog.setVisible(true);
             }
          }));
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("MainClass2");
          frame.getContentPane().add(new MainClass2(frame));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    @SuppressWarnings("serial")
    class DialogPanel extends JPanel {
       public DialogPanel() {
          JProgressBar progressBar = new JProgressBar();
    
          progressBar.setStringPainted(false);
          progressBar.setIndeterminate(true);
          
          add(progressBar);
       }
    }
    
    class BackgroundWorker extends SwingWorker<Void, Void> {
       private JDialog dialog;
       private long sleepTime;
       
       public BackgroundWorker(JDialog dialog, long sleepTime) {
          this.dialog = dialog;
          this.sleepTime = sleepTime;
       }
    
       @Override
       protected Void doInBackground() throws Exception {
          Thread.sleep(sleepTime);
          return null;
       }
       
       @Override
       protected void done() {
          dialog.setVisible(false);
       }
       
    }

  13. #13
    etricky is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Fubarable and daryl, thank you so much for your support.
    I realize now my mistakes and have learned how not to repeat them.
    Best regards,
    Ricky

Similar Threads

  1. Replies: 1
    Last Post: 04-19-2011, 08:52 AM
  2. JDialog
    By Nuclear Mosquito in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2010, 03:27 PM
  3. Replies: 7
    Last Post: 09-12-2009, 07:06 PM
  4. Replies: 2
    Last Post: 05-30-2009, 10:42 AM
  5. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 05:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •