Thursday, April 5, 2018

[Swing][Resolved] swing close window on button click

Solution

To close window when button be clicked,

Method 1: using JFrame.dispose()

you can use dispose() method on an instance object of JFrame, it close the window with the button you clicked only. If there is another window in your application running, they won't be closed.
https://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#dispose%28%29
package terrapinssky;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class TerrapinsSky {
    JFrame frame;
    JButton btnSubmit;
    JButton btnClose;
    int x = 0;
    int y = 0;
  
    public static void main(String[] args){
        new TerrapinsSky().initGUI();
    }
   
    public void initGUI(){
        frame = new JFrame("Hello World");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        JPanel panel = new JPanel();
       
        btnSubmit = new JButton("Submit");
        btnSubmit.addActionListener(new SubmitListener());
        panel.add(btnSubmit);
       
        btnClose = new JButton("Close");
        btnClose.addActionListener(new CloseListener());
        panel.add(btnClose);
       

        frame.getContentPane().add(panel);
        frame.setSize(300,100);
        frame.setVisible(true);
    }
   
    class SubmitListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            btnSubmit.setText("Clicked");
        }
    }
   
    class CloseListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    }
   
}

Method 2 : using System.exit(); 

System.exit(); causes the Java VM to terminate completely.  In Swing at usual we use System.exit(0); and we may use System.exit(1) within try-catch exception statements.
package terrapinssky;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class TerrapinsSky {
    JFrame frame;
    JButton btnSubmit;
    JButton btnClose;
    int x = 0;
    int y = 0;
  
    public static void main(String[] args){
        new TerrapinsSky().initGUI();
    }
   
    public void initGUI(){
        frame = new JFrame("Hello World");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        JPanel panel = new JPanel();
       
        btnSubmit = new JButton("Submit");
        btnSubmit.addActionListener(new SubmitListener());
        panel.add(btnSubmit);
       
        btnClose = new JButton("Close");
        btnClose.addActionListener(new CloseListener());
        panel.add(btnClose);
       

        frame.getContentPane().add(panel);
        frame.setSize(300,100);
        frame.setVisible(true);
    }
   
    class SubmitListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            btnSubmit.setText("Clicked");
        }
    }
   
    class CloseListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}

Reference

https://stackoverflow.com/questions/2352727/closing-jframe-with-button-click
https://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#dispose%28%29
https://stackoverflow.com/questions/13360430/jframe-dispose-vs-system-exit

No comments :

Post a Comment