Recover JFrame after using dispose with static method in nested static class

I have an open AppHelper class to display some help content using jframe. On the same JFrame there is an exit button that clicks on jframe. The ActionListener is implemented as a static nested class of the class mentioned above.

Also, all components of the help window are defined in the external class, and all of them are private and static. Also, the method that displays the help window is static.

Here is the code that I executed:

public class AppHelper { // helper frame private static JFrame appHelperFrame; // helper panel private static JPanel appHelperPanel; // helper pane private static JEditorPane appHelperPane; // exit helper button private static JButton exitAppHelperButton; // constraints private static GridBagConstraints appHelperPaneCons, exitAppHelperButtonCons; /** set layout */ private static void setLayoutConstraints () { // defines layout } /** * initialize the helper elements * @param void * @return void */ public static void initializeElements () { // initialize constraints setLayoutConstraints(); // handler AppHelper.AppHelperHandler appHelpHandler = new AppHelper.AppHelperHandler(); appHelperFrame = new JFrame("App Help"); appHelperPanel = new JPanel(); appHelperPanel.setLayout(new GridBagLayout()); appHelperPane = new JEditorPane(); exitAppHelperButton = new JButton("Exit"); exitAppHelperButton.addActionListener(appHelpHandler); java.net.URL helpURL = null; try { helpURL = new File("AppHelp.html").toURI().toURL(); } catch (MalformedURLException ex) { Logger.getLogger(AppHelper.class.getName()).log(Level.SEVERE, null, ex); } try { appHelperPane.setPage(helpURL); } catch (IOException ex) { Logger.getLogger(AppHelper.class.getName()).log(Level.SEVERE, null, ex); } appHelperPane.setEditable(false); appHelperFrame.add(appHelperPanel); appHelperPanel.add(appHelperPane, appHelperPaneCons); appHelperPanel.add(exitAppHelperButton, exitAppHelperButtonCons); appHelperFrame.setSize(350, 400); appHelperFrame.setResizable(false); appHelperFrame.setVisible(true); } /** * TODO */ public static void showAboutApp() { //throw new UnsupportedOperationException("Not yet implemented"); } /** * * Acts as the handler for the help window components * Implement actionListener interface. */ private static class AppHelperHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if(source == exitAppHelperButton) { appHelperFrame.dispose(); } } } } 

The reason for placing a JFrame instead of its invisibility is because I do not want this JFrame to consume memory when this JFrame is not in use.

Now the problem is , when I click the help button (in some other window) the JFrame is displayed. Now, when I click the exit button in this help window, the JFrame is deleted by the handler. The next time I click the help button again, the help window does not appear. I wanted to know if there is any error in my code or if I need to do something else.

+4
source share
1 answer

javadoc Window.dispose () states that

The window and its subcomponents can be displayed again, restoring their own resources, followed by a call for packaging or display.

And it works too, I tried. Just call appHelperFrame.setVisible(true) and thatโ€™s it. If the window is not activated, try calling appHelperFrame.setState(Frame.NORMAL) , which activates it.

You only need to call your initializeElements method once. Your showAboutApp() method should look something like this:

 public static void showAboutApp() { if (appHelperFrame == null) initializeElements(); // This also makes the frame visible else { appHelperFrame.setVisible(true); appHelperFrame.setState(Frame.NORMAL); } } 

Final Note:

If you always call this showAboutApp() from the EDT (Event Dispatching Thread), then you are kind. If you call this from multiple threads, you can execute it in EDT using SwingUtilities.invokeAndwait() or SwingUtilities.invokeLater() , which also provides synchronization between multiple threads.

0
source

Source: https://habr.com/ru/post/1446879/


All Articles