Solved

Problem making a "busy" box

Posted on 2001-06-14
14
430 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 85
base64 decode encode 12 96
Java Jpanels and Jframe 8 20
GUI builder for Eclipse? 8 8
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

708 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

13 Experts available now in Live!

Get 1:1 Help Now