Solved

Problem making a "busy" box

Posted on 2001-06-14
14
434 Views
Last Modified: 2013-12-13
I am trying to make a "busy" box, which is a dialog box which pops up to tell the user that the program is currently doing some processing. Below is the code for the class.

public class BusyBox extends JDialog implements Runnable {

   JFrame parent;

    public BusyBox(JFrame parent, String title, String message) {
        super( parent, title, true );
        this.parent = parent;
        JLabel label = new JLabel(message);
        label.setHorizontalAlignment(JLabel.CENTER);
        Container contentPane = getContentPane();
        contentPane.add(label, BorderLayout.CENTER);
        setSize(new Dimension(300, 125));
        setResizable(false);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    public void showMessage() {
        Thread thread = new Thread(this);
        thread.start();
    }

    public void run() {
        setLocationRelativeTo(parent);
        setVisible(true);
    }

    public void done() {
        setVisible(false);
    }

}

Here's how I use the code:

BusyBox busyBox = new BusyBox(this, "Processing", "Please wait...");
busyBox.showMessage();
// some heavy processing code here
busyBox.done();

The problem I'm facing is that the text of the label on the dialog box does not show up.
0
Comment
Question by:yongsing
[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
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6194033
Works fine here??
Do you just get a blank dialog box?

Not sure what reason you have for using a thread, but it appears unnecesary. You could simply change showMessage to be:

public void showMessage() {
       setLocationRelativeTo(parent);
       setVisible(true);
   }

   
0
 
LVL 1

Expert Comment

by:tonus
ID: 6194042
I tried the above piece of code and it's working fine. Don't know why it is not working in your system.
0
 
LVL 92

Expert Comment

by:objects
ID: 6194049

import javax.swing.*;
import java.awt.*;

public class BusyBox extends JDialog {

  JFrame parent;

   public BusyBox(JFrame parent, String title, String message) {
       super( parent, title, true );
       this.parent = parent;
       JLabel label = new JLabel(message);
       label.setHorizontalAlignment(JLabel.CENTER);
       Container contentPane = getContentPane();
       contentPane.add(label, BorderLayout.CENTER);
       setSize(new Dimension(300, 125));
       setResizable(false);
       setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
   }

   public void showMessage() {
       setLocationRelativeTo(parent);
       setVisible(true);
   }

   public void done() {
       setVisible(false);
   }
   public static void main(String[] args)
   {
        try{
          BusyBox busyBox = new BusyBox(new JFrame(), "Processing", "Please wait...");
          busyBox.showMessage();
          Thread.sleep(5000);
          busyBox.done();
          }catch (Exception e) { e.printStackTrace(); }
     }

}

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 9

Author Comment

by:yongsing
ID: 6194326
>> Not sure what reason you have for using a thread, but it appears unnecesary. You

The dialog box is made modal so that the user can't click on the frame as the processing is going on. Since it is modal, it will block on calling setVisible(true). Therefore I use a separate thread for calling setVisible(true), so that the processing code can be reached.

For the code you gave, it will not work because Thread.sleep(5000) will never be reached. Unless, of course, I changed the first line of the constructor to super(parent, title, false); Anyway, I tested your code and the label text does show up, but not immediately. There is a fraction of a second delay.

The funny thing is that when I use BusyBox in a larger GUI program, the label text won't show up at all.

If I don't use a busy box, is there any other better way to block the user from clicking on the GUI when there is a processing going on? Besides, I feel that the busy box can inform the user that some processing is going on.

When the user clicks a button on the main frame, the button will stay pressed when there is some heavy processing on the event dispatching thread.
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6194445
> For the code you gave, it will not work

It works fine here. JDialog.setVisible does not block.

> is there any other better way to block the user

Yes, use a glass pane to capture all the user input.
It's covered pretty well in Sun's Java tutorial.

But you're right, sometimes modal dialogs are useful but they can be intrusive to the user. I often use a glass pane to block the user input and display messages to the user on a status bar on the frame.

> When the user clicks a button on the main frame,
> the button will stay pressed when there is some heavy
> processing on the event dispatching thread.

Makes sense, as the event thread is responsible for updating the button.


0
 
LVL 9

Expert Comment

by:Ovi
ID: 6194454
I have myself a question : Why don't you use a simple JOptionPane ? Is highly configurable, you have access to action buttons (enabling, disabling, adding), html support, custom iconimage support (see the api for the rest), and so on ...
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6194466
... and the user is not able to give any input to main app.
0
 
LVL 9

Author Comment

by:yongsing
ID: 6194510
>> It works fine here. JDialog.setVisible does not block.

How can that be? If the dialog is modal, it will block when setVisible(true) is called. Otherwise, what's the meaning of 'modal'?

>> I have myself a question : Why don't you use a simple JOptionPane ?

I thought JOptionPane is for getting inputs from the user. When you call any of the showXXXX methods, it will block. Perhaps you can show me an example of how to use it.

I have never tried objects' suggestion of using glass pane. I will look into it.
0
 
LVL 92

Expert Comment

by:objects
ID: 6194530
The dialog blocks, the call doesn't.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6194548
JOptionPane is use for :
- message
- input
- confirmation
- option.
 ... all this with different options. I will write an example of usage.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6194661
The code :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JOptionPaneCustomizer extends JFrame {
     JButton a1;
     JButton a2;
     JButton a3;
     JButton block;
     JOptionPane opt;

  public JOptionPaneCustomizer() {
          init();
  }

     private void init() {
          getContentPane().setLayout(new GridLayout(4, 1));
          a1 = new JButton("Action 1");
          a2 = new JButton("Action 2");
          a3 = new JButton("Action 3");
          block = new JButton("Block App");
          a1.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                    JOptionPane.showConfirmDialog(JOptionPaneCustomizer.this, "Are you sure ?");
               }
          });
          a2.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                    JOptionPane.showInputDialog(JOptionPaneCustomizer.this, "Enter something:");
               }
          });
          a3.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                    JOptionPane.showMessageDialog(JOptionPaneCustomizer.this, "Wrong button", "Error", JOptionPane.ERROR_MESSAGE);
               }
          });
          block.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                    blockApp();
               }
          });
          getContentPane().add(a1);
          getContentPane().add(a2);
          getContentPane().add(a3);
          getContentPane().add(block);
          setSize(200, 200);
          setLocation(200, 200);
          setVisible(true);
     }

     public void blockApp() {
          String msg = "<html><font color=red><b><i><u>Working</u></i></b>" +
          "<font color=blue><i>, please do something else ...</i></html>";
          opt = new JOptionPane();
          opt.setMessage(msg);
          opt.setMessageType(JOptionPane.WARNING_MESSAGE);
          JLabel l = new JLabel("");
          l.setEnabled(false);

          Object[] action = new Object[1];
          action[0] = l;
          opt.setOptions(action);
          JDialog dlg = opt.createDialog(this, "Announcement");
          dlg.setVisible(true);
     }

     static public void main(String[] args) {
          JOptionPaneCustomizer jopc = new JOptionPaneCustomizer();
     }
}


//*******************************************


The interest method for you is blockApp(). In there I create an dialog and replace the default button OK with an empty JLabel which will be not visible. So the user will not be abble to click nothing in the main app.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6197915
so ...
0
 
LVL 9

Author Comment

by:yongsing
ID: 6200801
Ovi,

You don't seem to understand what I wanted. When my program does processing like a database query, I wanted a dialog box to pop up and says something like "Busy, please wait...". For your example, on popping up the dialog box, the main frame will be blocked, so where do I put the query code? I want the dialog box to be there at the same time that the query is going on.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6200984
so ? Before you start the "query" you pop up the box. Imediately after you do the work, and when is finishing you close (hide) the box. I don't understand why is so a big problem in doing that. The comment above with the JOptionPane was an example of usage, to demonstrate to you some of the capabilities of this class. You can consider in the above example that the blocking operation is made programaticaly in the same situation as the user click's on the 'block' button.
0

Featured Post

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!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

730 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