SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Closing a Java Frame

    PLEASE don't tell me to set the default close operation to exit on close. I'm not a Java noob, but this problem has been giving me a headache for hours and hours. Here's my code below. Basically, I have a thread in the background updating the JProgressBar. It works fine, but the JFrame just won't close when I click the red X. After the computation finishes in the thread, the WindowListener takes effect, and the JFrame closes and the System.exit(0) executes, ending the whole program. I have exhausted everything I've read on forums. I've tried using ActionListeners, WindowListeners, a MouseListener, EXIT_ON_CLOSE, _DISPOSE_ON_CLOSE...I swear, everything. If there's something I'm missing, I'd really appreciate any insights on how to close this window.

    Code Java:
    		progress = new JProgressBar(0,10);                         // Make default size to 0,10
            progress.setPreferredSize(new java.awt.Dimension(225,20));              // Set size to small but normal
            progress.setValue(0);                                                   // Set default value to 0
            JFrame dialog = new JFrame("Converting Song...");				// Set frame title
            dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 // Hide the progress bar if closed
            dialog.getContentPane().add(progress);                                  // Add the progress bar to the dialog box
            dialog.setIconImage(Main.imgIcon);
    		dialog.addWindowListener(new WindowListener()
    		{
    			public void windowClosed(WindowEvent e){
    			isRunning = false;
    			System.exit(0);
    			}
    			public void windowOpened(WindowEvent e) {}
    			public void windowClosing(WindowEvent e)
    			{
    				isRunning = false;
    				System.exit(0);
    			}
    			public void windowDeiconified(WindowEvent e) {}
    			public void windowActivated(WindowEvent e) {}
    			public void windowDeactivated(WindowEvent e)
    			{
    				isRunning = false;
    				System.exit(0);
    			}
    			public void windowIconified(WindowEvent e) {
    			}
    		});
    		dialog.pack();                                                          // Pack the progress bar in the dialog box
            dialog.setLocationRelativeTo(parent);                                     // Set the dialog in the center of the frame
            dialog.setVisible(true);                                                // Make it visible
     
     
            progressGraphics = progress.getGraphics();                     // Get the graphics from the progress bar
            new Thread(this, "MIDI Decoder").start();                            // Start the new thread
            while(isRunning)                                                        // As long as it's running
            {
                if ( progress.getValue() != count )                                 // If the progress bar has changed
                {
                    progress.setValue(progressCounter / 10 + count);                                       // Set the level of the bar to the count
                    progress.update(progressGraphics);                              // Update the graphics
                    try 
                    {
                        Thread.sleep(50);                                           // Try sleeping for the other thread to run
                    }
                    catch (InterruptedException ex)
    				{
    					if ( debug ) System.out.println( "Error Pausing" );
    				}
                }
     
    			System.out.println(dialog.isFocusable());
    			dialog.requestFocus();
     
    			System.out.println(dialog.hasFocus());
            }

    As I mentioned, I've tried JFrame.EXIT_ON_CLOSE.

    The problem isn't that the frame won't close, but that it waits for the thread to finish to close. I need a solution that stops the thread in its tracks. It should be doing that right now...setting isRunning to false should kill the thread, but it's not.

    If I set a break in the WindowListener code and run in debug mode, I NEVER reach the break even when clicking the X. That's my problem.

    I should also mention the print statements at the bottom. The first always prints true, the second always prints false. Is this my problem, and how would I resolve that?

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    I think progress bars are one of those things that we assume are easy to use, but turn out to be somewhat complicated. Your best bet is to follow the Sun Progress Bar Tutorial.

  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try another codes! There are many ways to create the same program with different codes.
    I Got my Teacher Certification in 3 months!

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for those of you interested in the solution:

    Although I created a thread, it is not updating anything. I was updating the progress bar in the main thread.

    Code Java:
    progress.setValue(progressCount/ 10 + count);
    progress.update(progressGraphics)

    Then, I slept the main thread, where the handler code is. So the EDT (event dispatcher thread) will never run.

    Code Java:
    try
    {
    	Thread.sleep(50);
    }

    I wrote a simple working solution—no window listener needed. Good luck.

    Code Java:
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
     
    class Example 
    {
    	private static boolean isRunning;
    	private static JProgressBar progress = null;
    	private static int count = 100;
    	public static void main(String... args) 
    	{
    		progress = new JProgressBar(0,count);
    		progress.setPreferredSize(new java.awt.Dimension(225,20));
    		progress.setValue(0);
    		JFrame dialog = new JFrame("");
    		JPanel p = new JPanel();
    		p.add(progress);
    		dialog.add(p, BorderLayout.NORTH);
    		dialog.add(new JLabel("Converting"), BorderLayout.SOUTH);
    		dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		dialog.setSize(500, 100);
    		dialog.setVisible(true);
    		new Thread(new UpdateThread()).start();
    	}
    	private static class UpdateThread implements Runnable 
    	{
    		private int i;
    		private Graphics progressGraphics = null;
    		UpdateThread() 
    		{
    			super();
    			isRunning = true;
    			progressGraphics = progress.getGraphics();
    		}
    		public void run() 
    		{
    			while(isRunning) 
    			{
    				if (progress.getValue() != count)
    				{
    					progress.setValue(i++);
    					progress.update(progressGraphics);
    				}
    				try 
    				{
    					Thread.sleep(50);
    				}
    				catch (InterruptedException ex){}
    			}
    		}
    	}
    }


Bookmarks

Posting Permissions

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