• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

Problem making a "busy" box

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
yongsing
Asked:
yongsing
  • 6
  • 4
  • 3
  • +1
1 Solution
 
objectsCommented:
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
 
tonusCommented:
I tried the above piece of code and it's working fine. Don't know why it is not working in your system.
0
 
objectsCommented:

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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
yongsingAuthor Commented:
>> 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
 
objectsCommented:
> 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
 
OviCommented:
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
 
OviCommented:
... and the user is not able to give any input to main app.
0
 
yongsingAuthor Commented:
>> 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
 
objectsCommented:
The dialog blocks, the call doesn't.
0
 
OviCommented:
JOptionPane is use for :
- message
- input
- confirmation
- option.
 ... all this with different options. I will write an example of usage.
0
 
OviCommented:
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
 
OviCommented:
so ...
0
 
yongsingAuthor Commented:
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
 
OviCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now