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();
}
Added
panel.repaint();
After mycheckMark.setVisible(false)
and it works like a charm.