Ronak Jain
Ronak Jain

Reputation: 2440

How to stop JProgressBar on parent?

I am working on an application where I am using JprogressBar, When I am doing something then progress bar is running. I also added escape functionality there. when anyone click on Escape button of keyboard then focused Jpanel/Jdialog/JFrame is dispose.It working fine. My problem is that JprogressBar is not getting stopped. I want to do that if any Jpanel/Jdialog/JFrame is getting closed through escape button then if it's parent have JProgressBar then it should be getting stopped. How can I do this? Parents of all Jpanel/Jdialog/JFrame can be different?

My Escape Functionality is as above :

public static void setDisposeOnEsc(JComponent c, final Object promptControl) {
        Action escape = new AbstractAction() {

            {
                putValue(NAME, "escape");
            }

            public void actionPerformed(ActionEvent e) {
                    JComponent source = (JComponent) e.getSource();
                    try {
                        Window window = SwingUtilities.getWindowAncestor(source);
                        window.dispose();
                    } catch (Exception ex) {
                    }
                    try {
                        Dialog dialog = (Dialog) source.getFocusCycleRootAncestor();
                        dialog.dispose();
                    } catch (Exception ex) {
                    }
                    try {
                        JFrame jFrame = (JFrame) source.getFocusCycleRootAncestor();
                        jFrame.dispose();
                    } catch (Exception ex) {
                    }   
            }
        };
        Object name = escape.getValue(Action.NAME);
c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"),name);
        c.getActionMap().put(name, escape);
    }

I want to do that if any Jpanel/Jdialog/JFrame which getting disposed through above method,then if it's parent have JprogressBar and is running then it should be stopped.

My English is not so good,so please ignore grammar mistakes.

Thanks in advance.

Upvotes: 2

Views: 401

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

I'm not entirely sure I understand, but you could use something like

public static <T extends Component> List<T> findAllChildren(JComponent component, Class<T> clazz) {

    List<T> lstChildren = new ArrayList<T>(5);
    for (Component child : component.getComponents()) {

        if (clazz.isInstance(child)) {

            lstChildren.add((T) child);

        }

        if (child instanceof JComponent) {

            lstChildren.addAll(findAllChildren((JComponent)child, clazz));

        }

    }

    return Collections.unmodifiableList(lstChildren);

}

To find all the references to JProgressBar, from there you can do what you want...

Upvotes: 1

Related Questions