Simple yes or no program in java swing

Simple yes or no program in java swing


After taking a look at article best book to buy to learn Java , in this article we will see how to write a simple yes or no program in java swing , to create option dialog of with Yes and No we will use the JOptionPane class to show the Yes and No options. Below is the simple yes or no program in java swing, in this program we can see the JOptionPane.showConfirmDialog being used to show the Yes and No options using JOptionPane in java swing. Now we will see what argument it takes and what does it signify.

The first argument to showConfirmDialog method is the parent component, which must be a Frame, null or a component inside a Frame. If we specify a Frame, then the option dialog which we are showing appears on the center and follow the focus of the Frame. Second argument is the question string we want to pass and third argument is the title of the option dialog. The fourth argument is to specify which buttons we want to show on the options dialog.


public class YesOrNoProgram{
JFrame frame;
public static void main(String[] args){
YesOrNoProgram dbShow = new YesOrNoProgram();
}

public YesOrNoProgram(){
frame = new JFrame("Simple Yes or No Options Demo");
JButton button = new JButton("Show YES/NO Options");
button.addActionListener(new DialogYesNoAction());
frame.add(button);
frame.setSize(500, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(  JFrame.EXIT_ON_CLOSE );
}

public class DialogYesNoAction implements ActionListener{
public void actionPerformed( ActionEvent evt ){
int ret = JOptionPane.showConfirmDialog(
frame,
"Do you want to take next step?",
"Confirm Dialog Options",
JOptionPane.YES_NO_OPTION //YES and NO options
);
System.out.println("ret "+ret);
}
}
}

























Customizing the button text in JOptionPane

To achieve customizing button text in JOptionPane we need to use showOptionDialog as shown in the code example below : Here we are passing the array of button text to the showOptionDialog and we can also set which button you want to be set as default button.
public class YesOrNoProgram {

    JFrame frame;

    public static void main(String[] args) {
        YesOrNoProgram dbShow = new YesOrNoProgram();
    }

    public YesOrNoProgram() {
        frame = new JFrame("Simple Yes or No Options Demo");
        JButton button = new JButton("Show YES/NO Options");
        button.addActionListener(new DialogYesNoAction());
        frame.add(button);
        frame.setSize(500, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    }

    public class DialogYesNoAction implements ActionListener {

        public void actionPerformed( ActionEvent evt) {
            Object[] options = {"Yes it is",
                "No at all"};
            int ret = JOptionPane.showOptionDialog(frame,
                    "Is it what/how you want? ",
                    "Question",
                    JOptionPane.YES_NO_OPTION ,
                    JOptionPane.QUESTION_MESSAGE ,
                    null, //Keep null if you dont need custom icon
                    options, //these are titles of buttons
                    options[0]); //default title for button
            System.out.println("ret " + ret);
        }
    }
}













So here in this article we have seen the simple yes or no program in java swing, to see more about JOptionPane check JOptionPane example and JOptionPane tutorial
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

1 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete