Display array in JOptionPane

家住魔仙堡 提交于 2019-11-29 17:59:42

You could wrap the output in HTML and let Swing render it...

public void show() {
    StringBuilder sb = new StringBuilder(64);
    sb.append("<html><table><tr><td>Item</td><td>Price</td><td>Quantity</td><td></td>Priority</tr>");
    sb.append("<tr>");
    sb.append("<td>").append(itemNames).append("</td>");
    sb.append("<td>").append(itemPrice).append("</td>");
    sb.append("<td>").append(itemQuantity).append("</td>");
    sb.append("<td>").append(itemsPriority).append("</td>");
    sb.append("</tr></table></html>");

    JOptionPane.showMessageDialog(null, sb);
}

Now, I would create a method that was capable of taking one or more ShoppingLists and using a similar method, loop through each shopping list and create a new row from it.

import javax.swing.JOptionPane;

public class ShopList {

    private static String allItems = "";

    public static void main(String[] args) {

        ShoppingList shoppingList = null;
        String enterName = JOptionPane.showInputDialog(null, "Username: ");

        for (int i = 0; i < 2; i++) {
            int index = (int) (25 * Math.random());
            String[] options = { "Apples", "Applesauce", "Cereal", "Exit" };
            String input = (String) JOptionPane.showInputDialog(null,
                    "Select an Item", "Welcome " + enterName + "!",
                    JOptionPane.QUESTION_MESSAGE, null, options, "Apples");

            String itemPrice = JOptionPane.showInputDialog("Enter Price");
            double itemp = Double.parseDouble(itemPrice);

            String[] itemQuantity = { "1", "2", "3", "4", "5" };
            String itemq = (String) JOptionPane.showInputDialog(null,
                    "Enter   Quantity", "Welcome",
                    JOptionPane.QUESTION_MESSAGE, null, itemQuantity, "1");

            String itemsPriority = JOptionPane
                    .showInputDialog("Enter Priority");
            int itempry = Integer.parseInt(itemsPriority);

            shoppingList = new ShoppingList(input, itemp, itemq, itempry);
//          shoppingList.show();

        }

        shoppingList.show();

    }

    public static class ShoppingList {
        String itemNames;
        double itemPrice;
        String itemQuantity;
        int itemsPriority;

        public ShoppingList(String name, double price, String quantity,int priority) {
            itemNames = name;
            itemPrice = price;
            itemQuantity = quantity;
            itemsPriority = priority;
            allItems = allItems + "Name: " + itemNames + " Price: " + itemPrice + " Quantity: " + itemQuantity + " Priority: " + itemsPriority + "\n"; 

        }

        public void setitemNames(String name) {
            itemNames = name;
        }

        public String getitemNames() {
            return itemNames;
        }

        public void setitemPrice(double price) {
            itemPrice = price;
        }

        public double getitemPrice() {
            return itemPrice;
        }

        /*
         * public void setitemQuantity(int quantity) { itemQuantity = quantity;
         * } public int getitemQuantity() { return itemQuantity; }
         */
        public void setitemsPriority(int priority) {
            itemsPriority = priority;
        }

        public int getitemsPriority() {
            return itemsPriority;
        }

        public void show() {
//          System.out.println(itemNames);
//          System.out.println(itemPrice);
//          System.out.println(itemQuantity);
//          System.out.println(itemsPriority);
            JOptionPane.showMessageDialog(null, allItems);
        }
    }
}

Using message Dialog of JOption pane u can show the pattern you want for eg

public void show() {

JOptionPane.showMessageDialog(null, itemNames + "  " + itemPrice + "  " + itemQuantity + "  " + itemsPriority);

}

Try this code:

public static void main(String[] args) 
{
    ShoppingList shoppingList = null;
     StringBuffer output = new StringBuffer() ; 
    String enterName = JOptionPane.showInputDialog(null, "Username: ");


    .
    .
      //  shoppingList = new ShoppingList(input,itemp, itemq,itempry);
        output.append("\""+input + " " + itemp +" " + itemq + " " + itempry+ "\"\n") ;
        System.out.println(output.toString());
}

  JOptionPane.showMessageDialog(null, output.toString());

}

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