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

Thread: Help formatting Dialog box!

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help formatting Dialog box!

    Hey guys! I'm new to java and I'm taking a class for college credit. I've always been interested in programming and I'm finally getting my hands dirty and I'm really enjoying it! I made the mistake however of taking my class online and theres very little help or support as well as almost no explanation of the material. Its pretty much; "Look at this example, understand it without any explanation, now make your own."

    Here is my problem, we are working with Dialog boxes, (JOptionPane) I put my code bellow. I want to know how to put some of the outputs on the same line of the dialog box so that its nicely formatted the way an address actually looks. Right now I have a separate line for address street number, street name, and city, state, and zip. Typically these items are included on the same line as each other and I would like my program to be formatted as such. If anyone can help me get it formatted nice and pretty to woo my teacher I would greatly appreciate it!! Code as follows;


    package Assignment2;
    import javax.swing.JOptionPane;

    public class Assignment2 {
    public static void main(String[] args) {

    //Variables
    String info="", name, address, city, state, item, value;
    double cost;
    int quant, zip, streetnum;
    double tcost, tax, ship, fcost;

    //Data
    name = JOptionPane.showInputDialog(null,"Enter Name","Enter Data", JOptionPane.QUESTION_MESSAGE);
    value = JOptionPane.showInputDialog(null,"Enter Street #","Enter Data", JOptionPane.QUESTION_MESSAGE);
    streetnum = Integer.parseInt(value);
    address = JOptionPane.showInputDialog(null, "Enter Street Name","Enter Data", JOptionPane.QUESTION_MESSAGE);
    city = JOptionPane.showInputDialog(null,"Enter City Name","Enter Data", JOptionPane.QUESTION_MESSAGE);
    state = JOptionPane.showInputDialog(null,"Enter State","Enter Data", JOptionPane.QUESTION_MESSAGE);
    value = JOptionPane.showInputDialog(null,"Enter Zip","Enter Data", JOptionPane.QUESTION_MESSAGE);
    zip = Integer.parseInt(value);
    item = JOptionPane.showInputDialog(null,"Enter Item","Enter Data", JOptionPane.QUESTION_MESSAGE);
    value = JOptionPane.showInputDialog(null,"Enter Price","Enter Data", JOptionPane.QUESTION_MESSAGE);
    cost = Double.parseDouble(value);
    value = JOptionPane.showInputDialog(null,"Enter Quantity","Enter Data", JOptionPane.QUESTION_MESSAGE);
    quant = Integer.parseInt(value);

    //Math time
    tcost = cost * quant;
    tax = tcost * .07;
    ship = tax * .01;
    fcost = tcost + tax + ship;

    //Print Invoice
    info=info+name+"\n";
    info=info+streetnum+"\n";
    info=info+address+"\n";
    info=info+city+"\n";
    info=info+state+"\n";
    info=info+zip+"\n";
    info=info+"Item = "+item+"\n";
    info=info+"Item Price = "+cost+"\n";
    info=info+"Quantity = "+quant+"\n";
    info=info+"Total = "+fcost+"\n";

    JOptionPane.showMessageDialog(null, info,"Ship To:", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    }


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

    Default Re: Help formatting Dialog box!

    put some of the outputs on the same line
    The newline character ("\n") is what puts the next String on the next line. If you want Strings on the same line, don't insert a newline character between them when you build the info String.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help formatting Dialog box!

    Thanks for answering my question so quickly Norm, the explanation of "\n" was - "Make sure you put this at the end of each line" we get a lot of what to do, but no explanation of why or what it is doing.

    I did a little critical thinking and in order to make a space between the different information I did this;

    info=info+streetnum;
    info=info+" "+address+"\n";

    Then I wanted to add an extra line between the address information so I did it like this

    info=info+zip+"\n";
    info=info+"\n";
    info=info+"Item = "+item+"\n";

    Are these "accepted" methods, or is there a more simple or correct way to get the results I'm looking for?

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

    Default Re: Help formatting Dialog box!

    Those ways are ok for what you are doing. It could all be done on one line:
    info=info+zip+"\n\n" +"Item = "+item+"\n";
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help formatting Dialog box!

    Thank you Norm! I'm sure I will have many other questions in the future, I really appreciate your help.

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

    Default Re: Help formatting Dialog box!

    You're welcome. See you next time.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. 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
  2. Default Dialog box changes size
    By anu21g in forum AWT / Java Swing
    Replies: 3
    Last Post: January 2nd, 2012, 05:46 AM
  3. Saving the dialog box data
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 24th, 2011, 02:53 PM
  4. Saving a file using a dialog box
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 05:21 PM
  5. [SOLVED] Help with dialog box
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 5
    Last Post: August 4th, 2009, 11:06 AM