Mark Santos
Mark Santos

Reputation: 108

String formatting doesn't work on Message Dialog - Java

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!

Upvotes: 1

Views: 370

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79115

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());

Upvotes: 1

Related Questions