Total Pageviews

Thursday, June 28, 2012

Creating Message Boxes In Java - Example

I have found it is surprising simple to create a Visual Basic style message box in Java. Below you will find a simplified Java method I coded, to create Message Boxes. Every thing should be self explanatory ( I hope ).
 // Import   
 import javax.swing.JOptionPane;  
   
 public class Message_Box {  
      public static void main(String[] args){  
           // Here is the call to the method  
            Popup("This is the title part","This is the messsage part",0);  
           // Garbage Collection  
           System.exit (0);  
             
      }  
      public static void Popup(String Title, String Message, Integer Type){  
           switch (Type){  
             // Plain Box  
             case 0:  
                  JOptionPane.showMessageDialog(null,Message,Title,JOptionPane.PLAIN_MESSAGE);  
                  break;  
             // Warning Box  
             case 1:  
                  JOptionPane.showMessageDialog(null,Message,Title,JOptionPane.WARNING_MESSAGE);  
                  break;  
             // Error Box  
             case 2:  
                  JOptionPane.showMessageDialog(null,Message,Title,JOptionPane.ERROR_MESSAGE);  
                  break;  
             // Question Box  
             case 3:  
                  JOptionPane.showMessageDialog(null,Message,Title,JOptionPane.QUESTION_MESSAGE);  
                  break;  
             // Blank Default Box  
             case 4:  
                  JOptionPane.showMessageDialog(null,Message);  
                  break;  
             // Bad Number  
             default:  
                  JOptionPane.showMessageDialog(null,"Number out of range: " + Type,"Error",JOptionPane.ERROR_MESSAGE);  
                  break;  
             }}}  
 //User Input ( unused )  
 //String Input = JOptionPane.showInputDialog ( "Enter Something Here" );  
   

No comments:

Post a Comment