Solved

JDialog has transparent background, not contents

Posted on 2011-09-22
7
1,791 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

860 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