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 4 of 4

Thread: String Formatting on JOptionPane.showMessageDialog

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String Formatting on JOptionPane.showMessageDialog

    Hello, i want to display a formatted string on dialog box. I formatted the string with using

    String.format("%" + width + "s", str);

    It wotks fine with System.out.println but not working on dialog box. Here is the code and screen shot.

     
    //I use this method to format String
    public static String leftPad(String str, int width) {
            return String.format("%" + width + "s", str);
    }
    public static String rightPad(String str, int width) {
            return String.format("%-" + width + "s", str);
    }

    //Here where i invoke the formatting method
     public String printAddedCourses(){
     
            String str = "";
     
            //String str = Arrays.toString(addedCourses);
            for(int i=0; addedCourses[i] != null; 
     
                str += rightPad(addedCourses[i], 14);
                str += rightPad(Integer.toString(addedCredits[i]), 17);
                str += rightPad(addedCourseNames[i], 64);
                str += addedInstructors[i];
                str += "\n";
     
            }
     
            if( str.equals("")==true)
                return "None\n";
     
            return str;
        }

     
    //In here it displays the dialog message BUT NOT FORMATTED !
    if( check == false){
                JOptionPane.showMessageDialog(frame,
                                "<HTML><FONT color=\"red\">Selected Courses:</FONT></HTML>"
                                +"\n"+printAddedCourses() //IT HAVE TI PRINT HERE
                                +"Total Credits: "+totalCredit
                                +"\n\n<HTML><FONT color=\"red\">Conflicts:</FONT></HTML> \nNone" 
                                , "Analyze", JOptionPane.INFORMATION_MESSAGE);
                System.out.println(printAddedCourses());
            }
            else if (check == true){
                JOptionPane.showMessageDialog(frame,
                                "<HTML><FONT color=\"red\">Selected Courses:"
                                +"\n"+printAddedCourses() //IT HAVE TI PRINT HERE
                                +"Total Credits: "+totalCredit
                                +"\n\n<HTML><FONT color=\"red\">Conflicts:</FONT></HTML> \n"+printConflictedCourses() 
                                , "Analyze", JOptionPane.INFORMATION_MESSAGE);
                System.out.println(printAddedCourses());
            }

    This is from dialog box window, it is printing wrong !
    ss.jpg

    This is the Console show, here it is working correctly !
    ss2.jpg

    So where am i doing wrong ?
    Last edited by Onur; September 23rd, 2011 at 08:03 AM.


  2. #2
    Junior Member Mrc0d3r's Avatar
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: String Formatting on JOptionPane.showMessageDialog

    And where's the definition of method rightPad() ?

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Formatting on JOptionPane.showMessageDialog

    Sorry about that, i forgot it. I added it to first code block.

  4. #4
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: String Formatting on JOptionPane.showMessageDialog

    Could you not use the StringBuffer class to append the into to the string? Then just put the String variable in the dialogbox, instead of calling a method?

    Like this:
    	     String x = new StringBuffer().append("a").append(4).append("c")
                     .toString();
     
    	     JOptionPane.showMessageDialog(null, x);
    Last edited by BuhRock; September 23rd, 2011 at 08:43 AM.

Similar Threads

  1. Gson Formatting
    By techwiz24 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 07:48 PM
  2. Formatting Output
    By mael331 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 14th, 2011, 06:41 PM
  3. Replies: 1
    Last Post: July 16th, 2011, 08:55 AM
  4. showMessageDialog is not showing!
    By jonathan920 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2010, 04:14 AM
  5. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM