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

Thread: Display in JOptionPane

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Display in JOptionPane

    I have my code done, but i need to display the results with JOptionPane. Is there a way to store the result of the for loops so i can recall it with JOptionPane.showmessage()?
    thanks

     
    import javax.swing.JOptionPane;
     
    public class pyramid {
    	public static void main(String[] args) {
    		String LinesString = JOptionPane.showInputDialog("Enter the number of lines: ");
    		int row = Integer.parseInt(LinesString);
     
    		int spaces = row - 1; 
    		int numPrint = 1; 
     
     
    		for(int x = 0; x < row; x++){  
    		  for(int y = 0; y < spaces; y++){  
    		   System.out.print("   ");}  
    		    for(int i = numPrint; i > 0; i--){ 
    		      System.out.printf("%3d",i);  
    		}  
    		   for(int z = 1; z < numPrint; z++){   
     
    		   System.out.printf("%3d",z + 1);  
     
    		}  
    		numPrint += 1;  
    		spaces -= 1;  
    		System.out.println();
     
    		}
    	}
    }


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    490
    Thanks
    8
    Thanked 46 Times in 44 Posts

    Default Re: Display in JOptionPane

    Your code with its inconsistent formatting is next to impossible to read. Please go through
    Code Conventions for the Java(TM) Programming Language: Contents

    db

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    490
    Thanks
    8
    Thanked 46 Times in 44 Posts

    Default Re: Display in JOptionPane

    You can store each line as an element of a String array and pass the array as the message parameter to JOptionPane.showMessageDialog

    db

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Display in JOptionPane

    Quote Originally Posted by t-rank View Post
    I have my code done, but i need to display the results with JOptionPane. Is there a way to store the result of the for loops so i can recall it with JOptionPane.showmessage()?
    thanks

     
    import javax.swing.JOptionPane;
     
    public class pyramid {
    	public static void main(String[] args) {
    		String LinesString = JOptionPane.showInputDialog("Enter the number of lines: ");
    		int row = Integer.parseInt(LinesString);
     
    		int spaces = row - 1; 
    		int numPrint = 1; 
     
     
    		for(int x = 0; x < row; x++){  
    		  for(int y = 0; y < spaces; y++){  
    		   System.out.print("   ");}  
    		    for(int i = numPrint; i > 0; i--){ 
    		      System.out.printf("%3d",i);  
    		}  
    		   for(int z = 1; z < numPrint; z++){   
     
    		   System.out.printf("%3d",z + 1);  
     
    		}  
    		numPrint += 1;  
    		spaces -= 1;  
    		System.out.println();
     
    		}
    	}
    }
    It depends if you like a JOptionPane for each iteration of the loop or not.

    If you do, I have no clue how to do the %3d with JOptionPane, but you'd

    do this:

    JOptionPane.showMessageDialog(Component parent, Object message, String title, int messageType);

    message Types are JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE; JOptionPane.INFORMATION_MESSAGE, JOptionPane.QUESTION_MESSAGE, and JOptionPane.ERROR_MESSAGE.

    You're probably using JOptionPane.INFORMATION_MESSAGE.

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    490
    Thanks
    8
    Thanked 46 Times in 44 Posts

    Default Re: Display in JOptionPane

    I have no clue how to do the %3d with JOptionPane
    String#format(...)

    db

  6. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    javapenguin (October 19th, 2010)

Similar Threads

  1. Change to JOptionPane
    By Liuric in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 12:07 AM
  2. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM
  3. NullPointerException when canceling JOptionPane.showInputDialog
    By Shaddam in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2010, 02:08 AM
  4. JOptionPane Question/ Printing
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 2
    Last Post: August 31st, 2009, 09:17 AM
  5. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 0
    Last Post: November 28th, 2008, 07:02 PM