1

First of all, thank you for taking the time to help me!

I am trying to print a couple of sentences on a message dialog with using string formatting.

When I use the string format and print it on the console, it prints with the proper format. Like the image below:enter image description here

However, when I try to print it on a Message Dialog, it prints with the wrong format, as show in the image below: enter image description here

This is how I am printing this:

  String result = new String();
  for(int i = 0; i<stocks.length;i++){
     result += stocks[i].toString() + "\n";
  }
       
  NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();

  // Prints Result on Console
  System.out.println(result);
  System.out.println("The total amount is: " + defaultFormat.format(BestStocksMA.getTotalCostMA()));
  

  // Prints Result on Panel
  JOptionPane.showMessageDialog(null, "Your companies and stocks are:\n" + result + "\nThe total amount is: " + defaultFormat.format(BestStocksMA.getTotalCostMA()));

And this is what I am using to format the String:

  public String toString() {

       String description;
  
       description = String.format("%-25s", name) +
                     String.format("%-20s", defaultFormat.format(price)) +
                     String.format("%-20s", defaultFormat.format(calCostMA()));
     
    return description;
    }
 

How can I make it so the String prints in a 'pretty' and organized way on the Message Dialog just like it does when printed on the console?

Thank you so much!

2
  • Use a monospaced font. Notice how the i and the p have different widths with the default font. Commented Dec 3, 2019 at 19:49
  • This did not work Commented Dec 3, 2019 at 21:01

1 Answer 1

1

You can use HTML tags to format your text. Check https://docs.oracle.com/javase/tutorial/uiswing/components/html.html for more details.

I would create an HTML table to display the result. I hope you do not need help to create the rows and columns in your for loop.

Given below is a sample program:

import javax.swing.JOptionPane;

public class Guess_The_Color {
    public static void main(String[] args) {
        StringBuilder sb=new StringBuilder();
        sb.append("<html>");
        sb.append("<table><tr><th>Name</th><th>Class</th><th>Roll No.</th></tr>");
        sb.append("<tr><td>Arvind</td><td>10</td><td>1</td></tr>");
        sb.append("<tr><td>Kumar</td><td>9</td><td>2</td></tr>");
        sb.append("<tr><td>Avinash</td><td>8</td><td>3</td></tr>");
        sb.append("</table>");
        sb.append("</html>");
        JOptionPane.showMessageDialog(null,sb.toString());
    }
}

Output:

enter image description here

Feel free to comment if you need any further help.

Update: Based on the conversation in the comments, I am posting the below code that will work for any number of rows

StringBuilder sb = new StringBuilder();

sb.append("<html>");
sb.append("<b>Your companies and stocks are</b>");
sb.append("<table>");
for (int i = 0; i < stocks.length; i++) {
    sb.append("<tr><td>"+stocks[i].getName()+"</td><td>"+String.valueOf(stocks[i].getPrice())+"</td><td>"+String.valueOf(stocks[i].calCostMA())+"</td></tr>");
}       
sb.append("</table>");
sb.append("</html>");

sb.append("The total amount is: "+String.valueOf(BestStocksMA.getTotalCostMA()));

JOptionPane.showMessageDialog(null, sb.toString());
Sign up to request clarification or add additional context in comments.

2 Comments

This adds so much more code and is not customizable. Sometimes the code will print 1 line, sometimes it prints 10. So this wouldn't work.
Still doesn't work as 'name' has private access in its class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.