-1

First of all I found some examples that work the way I want with a standard array how ever I need to use an ArrayList as the size is not limited. And the contents are add via different methods.

My problem is tring to get the Arraylist contents to display and on its own line. With my code bellow only the last content is display.

NOTE: This is not the actual application it is only a test program their for I have a filler method.

PS, Any assitance will be greatly apreceated. I have reseched this For the last 2 hours.

Here is the what I have done so far.

 import javax.swing.*;
  import java.util.ArrayList;
  import java.util.*;

   public class fe
  {
    private static ArrayList<String> errorCache = new ArrayList<String>();
    private static String details;

   public fe()
    {
    }

   public static void main(String[] args)
   {
    fillList(); 
    showMessage();
   }

  private static void showMessage()
   {
    for (int i=0;i<errorCache.size();i++)
    {
        details =  errorCache.get(i);
    }
    JOptionPane.showMessageDialog(null, details, "Printing results",  JOptionPane.INFORMATION_MESSAGE);
  }

  private static void fillList()
  {
    errorCache.add("this");
    errorCache.add("should");
    errorCache.add("bee");
    errorCache.add("on");
    errorCache.add("its");
    errorCache.add("own");
    errorCache.add("line");
  }
}

I got this to work thats to all that help I spent ages trying work this out. the end code it the result for anyone else that has problems with this.

 import javax.swing.*;
 import java.util.ArrayList;
 import java.util.*;

   public class fe
    {
     private static ArrayList<String> errorCache = new ArrayList<String>();

   public fe()
    {
    }

   public static void main(String[] args)
    {
      fillList(); 
      showMessage();
    }

   private static void showMessage()
   {
      String details = "";
      for (int i=0;i<errorCache.size();i++)
    {
        details +=  errorCache.get(i) + "\n";
    }
    JOptionPane.showMessageDialog(null, details, "Printing results", JOptionPane.INFORMATION_MESSAGE);

    //Remimues way.
    //StringBuilder builder = new StringBuilder("<html>"); 
    //for (int i = 0; i < errorCache.size(); i++) {
    //builder.append(errorCache.get(i));
    //builder.append("<br>");
    // }
     //builder.append("</html>");
    //JOptionPane.showMessageDialog
     //(null, builder.toString(), "Printing results", JOptionPane.INFORMATION_MESSAGE);

     }

    private static void fillList()
    {
        errorCache.add("this");
        errorCache.add("should");
        errorCache.add("bee");
        errorCache.add("on");
        errorCache.add("its");
        errorCache.add("own");
        errorCache.add("line");
     }
  }
4

2 回答 2

1

您可以结合使用 HTML 进行格式化和StringBuilder性能:

StringBuilder builder = new StringBuilder("<html>"); 
for (int i = 0; i < errorCache.size(); i++) {
    builder.append(errorCache.get(i));
    builder.append("<br>");
}
builder.append("</html>");
JOptionPane.showMessageDialog
    (null, builder.toString(), "Printing results", JOptionPane.INFORMATION_MESSAGE);
于 2013-06-07T12:40:17.180 回答
0

你的意思是这样的吗?

  private static void showMessage(){
      String details = "";

      for( int i=0; i < errorCache.size(); i++ ){
          details +=  errorCache.get(i) + " ";
      }

      JOptionPane.showMessageDialog( null, details, "Printing results", JOptionPane.INFORMATION_MESSAGE );
  }
于 2013-06-07T12:39:21.850 回答