Why setVisible doesn't work?

itro picture itro · Jan 25, 2012 · Viewed 12.2k times · Source

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);
        }
    });

Answer

mKorbel picture mKorbel · Jan 25, 2012

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