How do display Multiple Lines in a JOptionPane with ArrayList

泪湿孤枕 提交于 2019-12-13 00:24:37

问题


I'm trying to display 2 Arraylist in the JOptionPane. But I get the following on the dialog box

1,2,3,4
Swimming, Running, Cycling, Basketball

I would like to show the info in the dialog box like the following:

1 Swimming
2 Running
3 Cycling
4 Basketball

Please advise how do I go about? Here are my codes.

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

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

        ArrayList<String> sku = new ArrayList<String> ();
        sku.add("1");
        sku.add("2");
        sku.add("3");
        sku.add("4");


        ArrayList<String> games = new ArrayList<String>();
        games.add("Swimming");
        games.add("Running");
        games.add("Cycling");
        games.add("Basketball");

        for(int i = 0; i<games.size(); i++){

            String everything = sku.toString();
            String everything2 = games.toString();

            JOptionPane.showInputDialog(null, everything +"\n"+ everything2);       
        }

     }

}

回答1:


You can do this very easily. Put those lines in a String as an output and then print that in JOptionPane. After adding your data to arraylist just do the following.

String output = "";
for(int i = 0; i<games.size(); i++){
    String everything = sku.get(i).toString();
    String everything2 = games.get(i).toString();

    output += everything +" "+ everything2 + "\n";       
}
JOptionPane.showMessageDialog(null, output);


来源:https://stackoverflow.com/questions/24414736/how-do-display-multiple-lines-in-a-joptionpane-with-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!