Solved

JDialog has transparent background, not contents

Posted on 2011-09-22
7
1,819 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
null output 3 57
ejb stateless example 2 66
Setup GlassFish 4 55
maximize the sum of fractions 33 74
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

737 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