Java Swing Timer to hide and show components
Yeah if I had more time, I could clean up some logic but the project that I'm working on at school has a few weeks to design the GUI and get the logic working. Hell, I'm just using setVisible to navigate between the frames. I know I should use the cardlayout but I haven't got time to implement it.
I actually wanted for SearchingForYour and Imagelabel to be visible on clicking submit and disappear after 5 seconds and at the same time have MatchFound and ProceedToChat be visible.
Anyway, after playing around with the ActionListeners, I managed to get it working. MatchFound and ProceedToChat has to be in a different ActionListener than the components that I'm hiding.
ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { lblSearchingForYour.setVisible(false); imagelabel.setVisible(false); } }; Timer timer = new Timer(5000, taskPerformer); ActionListener resultDisplay = new ActionListener() { public void actionPerformed(ActionEvent evt) { lblMatchFound.setVisible(true); btnProceedToChat.setVisible(true); } }; Timer displaytimer = new Timer(5000, resultDisplay); btnSubmit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { lblSearchingForYour.setVisible(true); imagelabel.setVisible(true); btnClear.setEnabled(false); btnSubmit.setEnabled(false); btnUploadPicture.setEnabled(false); timer.start(); displaytimer.start(); } });
It was putting the timer.start() calls in the button submit action listener that has fixed it for you. Before, you were starting the timer at the end of your constructor. Hence, 5 seconds after starting the application the timer fired and they showed up. There wasn't really a need to split them up into two separate timers.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now