开发者

How should I shut down and restart a Swing JFrame thread

开发者 https://www.devze.com 2023-03-24 14:21 出处:网络
My application\'s main frame starts a JFrame thread: IBM1622GUI cardReadPunchGUI = new IBM1622GUI(); // ins开发者_开发知识库tantiate

My application's main frame starts a JFrame thread:

IBM1622GUI cardReadPunchGUI = new IBM1622GUI(); // ins开发者_开发知识库tantiate
Thread cardReadPunchThread = new Thread(cardReadPunchGUI); // alloc thread
cardReadPunchThread.start(); // call thread's run() method

Later, the main frame needs to "destroy" IBM1622GUI (as part of "taking a peripheral offline"), and still later, to reinstantiate it - presumably using the same procedure as above. What should the main frame do to basically return to the state prior to when the IBM1622GUI was first instantiated?

EDIT - I should probably mention that IBM1622GUI, the JFrame class, is being developed with NetBeans as a "Swing GUI Form", and thus there are certain limitations on what edits I can make to the source code.


Read this

You should use this style to init a JFrame.

IBM1622GUI cardReadPunchGUI = null;
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        cardReadPunchGUI = new IBM1622GUI();
    }
});

When you want to close the JFrame, here is already the question with answers

(Edit by Chap: the linked SO thread is very long and meandering, but it boils down to simply using cardReadPunchGUI.dispose(). This does, in fact, answer my question satisfactorily, and I'd like to give credit for this answer but I would also like for this note to be included.)


It sounds like you want to suspend() and resume() the cardReadPunchThread. Although those methods are deprecated, you can use the approach outlined in Java Thread Primitive Deprecation as a way to control the model. Alternatively, an instance of javax.swing.Timer is slightly easier to stop() and start(), as suggested here. On the view side, setVisible() may work, depending on your design.

0

精彩评论

暂无评论...
验证码 换一张
取 消