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

Thread: Multiple System.out.println lines to Dialog box.

  1. #1
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Multiple System.out.println lines to Dialog box.

    Hey guys.

    I have multiple System.out.println, 7 to be precise. The last 4 are in a if, else if, else statement. How can I put all of this into one dialog box?

    Thanks in advanced.

    Curtis H.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiple System.out.println lines to Dialog box.

    Are you asking how to write some lines of code in a method in a class that extents the JDialog class?
    Otherwise can you explain and post some code showing what you are trying to do.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Multiple System.out.println lines to Dialog box.

    This is the end of my code. It is for some sort of calculator. The only thing I am struggling with is "outputting it to a dialog box instead of having it output at the bottom of the eclipse screen".

     
    		System.out.println("The length of your building (in metres) is: " +length);
    		System.out.println("The width of your building (in metres) is: " +width);
    		System.out.println("The amount of weight you want to bring (kilograms) is: " +stock_coming);
    		System.out.println("The amount of weight you are allowed to have per square metre (kilograms) is: " +stock_allowed);
     
    		System.out.println("\nThe area of your building is: " + area + " metres squared" );
    		System.out.println("The total weight you can have is: " + enough + " kg");
    		System.out.println("The total weight you are trying to deliver is: " +  stock_coming + " kg\n");
     
    	    // if else statements
    		if 			( stock_coming > enough)
    			System.out.println("You would be exceeding your weight allowance!");
     
    		else if 	( stock_coming == enough)
    		    System.out.println("You have exactly the right amount!");
     
    		else 
    		    System.out.println("You have more than enough room!");

    I would like to output this inside a dialog box instead of it just appearing at the bottom of the screen. I can do the first 7 system.out lines, but I have no idea how to put the "if, else if, else" statement into one of these boxes. Just to clarify I would like it all in one dialog box. the JOptionPane one, with the INFORMATION_MESSAGE icon.

    Thanks in advanced

    Curtis H.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Multiple System.out.println lines to Dialog box.

    Have you tried to send the text to a JOptionPane? If so, show us that code. If not, start by thinking that all text currently being sent to the println() methods will instead be sent to a JOptionPane method, and program that. From that thinking and coding experience, you should see that the if/else logic is irrelevant to WHERE or HOW the text is being displayed. It's program logic that decides WHAT is being displayed not HOW or WHERE it is being displayed.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiple System.out.println lines to Dialog box.

    how to put the "if, else if, else"
    When building the String to be displayed in the dialog, the if statements can be used to control what text is added to the String that is being built:
    String theText = "First part";
    if(wantThisP) {
       theText += "thisP";
    }else{
       theText += "thatP";
    }
    At the end of the above, theText will contain the String to be shown in the dialog.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Multiple System.out.println lines to Dialog box.

    Norm's response helped me understand your question better. If you're constructing the String object from parts before sending it to a JOptionPane method for display, I recommend using an instance of StringBuilder in which to construct the message.

  7. #7
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Multiple System.out.println lines to Dialog box.

    I'm struggling to understand how I put the 7 lines of text (systemout) and the other statements in the same one box. Am I wrong to say if I put JOptionPane for each time I've put System.out, won't it just open multiple boxes, instead of one box with all the information?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Multiple System.out.println lines to Dialog box.

    Combine all of the Strings into one (using StringBuilder, if you'd like) and output them to the JOptionPane method once. Now struggle with some code.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    iCurtisIT (November 29th, 2013)

Similar Threads

  1. Java Dialog Box HELP
    By Green Light in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2013, 02:33 AM
  2. Help formatting Dialog box!
    By swirth123 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 24th, 2013, 03:20 PM
  3. Making an input dialog box appear after closing a combo box
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 11th, 2012, 06:22 PM
  4. File open dialog box
    By Padmahasa in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 26th, 2012, 08:39 AM
  5. [SOLVED] Help with dialog box
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 5
    Last Post: August 4th, 2009, 11:06 AM