Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:04:19+00:00 2026-05-27T07:04:19+00:00

I know setVisible(false) , dispose() ,but they can’t really close a JDialog. When I

  • 0

I know setVisible(false),dispose(),but they can’t really close a JDialog. When I have the other thread stop, the thread of the JDialog still runs.

And I can’t use System.exit(0),because the other thread needs run for a while.

Following the code, I finally resolve the problem by System.exit(0) at the end of the program.

public class CsUpdateCtrl {

/**
 * 升级service
 */
private CsUpdateService service;

private CsUpdateCtrl() {
    this.service = (CsUpdateService) RmiUtil.getBean(RmiUtil.Service.csupdate);
}

private static final Logger log = Logger.getLogger(CsUpdateCtrl.class.getName());

public static void main(String[] args) throws IOException {

    LogUtil.read();

    log.info("进入自动更新系统");

    CsUpdateCtrl ctrl = null;
    try {
        ctrl = new CsUpdateCtrl();
    } catch (Exception e) {
        SwingUtil.lookAndFeel();
        SwingUtil.message("无法连接服务器!");
        log.info("无法连接服务器:" + e.getMessage());
        return;
    }
    ctrl.start(ctrl.service.version());
}

/**
 * 根据版本号判断是否更新,更新完毕则启动程序
 */
private void start(int version) {
    PropertyIO io = new PropertyIO();
    String oldVersion = io.get(PropertyKey.version);
    String serverAddress = io.get(PropertyKey.serverAddress);
    if (oldVersion == null || !oldVersion.equals(String.valueOf(version))) {// 版本号不相等,下载更新程序
        log.info("下载更新…");
        DownLoad download1 = new DownLoad("http://" + serverAddress + "sdxg-csupdate/sdxg-csclient/sdxg-csclient.jar", "sdxg-csclient\\sdxg-csclient.jar");
        Thread t1 = new Thread(download1);
        DownLoad download2 = new DownLoad("http://" + serverAddress + "sdxg-csupdate/sdxg-csclient/lib/sdxg-web.jar", "sdxg-csclient\\lib\\sdxg-web.jar");
        Thread t2 = new Thread(download2);
        t1.start();
        t2.start();
        SwingUtil.lookAndFeel();
        final DownloadView dialog = new DownloadView(new javax.swing.JFrame(), true);
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                dialog.setVisible(true);
            }
        });
        // 结果判断
        // 若未完成,则不进行下一步
        while(!download1.result.finish() || !download2.result.finish()) {
            // do nothing
        }
        if(download1.result == Result.Complete && download2.result == Result.Complete) {// 更新完成
            // 版本号写入配置文件
            io.write(PropertyKey.version, String.valueOf(version));
            // Nothing is usefull here, include dispose,dispatchEvent and so on
            // 启用应用程序
            log.info("启用应用程序");
            this.runApp();
            System.exit(0);// well, this is usefull
        } else {// 更新失败
            // Nothing is usefull here, include dispose,dispatchEvent and so on
            JOptionPane.showMessageDialog(null, "下载应用程序失败!");
            log.info("下载应用程序失败!");
            System.exit(0);// well, this is usefull
        }
    }else {// 版本号一样
        log.info("启用应用程序");
        this.runApp();
    }
}

/**
 * 运行客户端程序
 */
private void runApp() {
    try {
        Runtime.getRuntime().exec("java -jar " + "sdxg-csclient\\sdxg-csclient.jar");
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "启动应用程序失败!");
        log.info(LogUtil.ex(ex, "启动应用程序失败!"));
    }
}

/**
 * 线程执行的结果
 */
enum Result {
    Complete,
    UnComplete,
    Fail;

    public boolean finish() {
        return this != UnComplete;
    }
}

/**
 * 下载文件
 * @param urlName
 * @param fileName
 * @throws MalformedURLException
 * @throws IOException 
 */
class DownLoad implements Runnable {

    private String urlName;
    private String fileName;

    Result result = Result.UnComplete;

    public DownLoad(String urlName, String fileName) {
        this.urlName = urlName;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        try {
            URL url = new URL(urlName);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream is = connection.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            FileOutputStream fos = new FileOutputStream(fileName);
            int n = -1;
            while ((n = bis.read()) != -1) {
                fos.write(n);
            }
            fos.flush();
            fos.close();
            bis.close();
            is.close();
            result = Result.Complete;
        } catch (Exception ex) {
            result = Result.Fail;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:04:19+00:00Added an answer on May 27, 2026 at 7:04 am

    Send the JDialog a WindowEvent.WINDOW_CLOSING event using dispatchEvent(), as shown here and here.

    Addendum: System.exit() should be used with care, as discussed here and here. In the example below, a second, non-daemon thread completes normally, even if the dialog is closed before its loop exits. See JLS §12.8 Program Exit for details.

    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    
    /**
     * @see https://stackoverflow.com/questions/8336161
     * @see https://stackoverflow.com/questions/6163606
     */
    public class DialogEventTest extends JDialog {
    
        public DialogEventTest() {
            this.setLayout(new GridLayout(0, 1));
            this.add(new JLabel("Dialog event test.", JLabel.CENTER));
            this.add(new JButton(new AbstractAction("Close") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    DialogEventTest.this.setVisible(false);
                    DialogEventTest.this.dispatchEvent(new WindowEvent(
                        DialogEventTest.this, WindowEvent.WINDOW_CLOSING));
                }
            }));
            this.addWindowListener(new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println(e.paramString());
                }
            });
        }
    
        private void display() {
            this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new DialogEventTest().display();
                }
            });
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    System.out.println("Starting…");
                    for (int i = 0; i < 5; i++) {
                        try {
                            Thread.sleep(1000);
                            System.out.println((i + 1) + "s. elapsed.");
                        } catch (InterruptedException e) {
                            e.printStackTrace(System.err);
                        }
                    }
                    System.out.println("Finished.");
                }
            }).start();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know, I would have thought the answer was obviously no as well, but
I'm making a custom ListCellRenderer. I know that you can have different dimensions for
Know this might be rather basic, but I been trying to figure out how
I know that I can do something like $int = (int)99; //(int) has a
I know this might be a no-brainer, but please read on. I also know
How can I in code of the custom Qt widget know that it is
how do i close a frame yet open a new frame? i have a
This discussion started over here but I thought it would be nice to have
I have a problem with a JPanel inside another one. I don't know why,
I have the following java.util.Timer but it does not seem to execute and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.