模态对话框不工作 [英] Modal dialog not working

查看:180
本文介绍了模态对话框不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当打开第二个对话框作为加载对话框以阻止用户在加载大文件时执行操作,我希望对话框限制任何用户操作(点击等等),同时对话框启动(并且大文件正在加载)。该对话框作为一个非模态对话框,您可以点击回到主窗口并点击东西,但是当使用模态对话框时,它会冻结程序的进程一旦显示。

When opening a second dialog as a 'loading dialog' to prevent user action while loading a large file, I want the dialog to restrict any user action (click and such) while the dialog is up (and the large file is loading). The dialog works as a 'Non-Modal dialog' which you can click back to the main window and click on stuff, but when using a 'Modal dialog' instead it freezes the program's processes once it is displayed.

如何让模态对话框正确显示?

How can I get the modal dialog to display correctly?

代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import java.util.logging.*;
import org.w3c.dom.*;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Loader implements Runnable  {

    final JFileChooser jfc = new JFileChooser();
    static JFrame frame = new JFrame();
    Frame parentUI = new  Frame();
    JDialog dialog = new JDialog();
    JLabel lbl_filename = new JLabel();
    JLabel lbl_path = new JLabel();

    static Loader load = new Loader(null);


    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        load.run();
        frame.setVisible(true);
    } 

    public Loader(Frame parent) {
        init();
        parentUI = parent;
    }

    @Override
    public void run() {
        createDialog(parentUI);
    }  

    public final void init() {
        JButton btn = new JButton("Open");

        frame.setTitle("Loader Test");
        frame.setSize(500, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new FlowLayout());

        btn.addActionListener(new Action1());

        frame.add(btn);
        frame.add(lbl_filename);
        frame.add(lbl_path);
    }

    class Action1 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            openFile();
        }
    }

    private void createDialog(final Frame parent) {

        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setModal(true);
        dialog.setTitle("Loader");

        URL url = this.getClass().getResource("/resource/loader.gif");
        Icon icon = new ImageIcon(url);
        JLabel label = new JLabel(icon);
        dialog.add(label);

        dialog.pack();
        dialog.setLocationRelativeTo(parent);
    }


    public void Show(Boolean visible) {
        this.run();
        dialog.setVisible(visible);
    }

    public void Close() {
        dialog.setVisible(false);
    }

    private void setJFCFilter(String file, String ext) {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(file, ext);
        jfc.setFileFilter(filter);
    }

    private void openFile() {
        File default_dir = new File(".");
        jfc.setCurrentDirectory(default_dir);
        setJFCFilter("Scalable Vector Graphics", "svg");

        int returnVal = jfc.showOpenDialog(parentUI);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            final String path = jfc.getSelectedFile().getAbsolutePath();
            String fileName = jfc.getSelectedFile().getName();

            lbl_filename.setText(fileName);
            lbl_path.setText(path);

            System.out.println("Loading file...");

            load.Show(true);

            new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    createDoc(path);
                    return null;
                };

                @Override
                protected void done() {
                    load.Close();
                };
            }.execute();

            System.out.println("Closing file...");
        }
    }

    private void createDoc(String file) {
        try {
            NodeList svgIDPaths;

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(file);

            String xpathIDExp = "//g/@id";

            XPathFactory xpf = XPathFactory.newInstance();
            XPath xpath = xpf.newXPath();
            XPathExpression expression = xpath.compile(xpathIDExp);

            svgIDPaths = (NodeList)expression.evaluate(doc, XPathConstants.NODESET); // Java OutOfMemory

        } catch (Exception ex) {
            Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


推荐答案

我看到你在做错什么 - 你正在打电话给show,这个方法显示在启动SwingWorker之前的 之前的模式对话框。再次,在显示模态对话框之前,先获取SwingWorker并且所有连线并运行

I see what you're doing wrong, -- you're calling show, the method that displays the modal dialog before starting the SwingWorker. Again, get the SwingWorker and all wired and running before showing a modal dialog.

简化代码(带Thread.sleep)

Simplified code (with Thread.sleep)

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

import java.util.concurrent.TimeUnit;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;

public class Loader implements Runnable {
    static JFrame frame = new JFrame();
    Frame parentUI = new Frame();
    JDialog dialog = new JDialog();
    JLabel lbl_filename = new JLabel();
    JLabel lbl_path = new JLabel();

    static Loader load = new Loader(null);

    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        load.run();
        frame.setVisible(true);
    }

    public Loader(Frame parent) {
        init();
        parentUI = parent;
    }

    @Override
    public void run() {
        createDialog(parentUI);
    }

    public final void init() {
        JButton btn = new JButton("Open");

        frame.setTitle("Loader Test");
        frame.setSize(500, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new FlowLayout());

        btn.addActionListener(new Action1());

        frame.add(btn);
        frame.add(lbl_filename);
        frame.add(lbl_path);
    }

    class Action1 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            openFile();
        }
    }

    private void createDialog(final Frame parent) {

        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setModal(true);
        dialog.setTitle("Loader");

        JLabel label = new JLabel("Label");
        dialog.add(label);

        dialog.pack();
        dialog.setLocationRelativeTo(parent);
    }

    public void show(Boolean visible) {
        this.run();
        dialog.setVisible(visible);
    }

    public void close() {
        dialog.setVisible(false);
    }

    private void openFile() {
        System.out.println("Loading file...");

        // !! load.show(true);

        new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                // createDoc(path);
                createDoc(null);
                return null;
            };

            @Override
            protected void done() {
                load.close();
            };
        }.execute();

        load.show(true); //!! 
    }

    private void createDoc(String file) {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

这篇关于模态对话框不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆