Creating a custom modal dialog for a Swing applet / application

后端 未结 2 2096
耶瑟儿~
耶瑟儿~ 2021-01-21 22:47

I\'m writing a Swing application that needs to function either as an applet in a browser or as a standalone application, i.e. it may be contained in either a JFrame or a JApplet

2条回答
  •  独厮守ぢ
    2021-01-21 22:57

    Take a look at JDialog. If you set it modal it will run its own event processing to keep the GUI up to date, while capturing mouse and keyboard events for its own use.

    I've looked at the code it uses and it's really not something you want to try to reinvent.

    If you run it non modal, you 'll probably need to add a listener to be called when it finally closes. That is done with addWindowListener and a WindowAdapter that overrides windowClosing.

    As for the owner parameter for the constructor, I use

        Window w = (Window) SwingUtilities.getAncestorOfClass(Window.class, comp);
    

    where comp is some visible component.

    It works because there is always a top level Window, whether running as an applet or application.

提交回复
热议问题