Solved

JDialog has transparent background, not contents

Posted on 2011-09-22
7
1,732 Views
Last Modified: 2012-05-12
With the code below, the dialog box does not display the progress bar.
The interior of the dialog box is transparent (showing as grey in the attached image).
How do I get the progress bar to appear?

I've noticed the if I call:
progressDialog.setModal(true);
then the progress is displayed, but then you have to click on the X to get the dialog box to not display,
setVisible(false) has no effect.
final JProgressBar jProgressBar = new JProgressBar(0, 100);
jProgressBar.setIndeterminate(true);
JDialog progressDialog = createDialog( "Analyzing ..." );
progressDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
progressDialog.getContentPane().add(jProgressBar);
progressDialog.setSize(300, 100);
progressDialog.setVisible(true);
  
  <do stuff>

progressDialog.setVisible(false);

Open in new window

analyzing.png
0
Comment
Question by:allelopath
  • 3
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Can you post the whole code, so that I could compile and run it myself?
0
 
LVL 1

Author Comment

by:allelopath
Comment Utility
Unfortunately, no, it is within a much larger chunk of code.
Can you theorize what setModal(true) might be doing to make it display?
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 200 total points
Comment Utility
Maybe it has something to do with the threads.
This is the code from

http://www.java2s.com/Code/Java/Swing-JFC/Creatingamodalprogressdialog.htm

which I modified - and it runs both when it is modal or not modal
and I added the button which can stop or close it.
Compare it with your code, maybe it will help

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Main implements ActionListener {
    JButton jb;

    Thread t;
    JDialog dlg;
    boolean stop;
  public static void main(String[] args) {
      new Main();
  }

      public Main(){
    JFrame parentFrame = new JFrame();
    parentFrame.setSize(500, 150);
    JLabel jl = new JLabel();
    jb = new JButton("stop");

      jb.addActionListener(this);

    
    jl.setText("Count : 0");

    parentFrame.add(BorderLayout.CENTER, jl);
           parentFrame.add(BorderLayout.EAST, jb);
           

    parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    parentFrame.setVisible(true);

     dlg = new JDialog(parentFrame, "Progress Dialog", false);
    JProgressBar dpb = new JProgressBar(0, 500);
    dlg.add(BorderLayout.CENTER, dpb);
    dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dlg.setSize(300, 75);
    dlg.setLocationRelativeTo(parentFrame);

    t = new Thread(new Runnable() {
      public void run() {
        dlg.setVisible(true);
      }
    });
    t.start();
    for (int i = 0; i <= 500; i++) {
      jl.setText("Count : " + i);
      dpb.setValue(i);
      if(dpb.getValue() == 500 || stop){
       // dlg.setVisible(false);
       // System.exit(0);
          break;

      }
      try {
        Thread.sleep(25);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    dlg.setVisible(true);
  }

    public void actionPerformed(ActionEvent e){
        if(e.getSource().equals(jb)){
            stop = true;
               dlg.setVisible(false);
        }

    }

  }

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Accepted Solution

by:
gordon_vt02 earned 300 total points
Comment Utility
Are you running the task in the main Swing thread or in a background thread?  If you're running it in the main Swing thread, it may not allow the progress bar to update itself because its taking all the processing time.  You should move your progress task into its own thread, but make sure you only update the progress bar in the Swing EventDispatcher thread.

Since you are using an indeterminate progress bar, you won't need to worry about updating it, but you should still run your working code in a background thread and have it stop the progress bar (on the EventDispatch thread) when it completes.
final JProgressBar progressBar = new JProgressBar();
// init progress bar and dialog
new Thread(new Runnable() {
    public void run() {
        // do some unit of work
        
        // ensure progress bar updates happen on
        // the event dispatch thread
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                progressBar.setValue(value);
            }
        });
    }
}).start();

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
Comment Utility
You are both correct in that it is a threading issue. I modified for_yan's code to be closer to what I have (attached). Standalone, this works fine. However, when I incorporate this into my code, the same problem occurs (progress bar does not show). This is because, as gordon_vt02 shows, the work itself must also be in the thread. gordon_vt02's code is not quite right either, however. He shows:
- do the work
- invokeLater

When what it needs to be is:
- invokeLater
- do the work
- progressDialog.setVisible(false)
 
0
 
LVL 1

Author Comment

by:allelopath
Comment Utility

import java.awt.BorderLayout;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class ProgressIndeterminateThread {

	Thread thread;
	JDialog jDialog;

	/**
	 * constructor
	 */
	public ProgressIndeterminateThread(){
		
		final int COUNT = 250;
		
		JFrame parentFrame = new JFrame();
		parentFrame.setSize(500, 150);
		JLabel jLabel = new JLabel();

		jLabel.setText("Count : 0");

		parentFrame.add(BorderLayout.CENTER, jLabel);
		parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		parentFrame.setVisible(true);

		jDialog = new JDialog(parentFrame, "Progress Dialog", false);
		JProgressBar jProgressBar = new JProgressBar(0, COUNT);
	    jProgressBar.setIndeterminate(true);
		jDialog.add(BorderLayout.CENTER, jProgressBar);
		jDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		jDialog.setSize(300, 75);
		jDialog.setLocationRelativeTo(parentFrame);

		thread = new Thread(new Runnable() {
			public void run() {
				jDialog.setVisible(true);
			}
		});
		thread.start();
		
		// do stuff
		for (int i = 0; i <= COUNT; i++) {
			
			jLabel.setText("Count : " + i);
			jProgressBar.setValue(i);
			// when done doing stuff, get out
			if (i == COUNT) {
				break;
			}
			
			// slow the count down
			try {
				Thread.sleep(10);
			} 
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		} // end for do stuff

		jDialog.setVisible(false);

		//parentFrame.setVisible(false);
	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new ProgressIndeterminateThread();
	}
}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now