Aug 21, 2014

JOptionPane showInputDialog, showOptionDialog (JAVA SWING GUI)







This post about showInputDialog and showOptionDialog of JOptionPane class.

This method is simple than previous posting method.

I added button for test(showInputDialog and showOptionDialog).




showInputDialog test 2-type.

First button is just input dialog.

Second is choice dialog.

showOptionDialog is similar to showConfirmDialog. 

But showoptionDialog is choice the option.

We change the option value.

Okay go to source code.

If you don't understand JButton, visit my post JButton.

This is first showInputDialog source.

  1.         //showInputDialog
  2.         else if(e.getSource()==btnShowinputdialog){
  3.             String text=JOptionPane.showInputDialog(null,"What's your hobby?","hobby",JOptionPane.INFORMATION_MESSAGE);
  4.             JOptionPane.showMessageDialog(null,"Your hobby is "+text);
  5.         }
Line 3 : simple showInputDialog.

Method is 

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

Component arg0 : location. generally null.

Object arg1 : showInputDialog's Message.

String arg2 : showInputDialog's Title.

int arg3 : showInputDialog's Icon.

And showInputDialog's return value is String.

So, I using this return value.

Line 4: Display showInputDialog's return value.

Click to showInputDialog



I write 'basketball' and click to 'Yes'.




This second showInputDialog's source


  1.         }else if(e.getSource()==btnShowmultiinputdialog){
  2.             Object[] choiceOne={"SAMSUNG-GALAXY","APPLE-IPhone","LG-G","Pantech-VEGA"};
  3.             Object choose=JOptionPane.showInputDialog(null,"what is best cell phone?" ,"Best phone",JOptionPane.QUESTION_MESSAGE,null,choiceOne,choiceOne[0]);
  4.             JOptionPane.showMessageDialog(null,"Your best cell phone is "+choose);
  5.         }

Second showinputDialog's Method is
JOptionPane.showInputDialog(Component arg0, Object arg1, String arg2, int arg3, Icon arg4, Object[] arg5, Object arg6)
Component arg0 : Location dialog. Generally null.

Object arg1 : showInputDialog's Message.

String arg2 : showInputDialog's Title.

int arg3 : showInputDialog's icon.

Icon arg4 : maybe null.

Object[] arg5 : Spinner's item.

Object arg6 : Default show item.

Click to showMultiInputDialog.



Line 2 : My item is 'SAMSUNG-GALAXY', APPLE-IPhone', 'LG-G', 'Pantech-VEGA'.

Line 3: I choose default item SAMSUNG-GALAXY. So. default show 'SAMSUNG-GALAXY'.

I choose APPLE-IPhone and click to 'Yes'.



This is showOptionDialog's source code.


  1.         //showOptionDialog
  2.         else if(e.getSource()==btnShowoptiondialog){
  3.             Object[] option={"IPhone5S","Galaxy Note3","G3"};
  4.             int response=JOptionPane.showOptionDialog(null, "What's your phone?","Select Phone",JOptionPane.DEFAULT_OPTION ,JOptionPane.QUESTION_MESSAGE, null, option, option[0]);
  5.             if(response==0){
  6.                 JOptionPane.showMessageDialog(null,"Your phone's company is APPLE");
  7.             }else if(response==1){
  8.                 JOptionPane.showMessageDialog(null,"Your phone's company is SAMSUNG");
  9.             }else if(response==2){
  10.                 JOptionPane.showMessageDialog(null,"Your phone's company is LG");
  11.             }
  12.         }
  13.         //showOptionDialog
showOptionDialog Method is


JOptionPane.showOptionDialog(Component arg0, Object arg1, String arg2, int arg3, int arg4, Icon arg5, Object[] arg6, Object arg7)
Component arg0 : location. Generally null.

Object arg1 : showOptionDialog's message.

String arg2 : showOptionDialog's title.

int arg3 : message option. Generally JOptionPane.DEFAULT_OPTION.

int arg4 : showOptionDialog's icon.

Icon arg5 : maybe null.

Object[] arg6 : option items.

Object arg7 : default selected item.

And showOptionDialog's return value is integer type.

Click to showOptionDialog.



Line 3 : I made items 'IPhone5S', 'Galaxy Note3', 'G3'.

Line 4 : I select default item 'IPhone5S'. show Dialog.

And I click to 'Galaxy Note3'.





No comments:

Post a Comment