0

What I need to do is to set a variable when I close a JFrame. I have a thread in my project. I want this thread to run only if a static variable in another class is set to true. This is the class in which I would set the variable (sliderActivated):

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;




public class ZoomSlider extends JFrame implements ChangeListener{

    private static final long serialVersionUID = -9026587156796382276L;
    private JSlider slider;
    private static boolean sliderActivated = false;

    public ZoomSlider(){
        //setSliderActivated(true);

        Container contentPane = getContentPane();
        this.setTitle("Zoom");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setAlwaysOnTop(true);
        Toolkit mioToolkit = Toolkit.getDefaultToolkit();
        Dimension dimensioniSchermo = mioToolkit.getScreenSize();

        int larghezzaFrame, altezzaFrame;
        larghezzaFrame = 80;
        altezzaFrame = (int)(dimensioniSchermo.getHeight() - 70);
        this.setSize(larghezzaFrame, altezzaFrame);

        this.setLocation(0,50);

        slider = new JSlider(JSlider.VERTICAL, 50, 200, 100);
        slider.setMajorTickSpacing(50);  
        slider.setMinorTickSpacing(10);  
        slider.setPaintTicks(true);  
        slider.setPaintLabels(true);  
        slider.addChangeListener(this);  

        contentPane.add(slider);

        this.setVisible(true);

    }

    @Override
    public void stateChanged(ChangeEvent arg0) {
        //setSliderActivated(true);

        int value = ((JSlider)arg0.getSource()).getValue();  
        EarthPanel.SCALE = value/100.0; 

        //setSliderActivated(false);    
    }

    public static boolean isSliderActivated() {
        return sliderActivated;
    }

    public static void setSliderActivated(boolean sliderActivated) {
        ZoomSlider.sliderActivated = sliderActivated;
    }
}

and this is the run method of the thread which uses the variable:

public void run() {     
     while(true){

        if(ZoomSlider.isSliderActivated()){
        this.setPreferredSize(new Dimension((int)(width * SCALE),(int)(height * SCALE)));
            repaint();
        }
    }
}

My idea is to repaint the principal panel (the code is not present in this question) only when the JFrame ZoomSlider.java is visible. In that moment, sliderActivated will be set to true. Instead, when I close the JFrame, sliderActivated will be set to false and the thread will stop running. How can I do this?

Keale
  • 3,924
  • 3
  • 29
  • 46

2 Answers2

0

Basically, you would need to add a WindowListener to the JFrame, which responds to either the closed or closing events:

jFrame.addWindowListener(new WindowAdapter() {
  @Override
  public void windowClosed(WindowEvent e) {
     // Do something
  }

  @Override
  public void windowClosing(WindowEvent e) {
     // Do something
  }
});
BarrySW19
  • 3,759
  • 12
  • 26
  • Hi! Thank you for your answer; I tried to use your method but I had the same problem: windowClosed() method is not invoked when I close the jFrame. Do you know why? – Giordano Agostini Oct 28 '14 at 14:15
  • Hmm, you may need to respond to both windowClosed() and windowClosing(), which one gets called is dependent on how the window is closed (code fragment changed). – BarrySW19 Oct 28 '14 at 14:39
  • Ok, I solved my problem with windowClosing() method of WindowListener interface! Thanks for your help! – Giordano Agostini Oct 28 '14 at 17:04
0

You should add a WindowListener or a WindowStateListener to your frame instance. Example for a WindowStateListener:

public class ZoomSlider extends JFrame implements ChangeListener, WindowStateListener {

    private static volatile boolean sliderActivated = false;

    [...]

    public void windowStateChanged(WindowEvent e) {
        if (e.getNewState() == WindowEvent.WINDOW_CLOSED) {
            setSliderActivated(false);
        } else if (e.getNewState() == WindowEvent.WINDOW_OPENED) {
            setSliderActivated(true);
        }
    }

}

Note that you must declare the field as volatile because two different threads are involved at writing/reading it.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • Thank you for your answer; I tried to use your example (I added this.addWindowStateListener(this) in the constructor of the jFrame), but windowStateChanged() method is invoked only when I minimize or maximize it and not when I close it. Is there something wrong? – Giordano Agostini Oct 28 '14 at 14:09
  • @Giordano Hmmm, maybe then better use `WindowListener`. – Seelenvirtuose Oct 28 '14 at 14:31