Results 1 to 7 of 7
  1. #1
    ntoxic8 is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Creating a loop for GUI

    I am trying to create a loop for my GUI, but can't seem to figure out how to do the loop so that I can create a array later down the road.
    any help would be appreciated.



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

    public class RetailGUI extends JFrame
    {



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

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



    /**
    * Constructor
    */

    public RetailGUI()
    {


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



    setSize(600, 300);

    // 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.
    enterButton = new JButton("Enter");
    printButton = new JButton("Print");
    exitButton = new JButton("Exit");

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

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

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


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

    new RetailGUI();
    }
    }

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

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







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







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

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Creating a loop for GUI

    Please tell us more details about your current problem as you're leaving a lot up in the air I think. Also please edit your post and add [code] [/code] tags around your posted code. The FAQ's and my links below can help.

  3. #3
    ntoxic8 is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating a loop for GUI

    The GUI is for a fake retail store. The employee is supposed to be able to choose the department, enter the item name, the original price, and the discount. The employee should be able to so the any number of items. When the employee is finished they should be able to hit print and a JTable should be displayed. I have tried different spots to place the loop. Also I need an idea on how to clear the fields and place the vaules into the array so that they can start on the next item.

    Java Code:
      
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
      
      public class RetailGUI extends JFrame
       {
          
              
    
         // The following variables will reference the
          // custom panel objects.
          private DepartmentPanel department;     // Bagel panel
          private ItemPanel item; // Topping panel
          private PricePanel price;    // Coffee panel
          private TitlePanel title;  // To display a greeting
     
          // The following variables will reference objects
          // needed to add the Calculate and Exit buttons.
          private JPanel buttonPanel;    // To hold the buttons
          private JButton enterButton,    // To calculate the cost
                          printButton,
                          exitButton;    // To exit the application
         
     
     
          /**
          * Constructor
           */
     
          public RetailGUI()
          {
            
              
            // Display a title.
             super("Retail Calculator");
             
             
             
             setSize(600, 300);
     
             // 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.
             enterButton = new JButton("Enter");
             printButton = new JButton("Print");
             exitButton = new JButton("Exit");
     
            // Register the action listeners.
             enterButton.addActionListener(new RetailGUI.EnterButtonListener());
             printButton.addActionListener(new RetailGUI.PrintButtonListener());
             exitButton.addActionListener(new RetailGUI.ExitButtonListener());
     
             // Add the buttons to the button panel.
             buttonPanel.add(enterButton);
             buttonPanel.add(printButton);
             buttonPanel.add(exitButton);
          }
     
          /**
          * Private inner class that handles the event when
           * the user clicks the Calculate button.
           */
     
          
          private class EnterButtonListener implements ActionListener
         {
             public void actionPerformed(ActionEvent e)
             {
               
                 new RetailGUI();
            }
         } 
    
          private class PrintButtonListener implements ActionListener
         {
             public void actionPerformed(ActionEvent e)
             {
               
                 String departmentName,
                        finalItem;
                 float    priceField,
                          discountField,
                          newPrice;
                 
                          
                 
                 
                 
    
               
                departmentName = department.getDepartmentName();
                finalItem = item.getItemName();
                priceField = price.getPriceField();
                discountField = price.getDiscountField();
                newPrice = priceField - (priceField * (discountField/100));
              
             
                    
              
              
               
              
               JOptionPane.showMessageDialog(null,"Department:        " + departmentName + "\n" + 
                                                 "Item Name:          " + finalItem + "\n" +
                                                  "Orginal Price:     $" + priceField + "\n" +
                                                  "Discount:               " + discountField + "%" + "\n" +
                                                  "New Price:          $" + newPrice + "\n");
            }
         }      
          
         private class ExitButtonListener implements ActionListener
         {
            public void actionPerformed(ActionEvent e)
           {
               // Exit the application.
               System.exit(0);
            }
         }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Creating a loop for GUI

    Thanks for your edits and your clarifications. Let me see if I understand you correctly. Are you thinking that you need a for loop for entering the data? If so, then you're mistaken and likely thinking along the lines of a console program where you must loop to get multiple inputs from the user. What you've got above is a Swing GUI program and it is event-driven, meaning no for loop is needed to get the data, but rather the data is extracted at the control of the user and only occurs when he presses the Enter button. Your current Enter button handler appears to try to create a whole new GUI, and you don't want to do that either. What you will need to do is to get the data to the user, and add it to your data repository, possibly an ArrayList or Vector. Then when the print button is pressed, use the data repository as the nucleus of your JTable's table model. If you hold the data in a Vector, you can simply pass that into the constructor of a DefaultTableModel object along with a Vector<String> for your column names, and if the JTable is constructed with this, the table is done and full of data.

  5. #5
    ntoxic8 is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating a loop for GUI

    Ok to how do I clear the textfield once I press enter?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    ntoxic8 is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating a loop for GUI

    Could I create the ArrayList in this section? If so what would it looke like?
    Java Code:
    public void actionPerformed(ActionEvent e)
             {
               
                 String departmentName,
                        finalItem;
                 float    priceField,
                          discountField,
                          newPrice;
                 
                          
                 
                
               
                departmentName = department.getDepartmentName();
                finalItem = item.getItemName();
                priceField = price.getPriceField();
                discountField = price.getDiscountField();
                newPrice = priceField - (priceField * (discountField/100));
              
             
                    
              
              
              
               JOptionPane.showMessageDialog(null,"Department:        " + departmentName + "\n" + 
                                                 "Item Name:          " + finalItem + "\n" +
                                                  "Orginal Price:     $" + priceField + "\n" +
                                                  "Discount:               " + discountField + "%" + "\n" +
                                                  "New Price:          $" + newPrice + "\n");
            }

Similar Threads

  1. Need help with creating a loop
    By bamagirl31 in forum New To Java
    Replies: 3
    Last Post: 06-23-2011, 03:18 AM
  2. Need Help Creating A Loop!
    By chrisnojen117 in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 07:30 PM
  3. Creating a loop help !
    By theman279 in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 10:13 AM
  4. Help with creating a while loop!
    By BAD in forum New To Java
    Replies: 1
    Last Post: 07-09-2010, 09:00 PM
  5. Creating a Loop
    By SenorJalapeno in forum New To Java
    Replies: 3
    Last Post: 04-02-2010, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •