Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispose() doesn't work on every frame

My program will alert users when data (stock name & price) in the database matches with the data (stock name and price) from Yahoo Finance. With the help of HarryJoy i'm able to implement a pop up notification.

The problem now is, all the functions only works on the last frame (YHOO). It will not dispose() after 5 seconds, or when I click the closeButton. Thanks!

PopUp

    if (stockPriceDB == popStockValue)
    {                       

        String header = "Stock: "  + stock.getTicker() + " is now @ " +  stock.getPrice();          
        String message = "";

        popUpFrame = new JFrame();
        popUpFrame.setSize(320,90); 
        popUpFrame.setUndecorated(true);                                    
        popUpFrame.getContentPane().setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.weightx = 1.0f;
        constraints.weighty = 1.0f;
        constraints.insets = new Insets(5, 5, 5, 5);
        constraints.fill = GridBagConstraints.BOTH;
        JLabel headingLabel = new JLabel(header);

        ImageIcon headingIcon = new ImageIcon("images/alert.gif");          
        headingLabel.setIcon(headingIcon);          

        popUpFrame.getContentPane().add(headingLabel, constraints);
        constraints.gridx++;
        constraints.weightx = 0f;
        constraints.weighty = 0f;
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.NORTH;                          

        closeButton = new JButton(); 

        closeButton = new JButton(new AbstractAction("x") 
        {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(final ActionEvent e) 
            {
                popUpFrame.dispose();
            }
        });

        closeButton.setMargin(new Insets(1, 4, 1, 4));
        closeButton.setFocusable(false);
        popUpFrame.getContentPane().add(closeButton, constraints);
        constraints.gridx = 0;
        constraints.gridy++;
        constraints.weightx = 1.0f;
        constraints.weighty = 1.0f;
        constraints.insets = new Insets(5, 5, 5, 5);
        constraints.fill = GridBagConstraints.BOTH;                 

        JLabel messageLabel = new JLabel(message);  
        popUpFrame.getContentPane().add(messageLabel, constraints);
        popUpFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        popUpFrame.setVisible(true);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(popUpFrame.getGraphicsConfiguration());
        popUpFrame.setLocation(screenSize.width - popUpFrame.getWidth(), screenSize.height - toolHeight.bottom - (popUpFrame.getHeight() * (x+1)));

        new Thread()
        {                           
        public void run() 
            {
                try 
                {
                    Thread.sleep(5000); 
                    popUpFrame.dispose(); 
                } 
                catch (InterruptedException e) 
                {
                    e.printStackTrace();
                }
            };

        }.start();
    }
}
like image 941
Lily S Avatar asked Jan 16 '23 17:01

Lily S


2 Answers

I suspect that the problem you are getting is that you are passing, to an external code (a different thread in this case), a variable of popUpFrame which you then assign new object every instance of the loop.

This approach is susceptible to errors as you are loosing the reference you passed. Because you are overwriting it every time you create a new object. Therefore, I think you might be only able to close to latest one.

To avoid something like this a variable you pass to an external code should always be final or when instantiating an external process store a reference to it inside the external code.

like image 126
Boro Avatar answered Jan 19 '23 08:01

Boro


As noticed by Boro, you are reusing the same popupFrame variable for all your frames. Of course it can only store a single frame, the last one you created. All the others are lost. So when you call popupFrame.dispose(), you actually dispose the last created JFrame independently of the "X" button you pressed

But I think that the choice of making so many frames is actually not a good idea. You should rather have a single JFrame which contains a set of JPanel's which you will remove after 5 seconds or when the 'X' button is pressed.

like image 39
Guillaume Polet Avatar answered Jan 19 '23 06:01

Guillaume Polet