user1870921
user1870921

Reputation: 21

Adding data from a GUI in Java

So I am trying to create a retail GUI. An employee chooses the department, enters the item name, the price, and the discount. He should be able to enter data for multiple items and then should be able to hit print and a Jtable should appear. The issue I am having is writing the code to be able to update the arraylist for each item he enters. I seem to overwrite position[0] each time and it does not store the new item. Also I have no idea where to begin with the Jtable. I am fairly new to Java so ANY help you give is greatly appreciated.

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.util.*;


  public class RetailGUI extends JFrame
   {

     // The following variables will reference the
      // custom panel objects.
      private DepartmentPanel department;     
      private ItemPanel item; 
      private PricePanel price;    
      private TitlePanel title;  // To display a greeting

      // The following variables will reference objects
      // needed to add Next and Exit buttons.
      private JPanel buttonPanel;    // To hold the buttons
      private JButton nextButton,
                      printButton,
                      exitButton;    // To exit the application


      /**
      * Constructor
       */

      public RetailGUI()
      {
        // Display a title.
         super("Retail Calculator");

         setSize(600, 250);

         // Specify an action for the close button.
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setLocationRelativeTo(null);

         // Create a BorderLayout manager for
         // the content pane.
         setLayout(new BorderLayout());

        // Create the custom panels.
         title = new TitlePanel();
         department = new DepartmentPanel();
         item = new ItemPanel();
         price = new PricePanel();

         // Call the buildButtonPanel method to
         // create the button panel.
         buildButtonPanel();

        // Add the components to the content pane.
         add(title, BorderLayout.NORTH);
         add(department, BorderLayout.WEST);
         add(item, BorderLayout.CENTER);
         add(price, BorderLayout.EAST);
         add(buttonPanel, BorderLayout.SOUTH);

         // Pack the contents of the window and display it.
         //pack();
         setVisible(true);
     }

      /**
       * The buildButtonPanel method builds the button panel.
       */

      private void buildButtonPanel()
      {
         // Create a panel for the buttons.
         buttonPanel = new JPanel();

         // Create the buttons.
         nextButton = new JButton("Next");
         printButton = new JButton("Print");
         exitButton = new JButton("Exit");

        // Register the action listeners.
         nextButton.addActionListener(new RetailGUI.NextButtonListener());
         printButton.addActionListener(new RetailGUI.PrintButtonListener());
         exitButton.addActionListener(new RetailGUI.ExitButtonListener());

         // Add the buttons to the button panel.
         buttonPanel.add(nextButton);
         buttonPanel.add(printButton);
         buttonPanel.add(exitButton);
      }

      /**
      * Private inner class that handles the event when
       * the user clicks the Next button.
       */     

      private class NextButtonListener implements ActionListener
     {
         public void actionPerformed(ActionEvent e)
         {

             String departmentName,
                    finalItem;
             float    priceField,
                      discountField,
                      newPrice;

          ArrayList   DepartmentList            = new ArrayList();
          ArrayList   ItemList                  = new ArrayList();
          ArrayList   PriceList                 = new ArrayList();
          ArrayList   DiscountList              = new ArrayList();
          ArrayList   NewPriceList              = new ArrayList();




          departmentName = department.getDepartmentName();
          finalItem = item.getItemName();
          priceField = price.getPriceField();
          discountField = price.getDiscountField();
          newPrice = priceField - (priceField * (discountField/100));

          DepartmentList.add(departmentName);



          System.out.print(DepartmentList);


           JOptionPane.showMessageDialog(null,"Department:        " + departmentName + "\n" + 
                                             "Item Name:          " + finalItem + "\n" +
                                              "Orginal Price:     $" + priceField + "\n" +
                                              "Discount:               " + discountField + "%" + "\n" +
                                              "New Price:          $" + newPrice + "\n");
        }
     } 

      private class PrintButtonListener implements ActionListener
     {
         public void actionPerformed(ActionEvent e)
         {


        }
     }      

     private class ExitButtonListener implements ActionListener
     {
        public void actionPerformed(ActionEvent e)
       {
           // Exit the application.
           System.exit(0);
        }
     }
}

Upvotes: 1

Views: 2428

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

You've declared your ArrayLists locally to the actionPerformed method of your NextButtonListener.

This means that each time the button is clicked, you create brand new, short lived, lists, adding your items to it and then discarding the results (allowing the garbage collector to pick them up sometime in the future).

You need to move these lists to scope level so that the various parts of your application can see them (maybe class level fields?).

Rather then using multiple array lists, where it's very easy to have the lists fall out of sync with each other, it would be a much better idea to create some kind of data object that contained all the information relevant to the transaction.

Lets say a Transaction object, which contains TransactionItems. Each item would contain the departmentname, itemName, originalPrice and discount (new price is a calculated field, no need to store it)

Transaction would then contain N TransactionItems.

On each click, you would get the current Transaction object and add a new TransactionItem to it based on the values entered by the user.

This would allow you to supply support functionality to the Transaction, such as the total value for the transaction for example

This will make significantly easier when it comes time to develop the JTable view.

Upvotes: 3

Related Questions