Java 模态对话框不工作

Java 模态对话框不工作,java,swing,dialog,Java,Swing,Dialog,当以“加载对话框”的形式打开第二个对话框以防止用户在加载大文件时执行操作时,我希望该对话框在对话框打开(并且加载大文件)时限制任何用户操作(单击等)。该对话框作为“非模式对话框”工作,您可以单击返回主窗口并单击内容,但当使用“模式对话框”时,它会在显示程序后冻结程序的进程 如何使模态对话框正确显示 代码: import javax.swing.*; 导入java.awt.*; 导入java.awt.event.*; 导入javax.xml.parsers.*; 导入javax.xml.xpath

当以“加载对话框”的形式打开第二个对话框以防止用户在加载大文件时执行操作时,我希望该对话框在对话框打开(并且加载大文件)时限制任何用户操作(单击等)。该对话框作为“非模式对话框”工作,您可以单击返回主窗口并单击内容,但当使用“模式对话框”时,它会在显示程序后冻结程序的进程

如何使模态对话框正确显示

代码:

import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入javax.xml.parsers.*;
导入javax.xml.xpath.*;
导入java.util.logging.*;
导入org.w3c.dom.*;
导入java.io.File;
导入java.lang.reflect.InvocationTargetException;
导入java.net.URL;
导入javax.swing.filechooser.FileNameExtensionFilter;
公共类加载器实现可运行{
final JFileChooser jfc=新的JFileChooser();
静态JFrame=新JFrame();
帧父用户界面=新帧();
JDialog dialog=新建JDialog();
JLabel lbl_filename=新的JLabel();
JLabel lbl_path=新JLabel();
静态加载程序加载=新加载程序(null);
公共静态void main(字符串[]args)抛出InterruptedException、InvocationTargetException{
load.run();
frame.setVisible(true);
} 
公共加载程序(帧父级){
init();
parentUI=父;
}
@凌驾
公开募捐{
createDialog(parentUI);
}  
公共最终void init(){
JButton btn=新JButton(“打开”);
frame.setTitle(“装载机测试”);
框架设置尺寸(500200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(空);
frame.setLayout(新的FlowLayout());
addActionListener(新Action1());
帧添加(btn);
frame.add(lbl_文件名);
frame.add(lbl_路径);
}
类Action1实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
openFile();
}
}
专用void createDialog(最终帧父对象){
setDefaultCloseOperation(JDialog.DISPOSE\u ON\u CLOSE);
dialog.setModal(true);
setTitle(“加载器”);
URL=this.getClass().getResource(“/resource/loader.gif”);
图标图标=新图像图标(url);
JLabel标签=新的JLabel(图标);
对话框。添加(标签);
dialog.pack();
对话框。setLocationRelativeTo(父级);
}
公共空白显示(布尔可见){
这个。run();
对话框.setVisible(可见);
}
公众假期结束(){
对话框.setVisible(false);
}
私有void setJFCFilter(字符串文件,字符串扩展){
FileNameExtensionFilter=新FileNameExtensionFilter(文件,ext);
jfc.setFileFilter(过滤器);
}
私有void openFile(){
文件默认目录=新文件(“.”);
jfc.setCurrentDirectory(默认目录);
setJFCFilter(“可缩放矢量图形”、“svg”);
int returnVal=jfc.showOpenDialog(parentUI);
if(returnVal==JFileChooser.APPROVE_选项){
最终字符串路径=jfc.getSelectedFile().getAbsolutePath();
字符串文件名=jfc.getSelectedFile().getName();
lbl_filename.setText(文件名);
lbl_path.setText(路径);
System.out.println(“加载文件…”);
load.Show(true);
新SwingWorker(){
@凌驾
受保护的Void doInBackground()引发异常{
createDoc(路径);
返回null;
};
@凌驾
受保护的void done(){
load.Close();
};
}.execute();
System.out.println(“关闭文件…”);
}
}
私有void createDoc(字符串文件){
试一试{
节点列表路径;
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
文档doc=builder.parse(文件);
字符串xpathIDExp=“//g/@id”;
XPathFactory xpf=XPathFactory.newInstance();
XPath=xpf.newXPath();
XPathExpression表达式=xpath.compile(xpathIDExp);
svgIDPaths=(NodeList)expression.evaluate(doc,XPathConstants.NODESET);//Java OutOfMemory
}捕获(例外情况除外){
Logger.getLogger(Loader.class.getName()).log(Level.SEVERE,null,ex);
}
}
}

我知道你做错了什么,--你在调用show,在启动SwingWorker之前显示模式对话框的方法。同样,在显示模式对话框之前,让SwingWorker和所有连接并运行

简化代码(带Thread.sleep)

import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.concurrent.TimeUnit;
导入java.lang.reflect.InvocationTargetException;
导入java.net.URL;
公共类加载器实现可运行{
静态JFrame=新JFrame();
帧父用户界面=新帧();
JDialog dialog=新建JDialog();
JLabel lbl_filename=新的JLabel();
JLabel lbl_path=新JLabel();
静态加载程序加载=新加载程序(null);
公共静态void main(字符串[]args)抛出InterruptedException、InvocationTargetException{
load.run();
frame.setVisible(true);
}
公共加载程序(帧父级){
init();
parentUI=父;
}
@凌驾
公开募捐{
createDialog(parentUI);
}
公共最终void init(){
JButton btn=新JButton(“打开”);
frame.setTitle(“装载机测试”);
框架设置尺寸(500200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(空);
frame.setLayout(新的FlowLayout());
广告
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);
        }
    }
}
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();
        }
    }
}