Solved

JDialog has transparent background, not contents

Posted on 2011-09-22
7
1,800 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 76
glassfish admin console 1 20
Html split(text) 2 27
Strange router problem - can't access hotmail.com 14 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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