Aug 20, 2014

JOptionPane showMessageDialog (JAVA SWING GUI)


I posting SWING's popup message showMessageDialog of JOptionPane.

JOptionPane has 4-methods


Method NameDescription
showConfirmDialogAsks a confirming question, like yes/no/cancel.
showInputDialogPrompt for some input.
showMessageDialogTell the user about something that has happened.
showOptionDialogThe Grand Unification of the above three.

showMessageDialog is default dialog.

showMessageDialog has 5 options.

  • ERROR_MESSAGE
  • INFORMATION_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE

I making sample source for showMessageDialog test.

 

I made 5 buttons.

This is JAVA source.

  1.     @Override
  2.     public void actionPerformed(ActionEvent e) {
  3.         if(e.getSource()==btnErrMsg){
  4.             JOptionPane.showMessageDialog(null,"THIS IS ERROR MESSAGE","ERROR TITLE",JOptionPane.ERROR_MESSAGE);
  5.         }else if(e.getSource()==btnInfoMsg){
  6.             JOptionPane.showMessageDialog(null,"THIS IS INFORMATION MESSAGE","INFORMATION TITLE",JOptionPane.INFORMATION_MESSAGE);
  7.         }else if(e.getSource()==btnWarningMsg){
  8.             JOptionPane.showMessageDialog(null,"THIS IS WARNING MESSAGE","WARNING TITLE",JOptionPane.WARNING_MESSAGE);
  9.         }else if(e.getSource()==btnQuestionMsg){
  10.             JOptionPane.showMessageDialog(null,"THIS IS QUESTION MESSAGE","QUESTION TITLE",JOptionPane.QUESTION_MESSAGE);
  11.         }else if(e.getSource()==btnPlainMsg){
  12.             JOptionPane.showMessageDialog(null,"THIS IS PLAIN MESSAGE","PLAIN TITLE",JOptionPane.PLAIN_MESSAGE);
  13.         }
  14.        
  15.     }

If you don't know JButton, visit this post How to using JButton of SWING (JAVA)

Line 4,6,8,10,12 is running showMessageDialog.

JOptionPane.showMessageDialog(Component arg0, Object arg1, String arg2, int arg3​)

Component arg0 : location of showMessageDialog.
Object arg1 : Message.
String arg2 : title.
int arg3 : showMessageDialog's option.

Generally Component arg0 is null.

Line 3 : btnErrMsg is ERROR_MESSAGE.

Line 5 : btnInfoMsg is INFORMATION_MESSAGE, 

Line7 : btnWarningMsg is WARNING_MESSAGE,

Line9 : btnQuestionMsg is QUESTION_MESSAGE,

Line11 : btnPlainMsg is PLAINE_MESSAGE.

click to ERROR_MESSAGE.


Click to INFORMATION_MESSAGE.



Click to WARNING_MESSAGE 



Click to QUESTION_MESSAGE 

Click to PLAIN_MESSAGE 




How to different??

Just Icon!!

showMessageDialog's option is Icon.

You can make popup-message according to situation using option.

Also possible to using this form.

JOptionPane.showMessageDialog(Component arg0, Object arg1)

This form's default option is INFORMATION_MESSAGE.

No comments:

Post a Comment