I am having a difficult time with this one line of code for my homework that is due Sun.:

Modify the Inventory5 Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the Inventory5. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.

Add a company logo to the GUI using Java graphics classes :

package inventory5;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.UIManager;

class Inventory5Main implements ActionListener
{
    XboxDVD[] product;
    JTextField[] fields;
    NumberFormat nf;

    public void actionPerformed(ActionEvent e)

    {
        int index = ((JComboBox)e.getSource()).getSelectedIndex();
        populateFields(index);
    }

    public static void main(String[] args)

    {
        try
        {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (Exception e)
        {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        Inventory5Main test = new Inventory5Main();
        test.initXboxDVD();
        test.showGUI();
        test.populateFields(0);
    }

    private void initXboxDVD() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void populateFields(int i) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void showGUI() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /*public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    private void initXboxDVD() {
    throw new UnsupportedOperationException("Not yet implemented");
    }

    private void populateFields(int index) {
    throw new UnsupportedOperationException("Not yet implemented");
    }

    private void showGUI() {
    throw new UnsupportedOperationException("Not yet implemented");*/    
}

XboxDVD[] product; This is where the problem is ........and i dont know where or how to correct this !

Recommended Answers

All 6 Replies

Do you have in your package inventory5 class called XboxDVD? If not, there is your issue...

I don't know if this is relevant or not, since looking at your code, I see nothing really. But to switch the item in the combo box you'd just use jComboBox.setSelectedItem(index). And your method would obviously have to check the array to make sure you're in bounds, then flip to the end of it if you're at the first or last item.

I guess my issue is bigger than i thought because i started this from scratch. Would it have been better to use and existing XboxDVD Class from my Inventory 4 projects. Here is the code for Inventory 4 XboxDVD Classes :

Inventory 4:

class XboxDVD {
    private int XboxDVDItem;
    private String XboxDVDTitle;
    private int XboxDVDStock;
    private double XboxDVDPrice;

    public XboxDVD(int item, String title, int stock, double price) {
        XboxDVDItem  = item;
        XboxDVDTitle = title;
        XboxDVDStock = stock;
        XboxDVDPrice = price;
    } //end four-argument constructor

    // set XboxDVD Item
    public void setXboxDVDItem(int item) {
        XboxDVDItem = item;
    } //end method  set XboxDVD Item

    //return XboxDVD Item
    public int getXboxDVDItem() {
        return XboxDVDItem;
    } //end method get XboxDVD Item

    //set XboxDVD Title
    public void setXboxDVDTitle(String title) {
        XboxDVDTitle = title;
    } //end method set XboxDVD Title

    //return XboxDVD Title
    public String getXboxDVDTitle() {
        return XboxDVDTitle;
    } //end method get XboxDVD Title

    public void setXboxDVDStock(int stock) {
        XboxDVDStock = stock;
    } //end method set XboxDVD Stock

    //return XboxDVD Stock
    public int getXboxDVDStock() {
        return XboxDVDStock;
    } //end method get XboxDVD Stock

    public void setXboxDVDPrice(double price) {
        XboxDVDPrice = price;
    } //end method setXboxDVDPrice

    //return XboxDVD Price
    public double getXboxDVDPrice() {
        return XboxDVDPrice;
    } //end  method get XboxDVD Price

    //calculate inventory value
    public double value() {
        return XboxDVDPrice * XboxDVDStock;
    } //end method value

        public String toString() {
        return String.format("item=%3d   title=%-20s   units=%3d   price=$%6.2f   value=$%7.2f",
                              XboxDVDItem, XboxDVDTitle, XboxDVDStock, XboxDVDPrice, value());
    }

} 
//end class XboxDVD

Should i take this class from existing Inventory4 package? If so where would i want to put this ?

You should included this class in your project if there are no major changes required by your assignment, or create new one if there is lot of things to change.
In short, yes you need that class.

Would i be changing the entire line or just the way you said it?
Here is the changed code:

int index = ((JComboBox)e.getSource()).setSelected(Index)();

What you want to do is cause the item in the JComboBox to appear. So you will need to use nameOfYourJComboBox.setSelectedIndex(indexYouWantToShowUp);

Also, get rid of all those comments that say "//end whatever method", they are not useful and make it harder to read your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.