Doubt - setDefaultCloseOperation

2

Would it be possible to somehow move to:

setDefaultCloseOperation(UM METHODO?);

Or using some other option to do the same procedure?

What happens is that I created a button and it calls a "method" to hide the program. I need to click on the minimize method call the same method of the button to hide the program next to the clock.

    
asked by anonymous 30.03.2015 / 14:12

2 answers

4

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");
    }
}
    
30.03.2015 / 15:40
3

Not that way. setDefaultCloseOperation receives an integer corresponding to the action that must be taken, for example, to close the frame JFrame#EXIT_ON_CLOSE :

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// é a mesma coisa que:
frame.setDefaultCloseOperation(3);

You can add a WindowStateListener and through of the WindowEvent#getNewState method get the current (minimized, maximized etc) status of JFrame .

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;


public class MeuJFrame extends JFrame {

    public MeuJFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Adiciona o listener
        this.addWindowStateListener(new WindowAdapter() {
            // Quando houver uma mudança no estado da janela (lê-se seu JFrame)
            @Override
            public void windowStateChanged(WindowEvent e) {
                // Se o estado atual for minimizado, faz algo...
                if(Frame.ICONIFIED == e.getNewState()){
                    System.out.println("Chamando o método...");
                }
            } 
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MeuJFrame().setVisible(true);
            }
        });
    }
}
    
30.03.2015 / 15:46