0

I am trying to generate a list of a inventory in all products grabbed from an Array List.

The issue i am running into is how to create the headings/ titles for the list to categorize each product info grabbed from in a WHILE loop .

I am trying to come up with something like this :
List Format I want

But I am getting the following:
List format I am getting from my code

Want to know if there is a way i can add 1 single line on top for headings for category for each product info without it repeating in the while loop.

Here is the code used to display this list.


String menuChoice = getData.getString("(1)- Locate a single product item \n" + "(2)- Print active database inventory reprot \n" + "(3)- Print deleted products inventory report");
switch(menuChoice) {
    case "1" : 
        // create Product object to call methods from product. 
        itemname = getData.getString("Please enter the product name you wish to view");
        pb.search(itemname);
        if (! pb.inList() )
            JOptionPane.showMessageDialog(null, "no product or no database for product found ");
        else {
            Product po = pb.getProduct();
            String  str = "\tProduct Information\n" + "\n"+ "\n" + "(Product Name): "+ po.getProduct()+"\n" + "(Product Cost): "+  "$" + po.getPrice() +"\n"+ " "  +"(Quantity):" + po.getQuantity();
            display(str , "Product Database" , JOptionPane.INFORMATION_MESSAGE);
        }
        break; // Leave breaks before Case , outside of current case brackets , 
    case "2" :
        // Create Inventory report for active products 
        ArrayList list = pb.getList();
        if (list.isEmpty())
            JOptionPane.showMessageDialog(null, "Product inventory list is empty");
        else {
            JOptionPane.showMessageDialog(null, "Product \t " + "Quantity \t" + " "  +"Price \t" +"Manufacturer\t" + " State" );// still have to add purchase Date field. 
            int i = 0 , length = pb.getSize();
            String s = "";
            while (i < length) {
                Product pr =  (Product)list.get(i);
                s = s + pr.getProduct() + "\t" + pr.getQuantity() +"\t" + " " + pr.getPrice() +"\t"+ pr.getCompany().getName() +"\t" +pr.getCompany().getAddress().getState() + "\n";
                i++;
            }
            display (s ,"   Product   Quantity    Price    Manufacturer    State" , JOptionPane.INFORMATION_MESSAGE);
        }
        break;
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You will want to create and display a JTable and table header without gridlines. – Hovercraft Full Of Eels Sep 04 '22 at 14:46
  • Gotcha ima look in to Jtables now –  Sep 04 '22 at 14:57
  • 1
    Also, for future questions, please consider posting your code as a [mre], code that others can compile, run and experience without having to alter. Please check out the link as this tool can help both you and us. For instance, the switch-case has no relevance to your problem. Instead, it's the product list and its display that matters, and so best to have a small program with imports, main method, a Product class, a `List` and a code attempt to display the List. – Hovercraft Full Of Eels Sep 04 '22 at 15:21

0 Answers0