Aug 28, 2014

AlertDialog of Android


In android,  we can make  simple dialog using AlertDialog.

I make simple button program for alertdialog test.


This is program's layout for alertdialog test.

Simple!! I make 3 buttons.

First button is one button alertdialog.

Second button is two buttons alertdialog.

Third button is three buttons alertdialog.



 If you click to first button(one button alertdialog test), you can see this dialog.



 If you click to second button(two buttons alertdialog test), you can see this dialog.



 If you click to third button(three buttons alertdialog test), you can see this dialog.

This is JAVA code 1 button.


  1. AlertDialog.Builder ad=new AlertDialog.Builder(this);
  2. ad.setTitle("1 button").setMessage("This is one button dialog.").setNeutralButton("check",new DialogInterface.OnClickListener() {
  3.     public void onClick(DialogInterface dialog, int which) {
  4.         Toast.makeText(getApplicationContext()"Test end!!", Toast.LENGTH_SHORT).show();
  5.         }
  6.     }).create().show();

setTitle is alertdialog's title text.

setMessage is alertdialog's message text.

setNutralButtun is alertdialog's button.

button's method is

setPositiveButton(CharSequence text, OnClickListener listener)
setNegativeButton(CharSequence text, OnClickListener listener)
setNeutralButton(CharSequence text, OnClickListener listener)
CharSequence text is button's name.

OnClickListener listener is alertdialog button's listener
create() is dialog create.

show() is dialog show.

This is 2buttons

  1.             ad.setTitle("2 button").setMessage("This is two button dialog.").setPositiveButton("OK",newDialogInterface.OnClickListener() {
  2.                
  3.                 public void onClick(DialogInterface dialog, int which) {
  4.                     Toast.makeText(getApplicationContext()"You Click to OK!!", Toast.LENGTH_SHORT).show();
  5.                    
  6.                 }
  7.             }).setNegativeButton("NO"new DialogInterface.OnClickListener() {
  8.                
  9.                 @Override
  10.                 public void onClick(DialogInterface dialog, int which) {
  11.                     Toast.makeText(getApplicationContext()"You Click to No!!", Toast.LENGTH_SHORT).show();
  12.                    
  13.                 }
  14.             }).create().show();

And 3buttons

  1.             ad.setTitle("3 button").setMessage("This is three button dialog.").setPositiveButton("YES",newDialogInterface.OnClickListener() {
  2.                
  3.                 public void onClick(DialogInterface dialog, int which) {
  4.                     Toast.makeText(getApplicationContext()"You Click to YES!!", Toast.LENGTH_SHORT).show();
  5.                    
  6.                 }
  7.             }).setNegativeButton("NO"new DialogInterface.OnClickListener() {
  8.                
  9.                 @Override
  10.                 public void onClick(DialogInterface dialog, int which) {
  11.                     Toast.makeText(getApplicationContext()"You Click to No!!", Toast.LENGTH_SHORT).show();
  12.                    
  13.                 }
  14.             }).setNeutralButton("cancel"new DialogInterface.OnClickListener() {
  15.                
  16.                 @Override
  17.                 public void onClick(DialogInterface dialog, int which) {
  18.                     Toast.makeText(getApplicationContext()"You Click to CANCEL!!", Toast.LENGTH_SHORT).show();
  19.                    
  20.                 }
  21.            
  22.             }).create().show();


Aug 22, 2014

SWING JScrollPane (JAVA GUI)





This post about scroll.

We need scrollbar when making chat or communication program.

We can easily add scrollbar using JScrollPane.

Just add JScrollPane and paste to text-window.

My case paste to JEditorPane commText.


And go to source.

  1.         JScrollPane scrollPane = new JScrollPane(commText);
  2.         scrollPane.setBounds(1210471389);
  3.         frame.getContentPane().add(scrollPane);
Look at Line 1.

JScrollPane include JEditorPane commText.

And Line 2 is JScrollPane's size.

The size bigger then JEditorPane's size.

Okay, run to source.

You can see this picture.


You don't see Scrollbar .

If you add text more

You can see Scrollbar.


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'.





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'