2

I have a project where I use JDesktopPane for the main application and a bunch of JInternalFrames for a series of independent analyses.

Certain bits of the analyses are time-consuming thus I run them on SwingWorkers, but I would like to both disable the GUI (so no actions are queued) and inform the user that some action is going on and that it's normal.

Previously I have used a custom GlassPane for that purpose, and it has worked nicely before. Now I am experiencing some issues, using the same class as before. Specifically, the glassPane intercepts user input, expected but no visual cue is visible, which makes me think that the paintComponent() is never called on the glassPane.

Just to be sure I googled and came across another implementation (called DisabledGlassPane) of the "please-wait-glassPane" concept but to no success really. While trying to debug the issue I realised that when I start/activate my glassPane it is invalid by default and does not get validated by itself.

If I specifically call validate() on the JInternalFrame after activating the glassPane, it appears to be valid and visible, based on the properties of the glassPane but I see nothing on the screen (both GlassPane implementations have color and text based features that should be immediately visible to the user).

EDIT:

Below is the relevant piece of the code, extracted out of the bigger scheme of things into a minimalist, self-contained (with the exception of the DisabledGlassPane class mentioned above, omitted for the sake of brevity) example. When I run the DesktopFrame class below, and click the button the calculations start, the cursor changes to waiting mode, however the screen is not grayed out, and the message to the user is not displayed, hence my suspicion of paintComponent is never actually called..

I am primarily wondering if I have made an obvious miss, since I am not that experienced with GUI programming and Swing.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingWorker;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;


public class DesktopFrame extends JFrame implements InternalFrameListener{

    private JDesktopPane dpane;
    private JInternalFrame f;
    static DisabledGlassPane gp = new DisabledGlassPane();

    public DesktopFrame()  {
        dpane = new javax.swing.JDesktopPane();
        dpane.setPreferredSize(new java.awt.Dimension(1020, 778));
        setContentPane(dpane);
        addFrame();
        pack();
    }

    public JInternalFrame addFrame(){
        f = new JInternalFrame("test");
        f.setGlassPane(gp);
        f.addInternalFrameListener(this);
        f.setLayout(new GridLayout());
        f.setPreferredSize(new java.awt.Dimension(400,300));
        f.add(new javax.swing.JLabel("something something"));
        f.add(new javax.swing.JTextArea(10, 10));
        javax.swing.JButton but = new JButton("click me!");
        but.setPreferredSize(new java.awt.Dimension(100,50));
        but.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gp.activate("Please wait...");
                SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    for(float i=-3000; i < 3000; i = i + 0.01f){
                        double exp = Math.pow(2,i);
                        double fac = Math.pow(i, 2);
                        System.out.println(exp/fac);
                    }                                   
                    return null;
                    }
                };

                worker.execute();
                try {
                    if(worker.get() == null)
                        gp.deactivate();            

                } catch (InterruptedException | ExecutionException e1) {
                    e1.printStackTrace();
                }

            }
        });
        f.add(but);
        f.setVisible(true);
        f.pack();
        dpane.add(f);

        try {
            f.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
            e.printStackTrace();
        }
        dpane.repaint();
        return f;
    }

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DesktopFrame df = new DesktopFrame();                   
                df.setLocationRelativeTo(null);
                df.setVisible(true);
                df.setDefaultCloseOperation(EXIT_ON_CLOSE);             
            }
        });
    }

    @Override
    public void internalFrameOpened(InternalFrameEvent e) {}
    @Override
    public void internalFrameClosing(InternalFrameEvent e) {}
    @Override
    public void internalFrameClosed(InternalFrameEvent e) {}
    @Override
    public void internalFrameIconified(InternalFrameEvent e) {}
    @Override
    public void internalFrameDeiconified(InternalFrameEvent e) {}
    @Override
    public void internalFrameActivated(InternalFrameEvent e) {}
    @Override
    public void internalFrameDeactivated(InternalFrameEvent e) {}
}
posdef
  • 6,498
  • 11
  • 46
  • 94

1 Answers1

0

but I would like to both disable the GUI (so no actions are queued) and inform the user that some action is going on and that it's normal.

Check out the Disable Glass Pane for a general solution you might be able to use. The above class intercepts mouse and key events and allows you to display a message while the glass pane is visible.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    thanks for the reply, but probably you did not realise that I have already checked that link out and tried the class, I even linked the exact same page. I downvoted this answer, purely based on the fact that it does not add anything to the question, I would be happy to remove my downvote, and instead upvote if you could take a look at the question again, a bit more thoroughly – posdef Jul 08 '14 at 09:57
  • @posdef, Oops, missed the link. Heading out on vacation for a few days in 15 minutes so I have nothing new to offer. You didn't poste a `SSCCE` and I don't have time to try to duplicate your situation. All questions should be posted with a proper SSCCE if you want detailed help. I thought I was just pointing you in the right direction. Good luck. – camickr Jul 08 '14 at 13:39
  • It's a nasty piece of GUI that has many components, I am trying to isolate the problem to a self-contained minimal example, but it's not that easy (at least for me). Thanks anyways, and hope you have a nice vacation :) – posdef Jul 08 '14 at 14:50