Doubt - setDefaultCloseOperation

Asked

Viewed 1,169 times

2

It would be possible to somehow pass 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 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.

  • 1

    What do you intend to do? You can explain better?

  • 1

    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.

  • 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.

  • check out this link : http://www.landofcode.com/java-tutorials/java-events.php check that none of these methods are accurate, more specifically windowIconified

  • @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.

2 answers

4


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");
    }
}
  • All right, I got it. But there was something wrong. Method moveToTray asks it to be "Static" in that I accept the correction in eclipse more when correcting the icone is no longer shown. Nor can I make him find the "frame". Type: frame.this.setVisible(true); he says that it is not possible to locate the variable.'

  • Regarding finding the frame depends on how you created your form, whether the frame is its main class extending from Jframe, so to refer it just do this.setVisible(true), if you have a Jframe object with a reference variable for it, just do frame.setVisible(true), I’d have to see what your case is. About the first error, I could not understand very well what happened, perhaps it would be the case to create a new question, or explain me again here by the comments to see if this time I understand.

  • The "Method" is at the end of the frame. The main is as: public class main extends Jframe {

  • Just then inside the "Method", if it is not static, do this.setVisible(true) or this.setVisible(false), just be aware that you do not create a condition where your form disappears forever, you have to make a way for the user to change his status if he is invisible, from what I understand is this.

  • Thank you for your help and I’m sorry to bother you. Hug!

3

Not like that. 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 by the method WindowEvent#getNewState to obtain the current (minimized, maximized etc) state of the 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);
            }
        });
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.