Why setVisible doesn't work?
-
19-04-2021 - |
Question
I have a swing GUI with border layout. in the NORTH
I have added some component.
My label component which has GIF icon is invisible lblBusy.setVisible(false);
later a button make it visible like below. Why it does not show up?
btnDownload.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
lblBusy.setVisible(true);
btnCancel.setEnabled(true);
}
});
download = new Download(txtSource.getText(), new File(txtDestination.getText()), textAreaStatus);
download.start();
lblBusy.setVisible(false);
}
});
Solution
1) this is EventDispatchThread
rellated issue, EDT quite guaranteed that all changes to the GUI would be done on one moment
2) you invoked ActionPerformed
from JButton
, and untill all events ended your GUI should be freeze or is unresponsible, same for JButton
and JLabel
in your case
3) better would be redirect reading for File
contents to the Backgroung task e.g. SwingWorker
or Runnable#Thread
then JButton
and JLabel
will be changed and GUI would be during Background task responsible for Mouse or KeyBoard
or
4) dirty hack split to the two separated Action
delayed by javax.swing.Timer
, but in this case again untill all events ended your GUI will be freeze or is unresponsible
OTHER TIPS
Most probably because the GUI was packed at a time the label was not visible, so no space was assigned to display it. For anything more definite, post an SSCCE.
It seems to me that you are writing lblBusy.setVisible(true);
and after that lblBusy.setVisible(false);
in the mouseClicked() method. Since you wanted to make it visible at the click of a button aren't you be using only lblBusy.setVisible(true);
, instead of using both.
You can call lblBusy.setVisible(false);
from the end of your Download Class though, once it's done doing what it does.
Regards