Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: showInputDialog method

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default showInputDialog method

    how am i suppose to make a choice for the cancel_option of
    showInputDialog method?


    lets put in this way
    in the showConfirmDialog like this

            confirmation = JOptionPane.showConfirmDialog(null, "You Want To Procced Or Not?", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION);
     
            switch (confirmation) {
     
                case JOptionPane.YES_OPTION:
     
                    JOptionPane.showMessageDialog(null, "YES!");
                    break;
     
                case JOptionPane.NO_OPTION:
     
                    JOptionPane.showMessageDialog(null, "NO!");
                    break;
     
                case JOptionPane.CANCEL_OPTION:
     
                    JOptionPane.showMessageDialog(null, "CANCEL!");
                    break;


    how about in showInputDialog?
            String widthInput = JOptionPane.showInputDialog(null, "Between 100 And 500 Pixels Inclusive: ",
                                                            "Width Of The Shape", JOptionPane.WARNING_MESSAGE);

    i cant make a switch Statement for a String value...
    so in case i press the cancel button
    theres an output that will display..


    everytime i press the cancel button , the compiler shows me this

    [COLOR="Red"]Exception in thread "main" java.lang.NumberFormatException: null
            at java.lang.Integer.parseInt(Integer.java:417)
            at java.lang.Integer.parseInt(Integer.java:499)
            at preDefinedClassesApplications.Ch5DrawShape.inputDimension(Ch5DrawShape.java:216)
            at preDefinedClassesApplications.Ch5DrawShape.getShape(Ch5DrawShape.java:47)
            at preDefinedClassesApplications.Ch5DrawShape.start(Ch5DrawShape.java:34)
            at preDefinedClassesApplications.Ch5DrawShape.main(Ch5DrawShape.java:274)
    [/COLOR]


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: showInputDialog method

    never mind .. i solved it this way..
    i saw a sample of inputDialog in the internet

    and i analyze how does it happen to be fine (not displaying an exception) , when a user press the cancel button

    heres the code that i made from the one that i got in the net... this might help others regarding with inputDialog

        public static void main(String[] args) {
     
            String x = (String) JOptionPane.showInputDialog(null, "Press The Cancel!");
     
            if (x == null) {
     
                System.out.println("Theres No NumberFormatException Anymore!");
            }       
        }
    }


    now my question is about the code that ive got


    heres the code that ive got in the internet about inputDialog

    public class ComboBoxShowInputDialog
    {
      public static final String[] pizzas = { "Cheese", "Pepperoni", "Sausage", "Veggie",
                                              "Keso", "Tuna", "Baboy"};
     
      public static void main(String[] args)
      {
        JFrame frame = new JFrame("Input Dialog Example 3");
     
        String favoritePizza = (String) JOptionPane.showInputDialog(frame, "What is your favorite pizza?", "Favorite Pizza", JOptionPane.QUESTION_MESSAGE, null, pizzas, null);
     
        // favoritePizza will be null if the user clicks Cancel
        System.out.printf("Favorite pizza is %s.\n", favoritePizza);
        System.exit(0);
      }
     
    }

    im using the System class together with .print() .prntln() methods..
    but i didnt knew theres another print method signatured as
     printf()

    whats the purpose of this method? in my opinion.. this one accepts and displays objects.. (thats the only thing i notice about it),
    Last edited by chronoz13; November 22nd, 2009 at 07:07 AM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: showInputDialog method

    printf is a "remenant" that's quite useful from the C/C++ languages (actually, a lot of programming languages has it). It basically stands for "print formatted". You can format how you want the output displayed.

    ex: say you want to display a double with 2 decimal places:

    System.out.printf("%.2f",123.456789);

    The reason it's in these other languages is because they don't automatically concatenate strings together like in Java:

    System.out.print("The answer is " + 123.456789 + " meters");

    printf("The answer is %f meters", 123.456789);

    Java also has a number formatting class if you don't want to use the printf method and want to format it in different ways.