Solved

JDialog has transparent background, not contents

Posted on 2011-09-22
7
1,755 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
ID: 36584175
Can you post the whole code, so that I could compile and run it myself?
0
 
LVL 1

Author Comment

by:allelopath
ID: 36584819
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
ID: 36584921
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Accepted Solution

by:
gordon_vt02 earned 300 total points
ID: 36586902
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
ID: 36587663
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
ID: 36599270

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 72
ArrayIndexOutOfBoundException 9 67
table example 4 22
tomcat not starting 6 30
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

910 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

20 Experts available now in Live!

Get 1:1 Help Now