You must override the method 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 occurring with windows, such as opening, closing, enabling, disabling, minimizing and restoring.
Take 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");
}
}
What do you intend to do? You can explain better?
– jsantos1991
Also I could not understand what you need, if you could try to elaborate better your question would be good. Anyway take a look at this question that uses the same method you are quoting: Confirm form closure and see if it helps in any way.
– Math
Sure I’ll be clearer. I created 1 button and it calls a "method" to hide the program. I am needing that when clicking on the minimize it calls the "method" the same method as the button to hide the program next to the clock.
– kholyphoenix1
check out this link : http://www.landofcode.com/java-tutorials/java-events.php check that none of these methods are accurate, more specifically
windowIconified
– jsantos1991
@kholyphoenix1 transpus your comment to the question body, to make it easier for you to read. Feel free to [Dit] and change whatever you think is necessary.
– Math