Translate

viernes, 22 de noviembre de 2013

Java: Control Frame closing

Sometimes you need to control the closing of a given frame, for example, the main frame. You would need to do any special task before the closing, to save any king of data before the closing, do not permit the closing of the fame, etc...



Then you need to implement an interface for the given Frame. For windows you have the WindowsListener, which includes controls for the following actions:

  • WindowClosing
  • WindowOpened
  • WindowClosed
  • WindowIconified
  • WindowDeiconified
  • WindowActivated
  • WindowDeactivated.
To control the closing we are going to use the first one, and for this task we need to perform the following steps:

  1. Add implements WindowListener to de definition of the class.
  2. Implements all the methods (mandatory) defined before, even if you are going to use only the first one. You can do it mannually or using the options given by Eclipse and indicated with an error in the name of the class. In this case, put the cursor over the error indicated and a message will appear given the option to create automatically all the methods.
  3. In the constructor for the class add: addWindowListener(this)
  4.  Add the code for the method, in our case WindowClosing
The code wil be as shown



import java.awt.BorderLayout;


public class FMain extends JFrame implements WindowListener{

    private JPanel contentPane;
    private JTextField txtTo;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame frame = new FMain();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public FMain() {
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
      
        txtTo = new JTextField();
        txtTo.setBounds(54, 6, 134, 28);
        contentPane.add(txtTo);
        txtTo.setColumns(10);
      
        JLabel lblNewLabel = new JLabel("To :");
        lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        lblNewLabel.setLabelFor(txtTo);
        lblNewLabel.setBounds(6, 12, 45, 16);
        contentPane.add(lblNewLabel);
      
        JButton btnNewButton = new JButton("Borrar");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                txtTo.setText("");
            }
        });
        btnNewButton.setBounds(188, 7, 97, 29);
        contentPane.add(btnNewButton);
      
        addWindowListener(this);
    }
   
   
    public void windowClosing (WindowEvent e){
         JFrame frame = (JFrame)e.getSource(); //Gets The Form invoking the WindowClosing method
        String botonY = "SI";
        String botonN = "No";
        Object[] options={botonY,botonN};
        int n=JOptionPane.showOptionDialog(frame, "¿Desea cerrar la aplicación?", "Atención",            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                options, botonY);
        System.out.println("Option="+n);
        if (n==JOptionPane.YES_OPTION) //Exit
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        else frame.setVisible(true);
    }


    @Override
    public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub
      
    }

    @Override
    public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub
      
    }

    @Override
    public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub
      
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub
      
    }

    @Override
    public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub
      
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub
      
    }
   

}

No hay comentarios: