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?