You should override the windowIconified()
", it is invoked whenever the frame is minimized.
According to the documentation:
Invoked when a window is changed from a normal to a minimized state.
This method belongs to the WindowListener interface, which is the interface that handles events that occur with windows, such as open, close, activate, disable, minimize, and restore.
See the example:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Minimiza extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Minimiza frame = new Minimiza();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Minimiza() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//adicione esse trecho de código à sua classe
addWindowListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
super.windowIconified(e);
metodo(); //chama um método da sua classe
}
});
}
//método apenas de exemplo, pode implementar o que preferir aqui dentro
public void metodo() {
System.out.println("Método foi chamado");
}
}