Pages

Friday, November 02, 2012

Menggunakan Confirm Dialog Box pada Java

java image

package confirmdialogdemo;

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

public class ConfirmDialogDemo extends JFrame implements ActionListener {

    ConfirmDialogDemo(){
        super("Confirm Dialog");
        setSize(250, 100);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        JPanel pane = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton button = new JButton("Exit");
        button.addActionListener(this);
        pane.add(button);
        add(pane);
        setVisible(true);
    }

    public static void main(String[] args) {
        new ConfirmDialogDemo();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?", 
                "Confirm Exit", 
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if(confirm == 0){
            this.dispose();
            System.exit(1);
        }
    }
}

1 comment:

Related Posts Plugin for WordPress, Blogger...