
The JOptionPane provides various types of message boxes as follows :
- A simple message box that has one button.
- This type of message box is used only for showing appropriate message and user will end the message box by clicking the ‘Ok’ button.
2. A message box that has two or three buttons. You can set several values for viewing many message box as follows :
- ‘Yes’ and ‘No’
- ‘Yes, ‘No’ and ‘Cancel’
- ‘Ok’, and ‘Cancel’
- showMessageDialog() – It is used to display a simple message.
- showConfirmDialog() – It is used to asks the user for confirmation by displaying message.
- showInputDialog() – It is used to prompt for inputting.
NOTE 1 : You must use import javax.swing.JOptionPane; namespace in order to make the program work.
NOTE 2 : All you have to do is change INFORMATION_MESSAGE with WARNING_MESSAGE and ERROR MESSAGE.
Simple Message Box(showMessageDialog()) :
1. Information Message
Codes :
JOptionPane.showMessageDialog(null, "Hey, It's me Mohamed Shimran", "Ultimate Programming Tutorials", JOptionPane.INFORMATION_MESSAGE); System.exit(0);
Image :
2. Warning Message
Codes :
JOptionPane.showMessageDialog(null, "Hey, It's me Mohamed Shimran", "Ultimate Programming Tutorials", JOptionPane.WARNING_MESSAGE); System.exit(0);
Image :
3. Error Message
Codes :
JOptionPane.showMessageDialog(null, "Hey, It's me Mohamed Shimran", "Ultimate Programming Tutorials", JOptionPane.ERROR_MESSAGE); System.exit(0);
Image :
Confirm Message Box(showConfirmDialog()) :
Codes :
int message = JOptionPane.showConfirmDialog(null, "Hey, It's me Mohamed Shimran", "Ultimate Programming Tutorials", JOptionPane.ERROR_MESSAGE); if (message == JOptionPane.YES_OPTION) { //if yes clicked System.exit(0); } else { System.exit(0); //if no clicked }
Image :
Input Message Box(showInputDialog())
Codes :
//Prompt the user String name = JOptionPane.showInputDialog(null, "Hello?", "Ultimate Programming Tutorials", JOptionPane.WARNING_MESSAGE); System.exit(0);
Image :
That's all :D
1 comments:
core java examples
ReplyPost a Comment
Note: Only a member of this blog may post a comment.