-1

I am slightly confused, I have a jFrame of which I have made in Netbeans. This jFrame has a jLabel, of which is set to setVisible(false); from the beginning. Whenever a specific method is called, I then set the jLabel to setVisible(true); and then use a timer to set it to false again after 2 seconds. Apparently it won't work and I am unable to figure out why. I am aware of the repaint(); method, but can figure out how to make that work either.

I know the actual method for setting the visibility is called, as I have set it to print a line with the current state, which it does.

My actual code is the one below.

public JFram() {
        initComponents();
        setResizable(false);
        jLabel2.setVisible(false);
    }

static void tesMethod() {
            try {
         //function that does something
            } finally {
                new JFram().showHide(); //call function which is supposed to change the vissibility of jLabel
            }
    }

    void showHide() {
            jLabel2.setVisible(true);
            System.out.println("reached show");
            new java.util.Timer().schedule( 
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                     jLabel2.setVisible(false);
                     System.out.println("reached timer");
                    }
                 }, 
             2000 
            );
    }

The code below here is how I tried to use the repaint(); method.

void showHide() {
            jLabel2.setVisible(true);
            jLabel2.repaint();
            System.out.println("reached show");
            new java.util.Timer().schedule( 
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                     jLabel2.setVisible(false);
                     jLabel2.repaint();
                     System.out.println("reached timer");
                    }
                 }, 
             2000 
            );
    }
Sm00rf9000
  • 531
  • 1
  • 7
  • 20
  • Instead of playing with `JLabel` visibility, why not play with its text? (i.e. `JLabel.setText("");`) which will cause a similar effect. – Frakcool Feb 21 '17 at 17:31
  • Also you shouldn't be using a [`java.util.Timer`](https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html) but a [`javax.swing.Timer`](https://docs.oracle.com/javase/8/docs/api/javax/swing/Timer.html) – Frakcool Feb 21 '17 at 17:36
  • @Frakcool I really appreciate the input, I hadn't thought of that. After trying though, the issue is still there and it still won't work. I have changed all the `setVisible` lines to `setText("")` or `setText("Done")`. – Sm00rf9000 Feb 21 '17 at 17:37
  • Please see my answer, I think the problem is related to the problem stated in my 2nd comment – Frakcool Feb 21 '17 at 17:53

2 Answers2

2

I think your problem lies mainly in you using a java.util.Timer instead of a javax.swing.Timer and probably you're blocking the Event Dispatch Thread (EDT).

You could try this code and compare it with yours, I also don't see where you're adding your JLabel to your frame.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ShyLabel {

    private JFrame frame;
    private JLabel label;
    private Timer timer;
    private boolean isVisible;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ShyLabel().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        String labelText = "I'm a shy label that hides every 2 seconds";

        isVisible = true;
        frame = new JFrame(getClass().getSimpleName());
        label = new JLabel(labelText);
        timer = new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(isVisible ? "" : labelText);
                isVisible = !isVisible;
            }
        });

        timer.setInitialDelay(2000);
        timer.start();

        frame.add(label);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

The below image is produced by the above code, however because of the time I recorded the GIF it looks really fast instead of taking 2 seconds as it should be...

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

May be it is a problem of layout. As you set resizable to false before any layout calculation occurred, the label was ignored (as invisible) by the time of the first layout. You could try revalidate().

Laurent G
  • 397
  • 8
  • 16