Aug 21, 2014

JOptionPane showConfirmDialog (JAVA SWING GUI)





This time I posting about showConfirmDialog.

This Dialog similar to showMessageDialog. But this is 'confirm'.

'showConfirmDialog' has 4 options.

Option is...


  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION


I add source code to showMessageDialog test source.

There are new source's layout!!


Left side is test for showMessageDialog.

Right side is test for showConfirmDialog.

Okay, go to source.

  1.     @Override
  2.     public void actionPerformed(ActionEvent e) {
  3.         //showMessageDialog
  4.         if(e.getSource()==btnErrMsg){
  5.             JOptionPaneshowMessageDialog(null,"THIS IS ERROR MESSAGE","ERROR TITLE",JOptionPane.ERROR_MESSAGE);
  6.         }else if(e.getSource()==btnInfoMsg){
  7.             JOptionPane.showMessageDialog(null,"THIS IS INFORMATION MESSAGE","INFORMATION TITLE",JOptionPane.INFORMATION_MESSAGE);
  8.         }else if(e.getSource()==btnWarningMsg){
  9.             JOptionPane.showMessageDialog(null,"THIS IS WARNING MESSAGE","WARNING TITLE",JOptionPane.WARNING_MESSAGE);
  10.         }else if(e.getSource()==btnQuestionMsg){
  11.             JOptionPane.showMessageDialog(null,"THIS IS QUESTION MESSAGE","QUESTION TITLE",JOptionPane.QUESTION_MESSAGE);
  12.         }else if(e.getSource()==btnPlainMsg){
  13.             JOptionPane.showMessageDialog(null,"THIS IS PLAIN MESSAGE","PLAIN TITLE",JOptionPane.PLAIN_MESSAGE);
  14.         }
  15.        
  16.         //showconfirmDialog
  17.         else if(e.getSource()==btnDefaultoption){
  18.             JOptionPane.showConfirmDialog(null,"THIS IS DEFAULT OPTION","DEFAULT TITLE",JOptionPane.DEFAULT_OPTION);
  19.             
  20.         }else if(e.getSource()==btnYesnooption){
  21.             JOptionPane.showConfirmDialog(null,"THIS IS YES_NO OPTION","YES_NO TITLE",JOptionPane.YES_NO_OPTION);
  22.         }else if(e.getSource()==btnYesnocanceloption){
  23.             JOptionPane.showConfirmDialog(null,"THIS IS YES_NO_CANCEL OPTION","YES_NO_CANCEL TITLE",JOptionPane.YES_NO_CANCEL_OPTION);
  24.         }else if(e.getSource()==btnOkcanceloption){
  25.             JOptionPane.showConfirmDialog(null,"THIS IS OK_CANCEL OPTION","OK_CANCEL TITLE",JOptionPane.OK_CANCEL_OPTION);
  26.         }
  27.         //showconfirmDialog
  28.     }
Line 16~27 : adding code for showConfirmDialog.

JOptionPane.showConfirmDialog(Component arg0, Object arg1, String arg2, int arg3)
Component arg0 : location of dialog. So generally null.

Object arg1 : confrim message.

String arg2 : title

int arg3 : showConfirmDialog's option.

Look at Line 18, 21, 23, 25

This is making dialog.

Click to DEFAULT_OPTION



Click to YES_NO_OPTION


Click to YES_NO_CANCEL_OPTION



Click to OK_CANCEL_OPTION

Default icon is QUESTION_MESSAGE.

If you want change the icon, add parameter for icon.

  1. JOptionPane.showConfirmDialog(null,"THIS IS DEFAULT OPTION","DEFAULT TITLE",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);





And showConfirmDialog returned integer value.

You can use this return value for choice situation.

  1. int choose=JOptionPane.showConfirmDialog(null,"THIS IS YES_NO OPTION","YES_NO TITLE",JOptionPane.YES_NO_OPTION);
  2.            
  3. if(choose==JOptionPane.YES_OPTION){
  4.     JOptionPane.showMessageDialog(null,"You are choose YES!!");
  5. }else if(choose==JOptionPane.NO_OPTION){
  6.     JOptionPane.showMessageDialog(null,"You are choose NO!!");
  7. }

This is YES_NO_OPTION.

If you choose 'yes'


If you choose 'no'



No comments:

Post a Comment