Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: My code

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My code

    I a trying to get help coding the buttons in my code. My buttons need to loop though the products in the array.


    /* Program: Display Inventory GUI Form
    * File: DisplayInventory.java
    * Summary: Inherits JFrame and build an aplication that contains label and
    * text fields.
    * Author: Mark Byous
    * Date: 10/10/2013
    */

    import java.awt.GridLayout; // create grid layout
    import java.awt.BorderLayout; // set border layout
    import java.awt.event.ActionEvent; // required to click events
    import java.awt.event.ActionListener; // required for click events
    import javax.swing.JPanel; // create panels
    import javax.swing.JFrame; // require to use the frame
    import javax.swing.JButton; // required to create buttons
    import javax.swing.JLabel; // requierd to create text fields
    import javax.swing.JTextField; // required to create texts fields
    import javax.swing.ImageIcon; // required to access images

    public class DisplayInventory extends JFrame implements ActionListener
    {
    // Declare Class Variables

    // Declare Gridpanel as main panel inserted into gridPanel
    private JPanel gridPanel = new JPanel(); // one panel that contains my gridLayout
    private JPanel panel = new JPanel(); // panels used to display buttons, labels and text fields

    // Declare buttons
    JButton firstButton; // first button
    JButton nextButton; // next button
    JButton previousButton; // previous button
    JButton lastButton; // last button

    // Declare text Fields
    JTextField itemField; // item field
    JTextField itemNumberField; // itemnumber field
    JTextField priceField; // price field
    JTextField inventoryField; // inventory field
    JTextField numberField; // number field
    JTextField weightField; // weight field
    JTextField restockField; // restock field
    JTextField totalInventoryField; // totalInventory field

    // Declare Labels
    JLabel lblItem; // item label
    JLabel lblItemNumber; // itemNumber label
    JLabel lblPrice; // price label
    JLabel lblInventory; // inventory label
    JLabel lblNumber; // number label
    JLabel lblWeight; // weight label
    JLabel lblRestock; // restock label
    JLabel lblTotalInventory; // totalInventory label

    // Declare 5 Stock Items
    Product product1;
    Product product2;
    Product product3;
    Product product4;
    Product product5;

    private static final int Max_Product = 5; // maximum size of product array

    Product[] product = new Product[Max_Product]; // create product array

    private static int ArrayIndex = 0; // array index

    public DisplayInventory() // constructor

    {
    // begin DisplayInventory class


    // create and initialize five product objects
    // instantiate the Stock class
    Product product1 = new Product("Fuse", 123, 10.25, 10, .50); // create an instance of a stock item
    Product product2 = new Product("Resistor", 124, 6.55, 6, .0042); // create an instance for a stock item
    Product product3 = new Product("Capacitor", 125, 8.47, 12, 1.00); // create an instance for a stock item
    Product product4 = new Product("Inductor", 126, 9.86, 14, 2.00); // create an instance for a stock item
    Product product5 = new Product("Transistor", 127, 14.60, 10, .0034); // create an insatnce for a stock item



    // add the product objects to the array
    product[0] = product1; // add Fuse to Stock array
    product[1] = product2; // add Resistor to Stock array
    product[2] = product3; // add Capacitor to Stock array
    product[3] = product4; // add Inductor to Stock array
    product[4] = product5; // add Transistor to Stock array

    Product.sortByItem(product); // sort the array of products by name


    gridPanel.setLayout(new BorderLayout()); // create border layout
    gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); // add label panel
    gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); // add field panel
    gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); // add button panel
    add(gridPanel);
    }

    // helper method creates a panel for the button
    private JPanel createButtonPanel()
    {
    // ActionListener btnListen = new ButtonListener(); // create listener

    //create buttons
    firstButton = new JButton("First"); // create first button
    firstButton.setActionCommand("First"); // action command for button event // button listener to click button
    firstButton.addActionListener(this);

    nextButton = new JButton("Next"); // create and label button
    nextButton.setActionCommand("Next"); // action command for click button event
    // nextButton.addActionListener(btnListen); // button listener for click button event
    nextButton.addActionListener(this);

    previousButton = new JButton("Previous"); // create button to view previous product
    previousButton.setActionCommand("Previous"); // action command for click button
    previousButton.addActionListener(this); // button for click button event

    lastButton = new JButton("Last"); // create last button
    lastButton.setActionCommand("Last"); // action command for click button
    lastButton.addActionListener(this); // add action Listtener for click button


    // create panel object
    JPanel panel = new JPanel();

    panel.add(firstButton); // add first button panel
    panel.add(nextButton); // add next button panel
    panel.add(previousButton); // add previous button panel
    panel.add(lastButton); // add last button panel

    return panel; // return panel
    } // end createButtonPanel method

    private JPanel createLabelPanel() {

    // create instance label objects
    ImageIcon icon = new ImageIcon(".\\src\\productLogo.jpg"); // get logo
    JLabel lblLogo = new JLabel(icon); // add the image to a label

    lblItem = new JLabel("Product Name:"); // label for name of product
    lblItemNumber = new JLabel("Product Number:"); // Label for product number
    lblPrice = new JLabel("Product Price:"); // label for pricr of product
    lblInventory = new JLabel("The Inventory Price is:"); // label for total inventory price
    lblNumber = new JLabel("Number of Items in Stock:"); // label for amount of items in inventory
    lblWeight = new JLabel("Weight of Product:"); // label for weight of product
    lblRestock = new JLabel("The Restock fee is:"); //label for restocking fee
    lblTotalInventory = new JLabel("The Price of the Entire Inventory:"); // label for the total inventory

    panel = new JPanel();
    panel.setLayout(new GridLayout(6, 1));

    // add labels to the panel
    panel.add(lblLogo);

    panel.add(lblItem);
    panel.add(lblItemNumber);
    panel.add(lblPrice);
    panel.add(lblInventory);
    panel.add(lblNumber);
    panel.add(lblWeight);
    panel.add(lblRestock);
    panel.add(lblTotalInventory);

    return panel;
    } // end createlabelPanel method

    private JPanel createTextPanel()
    {

    // create instances of text box objects
    JLabel lblCompanyName = new JLabel(" Huber"); // company Logo

    itemField = new JTextField(); // item text field
    itemField.setEditable(false); // set the field as not editable so that the user can not edit field

    itemNumberField = new JTextField(); // itemNumber text field
    itemNumberField.setEditable(false); // set the field as not editable so that the user can not edit the field

    priceField = new JTextField(); // price text field
    priceField.setEditable(false); // set field as not editable

    numberField = new JTextField(); // number text field
    numberField.setEditable(false); // set field as not editable

    weightField = new JTextField(); // weight text field
    weightField.setEditable(false); // set field as not editable

    inventoryField = new JTextField(); // inventory text field
    inventoryField.setEditable(false); // set field as not editable

    restockField = new JTextField(); // restock text field
    restockField.setEditable(false); // set field as not editable

    totalInventoryField = new JTextField(); // totalInventory text field
    totalInventoryField.setEditable(false); // set field as not editable

    panel = new JPanel(); // create panel object
    panel.setLayout(new GridLayout(6, 1)); // create grid layout

    panel.add(lblCompanyName); // add logo to gridLayout

    panel.add(itemField); // add the product name to the grid layout
    panel.add(itemNumberField); // add the product number to the grid layout
    panel.add(priceField); // add the product price to the grid layout
    panel.add(inventoryField); // add the price of the product inventory to the grid layout
    panel.add(numberField); // add the number of items in stock to the grid layout
    panel.add(weightField); // add the weight of the product to the grid layout
    panel.add(restockField); // add the restoick fee to the grid layout
    panel.add(totalInventoryField); // add the amount of the totalInventory to that grid layout

    return panel; // return the panel

    } // end createTextPanel method

    @Override
    public void actionPerformed(ActionEvent e)
    {
    if(product[ArrayIndex].getTotalInventory() == 0) //
    {
    for(int x=0;x<product.length;x++)
    product[x].sumInventoryOfProducts(product[x].getInventory());
    } // end for

    totalInventoryField.setText(String.valueOf(product[ArrayIndex].getTotalInventory())); // display the sum of inventory

    // add button functions
    if( "First".equals(e.getActionCommand())) // if the first button is cliocked
    {
    ArrayIndex = 0; // set array index to the first element
    setFieldValues(); // display product information to form
    }

    if("Next".equals(e.getActionCommand())) // if the next button is clicked
    {
    ArrayIndex = (++ArrayIndex); // set array index to the previous element
    setFieldValues(); // diplay product information to the form

    }
    if("Previous".equals(e.getActionCommand())) // if the previous button is clicked
    {
    --ArrayIndex;
    if (ArrayIndex < 0)
    ArrayIndex = product.length - 1; // set array index to the next element
    setFieldValues(); // display product information to the form
    }
    if("Last".equals(e.getActionCommand())) // if the last button is clicked
    {
    ArrayIndex = product.length - 1; // set array index to the last button
    setFieldValues(); // display product information to form

    }
    } // end action performed

    private void setFieldValues()
    { // display product information to GUI

    itemField.setText(product[ArrayIndex].getItem()); // get the product name
    // and assign it to the item text field
    itemNumberField.setText(String.valueOf(product[ArrayIndex].getItemNumber())); // get the product number
    // and assign it to the itemnumber text field
    priceField.setText(String.valueOf(product[ArrayIndex].getPrice())); // get the price of the product
    // and assign it to the price text field
    inventoryField.setText(String.valueOf(product[ArrayIndex].getInventory())); // get the inventory price
    // and assign it to the inevntory text field
    numberField.setText(String.valueOf(product[ArrayIndex].getNumber())); // get the number of items in stock
    // and assign it to the number text field
    weightField.setText(String.valueOf(product[ArrayIndex].getWeight())); // get the weight of the product
    // and assign it to the weight text field
    restockField.setText(String.valueOf(product[ArrayIndex].getRestock())); // get the restock fee
    // and assign it to the restock text field
    totalInventoryField.setText(String.valueOf(product[ArrayIndex].getTotalInventory())); // get the amount of the totalInventory
    // and assign it to the totalInventory text field
    ArrayIndex = ArrayIndex + 1; // increment the arrayindex to get to the next value

    }


    public static void main(String[] args)
    {
    JFrame frame = new DisplayInventory(); // create a frame object

    frame.setSize(400, 200); // set the size of the window
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E); // close the application
    frame.setTitle("My Inventory Application"); // set the title of the window
    frame.setLocationRelativeTo(null); // center the form
    frame.setVisible(true); // display form

    } // end main method
    } // end DisplayInventory class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My code

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code

    Norm I am not clear on your reply. Are you saying that I should use [] instead of the () that I used to code my buttons?



    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My code

    I was NOT talking about anything in your code. I was asking you to put code tags before and after the code that you posted. Look at other threads on the forum to see how everyone's code is formatted by using code tags.

    See this: http://www.javaprogrammingforums.com...uncements.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM