5 Replies - 501 Views - Last Post: 11 December 2011 - 07:20 AM Rate Topic: -----

#1 CB8379  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-December 11

Java JButton problem in Inventory 5 program

Posted 10 December 2011 - 10:29 PM

Hi all,

I am having issues with getting two of my buttons functioning for in my GUI for an inventory program. This project is due tomorrow and I have not had any success in trying to implement actionListeners for both my next and previous buttons. I am not receiving any errors and have attempted several variations of code by looking at other's examples and can not seem to find one that works with my project.

Could someone take a look and possibly give me an example of what may work in this instance?? Thank you!

(I am only submitting my class because this is the only part I am having trouble with)

package inventory4;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.text.NumberFormat;

/**
 *
 * @author Chrystal
 */
public final class Inventory4GUI extends JFrame implements ActionListener{

     
    //******************* private variables for each window***************
    //product number
    private final JLabel ProdNumL = new JLabel("Item Number: ");
    private JTextField ProdNumT;
    //product name
    private final JLabel ProdNameL = new JLabel("Product Name: ");
    private JTextField ProdNameT;
    //product units
    private final JLabel ProdUnitsL  = new JLabel ("Product Units: ");
    private JTextField ProdUnitsT;
    //product price
    private final JLabel ProdAmtL = new JLabel ("Product Price: ");
    private JTextField ProdAmtT;
    // product category
    private final JLabel ProdCatL = new JLabel ("Product Category: ");
    private JTextField ProdCatT;
    // product restock fee
    private final JLabel RestockL = new JLabel ("Restocking Fee: ");
    private JTextField RestockT;
    // product totals
    private final JLabel ProdTotL = new JLabel ("Product Totals: ");
    private JTextField ProdTotT;
    // product total with restock fee
    private final JLabel ProdTotValL = new JLabel ("Product Total with Restock Fee: ");
    private JTextField ProdTotValT;
    // Complete inventory totals
    private final JLabel InventoryTotalL = new JLabel ("Total Inventory Value: ");
    private JTextField InventoryTotalT;
    
    //declare ProdArray[]
    Category4 ProdArray[] = new Category4[5];

    int currView;
    
    //private JFrame f;
    private JPanel centerP;
    
    //*************************************************************************
    //**************************** GUI frame/ CONSTRUCTOR *********************
    Inventory4GUI()
    {
        super("Scrapbook Supply Inventory Program");  //title
        Dimension size= new Dimension(150, 40);  //create size of box (p. 524(31))
        FlowLayout layout = new FlowLayout(FlowLayout.LEFT); // set frame layout (p. 524(31))
        //FlowLayout bLayout = new FlowLayout (FlowLayout.RIGHT);

        centerP = new JPanel();
        centerP.setLayout(new BoxLayout(centerP, BoxLayout.Y_AXIS));
        currView = 0;

        
        //centerP.setLayout(new BoxLayout(centerP, BoxLayout.Y_AXIS));
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        
      
        
        //**************************Adding information boxes and 
        //**************************text fields to JFrame
        JPanel jp;
        //item number box- textfield of 6
        jp = new JPanel(layout);
        ProdNumL.setPreferredSize(size);
        jp.add(ProdNumL);
        ProdNumT = new JTextField(6);
        ProdNumT.setEditable(false);
        jp.add(ProdNumT);
        centerP.add(jp);
        
        //product name box- textfield of 10
        jp = new JPanel(layout);
        ProdNameL.setPreferredSize(size);
        jp.add(ProdNameL);
        ProdNameT = new JTextField(10);
        ProdNameT.setEditable(false);
        jp.add(ProdNameT);
        centerP.add(jp);
        
        //units textfield of 6
        jp = new JPanel(layout);
        ProdUnitsL.setPreferredSize(size);
        jp.add(ProdUnitsL);
        ProdUnitsT = new JTextField(6);
        ProdUnitsT.setEditable(false);
        jp.add(ProdUnitsT);
        centerP.add(jp);
        
        //category-textfield of 10
        jp = new JPanel(layout);
        ProdCatL.setPreferredSize(size);
        jp.add(ProdCatL);
        ProdCatT = new JTextField(10);
        ProdCatT.setEditable(false);
        jp.add(ProdCatT);
        centerP.add(jp);
        
        //product price-textfield of 6
        jp = new JPanel(layout);
        ProdAmtL.setPreferredSize(size);
        jp.add(ProdAmtL);
        ProdAmtT = new JTextField(6);
        ProdAmtT.setEditable(false);
        jp.add(ProdAmtT);
        centerP.add(jp);
        
        //restock fee- textfield of 6
        jp = new JPanel(layout);
        RestockL.setPreferredSize(size);
        jp.add(RestockL);
        RestockT = new JTextField(6);
        RestockT.setEditable(false);
        jp.add(RestockT);
        centerP.add(jp);
        
        //total value- textfield of 6
        jp = new JPanel(layout);
        ProdTotL.setPreferredSize(size);
        jp.add(ProdTotL);
        ProdTotT = new JTextField(6);
        ProdTotT.setEditable(false);
        jp.add(ProdTotT);
        centerP.add(jp);
        
        // product total with restock fee- textfield of 6
        jp = new JPanel(layout);
        ProdTotValL.setPreferredSize(size);
        jp.add(ProdTotValL);
        ProdTotValT = new JTextField(6);
        ProdTotValT.setEditable(false);
        jp.add(ProdTotValT);
        centerP.add(jp);
        
        // complete inventory total - textfield of 6
        jp = new JPanel(layout);
        InventoryTotalL.setPreferredSize(size);
        jp.add(InventoryTotalL);
        InventoryTotalT = new JTextField(6);
        InventoryTotalT.setEditable(false);
        jp.add(InventoryTotalT);
        centerP.add(jp);
        
        //exit on close and box format of window
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setContentPane(centerP);
        setSize(400, 600);
        setResizable(true);
        setLocationRelativeTo(null);
        setVisible(true);

       ProdArray[0] = new Category4("Blue Paper", "100", 15, 0.75, "paper");
       ProdArray[1] = new Category4 ("Green Paper", "101", 15, 0.75, "paper");
       ProdArray[2] = new Category4 ("Yellow Paper", "102", 15, 0.75, "paper");
       ProdArray[3] = new Category4 ("Red Paper", "103", 15, 0.75, "paper");
       ProdArray[4] = new Category4 ("Purple Paper", "104", 15, 0.75, "paper");
        
//display array
       
       for (int i=0; i<ProdArray.length; i++ )
        {
            // item number
            setProductNumber(ProdArray[i].getProductNumber());
            // item name
            setProductName(ProdArray[i].getProductName());
            //category name
            setCatName(ProdArray[i].getCatName());
            // item units
            setProductUnits(String.valueOf(ProdArray[i].getProductUnits()));
            // product price
            setProductAmount(String.valueOf(ProdArray[i].getProductAmount()));
            //product total before restock fee
            setProductValue(ProdArray[i].getProductUnits(),ProdArray[i].getProductAmount());
            // restock fee
            setFee(ProdArray[i].getFee());
            // product with restock fee
            setTotalValue(ProdArray[i].getProductUnits(),ProdArray[i].getProductAmount());
            // total of all supply values
            double inventoryValue = sumProductValues(ProdArray);
            InventoryTotals(inventoryValue);
        } // end for
        
        //******************adding buttons**************************************
        
        JButton firstButton = new JButton("First Item");
        //firstButton = new JButton("First Item");
        firstButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
             //StartView = 0;
             for (int i=0; i<ProdArray.length; i++ )
                {
                    setProductNumber(ProdArray[0].getProductNumber());
                    setProductName(ProdArray[0].getProductName());
                    setCatName(ProdArray[0].getCatName());
                    setProductUnits(String.valueOf(ProdArray[0].getProductUnits()));
                    setProductAmount(String.valueOf(ProdArray[0].getProductAmount()));
                    setProductValue(ProdArray[0].getProductUnits(),ProdArray[0].getProductAmount());
                    setFee(ProdArray[0].getFee());
                    setTotalValue(ProdArray[0].getProductUnits(),ProdArray[0].getProductAmount());
                    double inventoryValue = sumProductValues(ProdArray);
                    InventoryTotals(inventoryValue);
                 } // end for             
            }
        });
        centerP.add(firstButton);
        
        JButton lastButton = new JButton("Last Item");
        //lastButton = new JButton ("Last Item");
        lastButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
            for (int i=0; i<ProdArray.length; i++ )
                {
                    setProductNumber(ProdArray[4].getProductNumber());
                    setProductName(ProdArray[4].getProductName());
                    setCatName(ProdArray[4].getCatName());
                    setProductUnits(String.valueOf(ProdArray[4].getProductUnits()));
                    setProductAmount(String.valueOf(ProdArray[4].getProductAmount()));
                    setProductValue(ProdArray[4].getProductUnits(),ProdArray[4].getProductAmount());
                    setFee(ProdArray[4].getFee());
                    setTotalValue(ProdArray[4].getProductUnits(),ProdArray[4].getProductAmount());
                    double inventoryValue = sumProductValues(ProdArray);
                    InventoryTotals(inventoryValue);
                 } // end for
            }
        });
        centerP.add(lastButton);
        
        JButton nextButton = new JButton("Next Item");
        //nextButton = new JButton("Next Item");
        nextButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                
            }
        });
        centerP.add(nextButton);
        
        JButton prevButton = new JButton("Previous Item");
        //prevButton = new JButton("Previous Item");
        prevButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
            
            }
        });
        centerP.add(prevButton);
        
            
    }// end Inventory4GUI() CONSTRUCTOR
    public void array()
    {
        
    }
    
    // all set methods to display GUI from main
    public void setProductName(String name)
    {
        ProdNameT.setText(name);
    }
    
    public void setProductNumber(String number)
    {
        ProdNumT.setText(number);
    }
    
    public void setProductUnits(String units)
    {
        ProdUnitsT.setText(units);
    }
    
    public void setProductAmount(String amount)
    {
        ProdAmtT.setText(amount);
    }
    
    public void setCatName(String category)
    {
        ProdCatT.setText(category);
    }
    
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    
    //product value is the amount of product times units (product total w/o fee)
    public void setProductValue(double price, double units)
    {
        double value = price*units;
        ProdTotT.setText(nf.format(value));
    }
    
    // total value is the the (product total w/ restock fee)
    public void setTotalValue(double units, double amt)
    {
        double TotValue = (0.05*(units*amt))+(units*amt);
        ProdTotValT.setText(nf.format(TotValue));
    }
    
    // this is the restocking fee * units per product
    public void setFee(double units)
    {
        RestockT.setText(nf.format(units));
    }
   
    
    //this is for the complete inventory total
    public void InventoryTotals(double total)
    {
        InventoryTotalT.setText(nf.format(total));
    }

    // loop to calculate inventory totals
    public double sumProductValues(Category4[] array)
    {
        Category4[] ProdArray = array;
        double total = 0;
        double price;
        double amt;
        for (int i=0; i<ProdArray.length; i++ )
            {
            price = ProdArray[i].getProductAmount();
            amt = ProdArray[i].getProductUnits();
            double this_value = amt*price;
            total = total + this_value;
            } // end for                
        return total;
    } // end sumProductValues  

    @Override
    public void actionPerformed(ActionEvent ae) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


 } // end JFrame


Attached File(s)

  • Attached File  week8.zip (28.69K)
    Number of downloads: 21


Is This A Good Question/Topic? 0
  • +

Replies To: Java JButton problem in Inventory 5 program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java JButton problem in Inventory 5 program

Posted 10 December 2011 - 10:54 PM

There is no code in your actionPerformed()
what problem do you have ?
Was This Post Helpful? 0
  • +
  • -

#3 anitdragon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-October 11

Re: Java JButton problem in Inventory 5 program

Posted 10 December 2011 - 10:57 PM

have to tried changing all the Category4 arrays to Category arrays then import java.util.Locale.Category?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java JButton problem in Inventory 5 program

Posted 10 December 2011 - 11:20 PM

You don't import at run time but I compilation time

Didn't pass through all you code but I guess you want different elements of your arrays when the "next" "previous" buttons are clicked

You will need an instance variable int displayedIndex that will hold the index of the array displayed
Your actionPerformed() for the next button will have to
displayedIndex++;
if(displatedIndex == ProdArray.length) displayedIndex == 0; // to wrap aroun

Your actionPerformed() for the previous button will have to
displayedIndex--;
if(displatedIndex < 0) displayedIndex == ProdArray.length - 1; // to wrap around

and then display the good array element

Happy coding
Was This Post Helpful? 0
  • +
  • -

#5 CB8379  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-December 11

Re: Java JButton problem in Inventory 5 program

Posted 11 December 2011 - 07:14 AM

Hello Pbl-

I just got up for the morning. I actually attempted to try a code similar to yours last night. The instance variable I had created (which is declared with the other variables) was currView = 0; .

I had also tried to set up a displayView() method with the array included within the method, and then wrote the actionListener with a similar set up to what you are displaying, but the button didn't work at all. Let me give it a try again...

WOW- after taking your snippet into consideration, the next button did not work, HOWEVER, I tried something a little different (because I do not think I had the instance variable linked to the array) and tried to include the array in the actionListener indexing the currView, and it worked!!

here is the updated code that is now functioning:

        JButton nextButton = new JButton("Next Item");
        //nextButton = new JButton("Next Item");
        nextButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                currView = 0;
                currView++;
                if (currView==ProdArray.length)
                    currView=0;
                for (int i=0; i<ProdArray.length; i++ )
                {
                    setProductNumber(ProdArray[currView].getProductNumber());
                    setProductName(ProdArray[currView].getProductName());
                    setCatName(ProdArray[currView].getCatName());
                    setProductUnits(String.valueOf(ProdArray[currView].getProductUnits()));
                    setProductAmount(String.valueOf(ProdArray[currView].getProductAmount()));
                    setProductValue(ProdArray[currView].getProductUnits(),ProdArray[currView].getProductAmount());
                    setFee(ProdArray[currView].getFee());
                    setTotalValue(ProdArray[currView].getProductUnits(),ProdArray[currView].getProductAmount());
                    double inventoryValue = sumProductValues(ProdArray);
                    InventoryTotals(inventoryValue);
                 }
                
            }
        });
        centerP.add(nextButton);


Was This Post Helpful? 0
  • +
  • -

#6 CB8379  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-December 11

Re: Java JButton problem in Inventory 5 program

Posted 11 December 2011 - 07:20 AM

But, I have 5 objects in the array, when I press on next/previous, it only moves up by one spot, when I click on one of them again, it does not continue through each object in the array.. any suggestions?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1