Question

I have an application which after a change is made a green check mark appears indicating success of the change. The application has several possible changes which could be made and I would like to be able to have the check mark disappear after 2.5 seconds. I have tried several things like:

panel.add(checkMark);
checkMark.setVisible(true);
panel.remove(checkMark);
checkMark.setVisible(false);

Nothing seems to be working. I added a timer call followed by a checkMark.setVisible(false) and nothing seems to be helping.

Could someone please point out what I am doing incorrectly? Below is my code:

//Create Change Role Button
    final JButton changeRoleBtn = new JButton("Change Role");
    changeRoleBtn.setBounds(50, 500, 150, 30);
    changeRoleBtn.setToolTipText("Changes the role of the User");
    changeRoleBtn.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //Create Success Image
            final ImageIcon i1 = new ImageIcon("/Users/vhaislsalisc/Documents/workspace/Role_Switcher/greenCheck.png");
            final JLabel checkMark = new JLabel(i1);
            checkMark.isOptimizedDrawingEnabled();
            i1.paintIcon(changeRoleBtn, getGraphics(), 400,25); 
            checkMark.setVisible(true);
            try
            {
                timer = new Timer(2000, new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        checkMark.setVisible(false);
                        timer.stop();

                    }
                });
                timer.start();

            }
            catch(Exception e5)
            {
                e5.printStackTrace();
                timer.stop();
            }
        }

    });

Here is the bit about the timer. The other code is relevant, as it includes my declaration for the graphic and how it is being called and used.

try
            {
                timer = new Timer(2000, new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        checkMark.setVisible(false);
                        timer.stop();

                    }
                });
                timer.start();

            }
            catch(Exception e5)
            {
                e5.printStackTrace();
                timer.stop();
            }
Was it helpful?

Solution 2

Added panel.repaint(); After my checkMark.setVisible(false) and it works like a charm.

try
            {
                timer = new Timer(1000, new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        checkMark.setVisible(false);
                        panel.repaint();
                        timer.stop();

                    }
                });
                timer.start();

            }
            catch(Exception e5)
            {
                e5.printStackTrace();
                timer.stop();
            }

OTHER TIPS

panel.add(checkMark);
checkMark.setVisible(true);
panel.remove(checkMark);
checkMark.setVisible(false);

When you add/remove components from a visible GUI the basic code is:

panel.add(...);
panel.revalidate();
panel.repaint();

By default all components have a zero size, so there is nothing to paint until you do the revalidate() which invokes the layout managers to give the components a size.

So you would use code like above to display the component, then you would start your Timer and when the timer fires you would remove it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top