Looking for help with Inventory Program Part 4 - Creating a GUI

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1459 Views - Last Post: 11 May 2014 - 08:56 AM Rate Topic: -----

#1 sewg82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-May 14

Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 05 May 2014 - 01:02 PM

Hello,

I am looking for help with my Inventory Program part 4 assignment. I am supposed to modify the code I wrote for part 3 to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of the product. It is also supposed to display the value of the entire inventory, the additional attribute (in my code I added the year the movie was released), and also the restocking fee.

My whole problem is that I have never done this before, and I'm really at a loss as far as where or how to begin. I've "Googled It" and have found pretty much nothing but dead ends. Below is the code that I have from my part 3 assignment. I just don't know how to begin the next step to create the GUI. Any help would be greatly appreciated.

package inventoryprogrampt4;

/**
 *
 * @author sgressett
 */
public class InventoryProgramPt4 {
    static int max = 5;
    static final Year[]year = new Year[max];
        
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        year[0] = new Year("The Hunger Games", 4015, 1, 14.99, 2012, 14.99, 0.75);
        year[1] = new Year("Twilight", 2174, 2, 7.99, 2008, 15.98, 0.40);
        year[2] = new Year("The Music Man", 8225, 1, 12.99, 1962, 12.99, 0.65);
        year[3] = new Year("The Other Boleyn Girl", 7453, 1, 13.99, 2008, 13.99, 0.70);
        year[4] = new Year("The Fast & The Furious", 1421, 1, 14.99, 2001, 14.99, 0.75);
        calcinv(year);
        printinv(year);
        sortinv(year);
        printinv(year);    
    }
    
    static void printinv(Year[]year) {
        System.out.println();
        
      for (int x=0; x<max; x++) {
          System.out.println("ID: " + year[x].pnumber);
          System.out.println("Name: " + year[x].pname);
          System.out.println("Units: " + year[x].punit);
          System.out.println("Price: $" + year[x].pprice);
          System.out.println("Release Year: " + year[x].movieYear);
          System.out.println("Restocking Fee: $" + year[x].reStockFee);
      }
    }
    
    static void calcinv(Year[]year) {//method calculates total inventory value
        double ivalue = 0;
        
     for (int x=0; x<max; x++) {
         ivalue += year[x].punit + year[x].pprice;
     }
     System.out.println("Total Inventory Value is: $" + ivalue);
    }
    
    static void sortinv(Year[]year) {//method sorts the array
        java.util.Arrays.sort(year);
    }
}

class DVD implements Comparable <DVD> {//creation of DVD class
    double punit, pnumber, pprice;
    String pname;
    
    public DVD(String iname, double inumber, double iunit, double iprice) {//defines variables
        pname = iname;
        pnumber = inumber;
        punit = iunit;
        pprice = iprice;
    }
    
    public int compareTo(DVD sortDVD) {
        return this.pname.compareTo(sortDVD.pname);
    }
}

class Year extends DVD {//creation of subclass
    public double movieYear;
    double value;
    double reStockFee;
    
    public Year(String iname, double inumber, double iunit, double iprice, int year,
            double invalue, double RestockFee) {//declaration of new variables created in subclass
        super(iname, inumber, iunit, iprice);
        movieYear = year;
        value = invalue;
        reStockFee = RestockFee;
    }
    
    public void setYear (int year) {//method sets the year
        movieYear = year;
    }
    
    public double getYear() {//method gets the year
        return movieYear;
    }
    
    public void getRestockFee() {//method calculates the restocking fee
        value = super.punit * super.pprice;
        reStockFee = value * 0.05;
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Looking for help with Inventory Program Part 4 - Creating a GUI

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,155
  • Joined: 12-June 08

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 05 May 2014 - 01:14 PM

Have you looked through the tutorial section? Sure a bit old, but still valid.

http://www.dreaminco...torial-in-java/
http://www.dreaminco...c-gui-concepts/

https://netbeans.org...ctionality.html
Was This Post Helpful? 1
  • +
  • -

#3 sewg82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 05 May 2014 - 01:15 PM

Not yet I haven't, but thank you. I will definitely check them out.
Was This Post Helpful? 0
  • +
  • -

#4 sewg82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 05 May 2014 - 04:40 PM

Hello again,


I checked into the tutorials and the only one that helped me was the NetBeans one. I've managed to get a basic GUI created, but I'm not sure how to get the information I need displayed into the program. Can anyone help to clear this up for me please?

This is the code that was generated when I created the GUI:

package inventoryprogrampt4;

/**
 *
 * @author sgressett
 */
public class DvdGUI extends javax.swing.JPanel {

    /**
     * Creates new form DvdGUI
     */
    public DvdGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setBackground(new java.awt.Color(153, 153, 255));
        setForeground(new java.awt.Color(153, 153, 153));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setBackground(new java.awt.Color(0, 153, 153));
        jButton1.setText("Next");

        jButton2.setBackground(new java.awt.Color(0, 153, 204));
        jButton2.setText("Back");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(47, 47, 47))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(204, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(35, 35, 35))
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
}




This is the code that holds the information I need the GUI to display:

package inventoryprogrampt4;

/**
 *
 * @author sgressett
 */
public class InventoryProgramPt4 {
    static int max = 5;
    static final Year[]year = new Year[max];
        
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        year[0] = new Year("The Hunger Games", 4015, 1, 14.99, 2012, 14.99, 0.75);
        year[1] = new Year("Twilight", 2174, 2, 7.99, 2008, 15.98, 0.40);
        year[2] = new Year("The Music Man", 8225, 1, 12.99, 1962, 12.99, 0.65);
        year[3] = new Year("The Other Boleyn Girl", 7453, 1, 13.99, 2008, 13.99, 0.70);
        year[4] = new Year("The Fast & The Furious", 1421, 1, 14.99, 2001, 14.99, 0.75);
        calcinv(year);
        printinv(year);
        sortinv(year);
        printinv(year);    
    }
    
    static void printinv(Year[]year) {
        System.out.println();
        
      for (int x=0; x<max; x++) {
          System.out.println("ID: " + year[x].pnumber);
          System.out.println("Name: " + year[x].pname);
          System.out.println("Units: " + year[x].punit);
          System.out.println("Price: $" + year[x].pprice);
          System.out.println("Release Year: " + year[x].movieYear);
          System.out.println("Restocking Fee: $" + year[x].reStockFee);
      }
    }
    
    static void calcinv(Year[]year) {//method calculates total inventory value
        double ivalue = 0;
        
     for (int x=0; x<max; x++) {
         ivalue += year[x].punit + year[x].pprice;
     }
     System.out.println("Total Inventory Value is: $" + ivalue);
    }
    
    static void sortinv(Year[]year) {//method sorts the array
        java.util.Arrays.sort(year);
    }
}

class DVD implements Comparable <DVD> {//creation of DVD class
    double punit, pnumber, pprice;
    String pname;
    
    public DVD(String iname, double inumber, double iunit, double iprice) {//defines variables
        pname = iname;
        pnumber = inumber;
        punit = iunit;
        pprice = iprice;
    }
    
    public int compareTo(DVD sortDVD) {
        return this.pname.compareTo(sortDVD.pname);
    }
}

class Year extends DVD {//creation of subclass
    public double movieYear;
    double value;
    double reStockFee;
    
    public Year(String iname, double inumber, double iunit, double iprice, int year,
            double invalue, double RestockFee) {//declaration of new variables created in subclass
        super(iname, inumber, iunit, iprice);
        movieYear = year;
        value = invalue;
        reStockFee = RestockFee;
    }
    
    public void setYear (int year) {//method sets the year
        movieYear = year;
    }
    
    public double getYear() {//method gets the year
        return movieYear;
    }
    
    public void getRestockFee() {//method calculates the restocking fee
        value = super.punit * super.pprice;
        reStockFee = value * 0.05;
    }
}



Was This Post Helpful? 0
  • +
  • -

#5 AGENT_P6  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 33
  • Joined: 05-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 05 May 2014 - 11:22 PM

GUI's can be really frustrating when you first learn. The key thing to remember is that you need a layout manager. If you try to just add things to the JPanel it just won't work - you'll have a button the size of the entire panel. Then you'll sit there for hours trying to setSize() and setPreferredSize() with no results.

Here is a link to a specific part of the official java tutorial for Swing. Take a look at the list of links on the left hand side. These are your different layout managers. I suggest starting off with GridLayout and making a 2 column layout to get used to things.

With GridLayout your JFooBars will always stretch out to fill the entire size of the cell of the grid. If your window is resizable your JFooBars will resize accordingly, resulting in ugliness. As a GUI beginner, I suggest making the window not resizable otherwise you'll waste a bunch of time fidgeting trying to get it to look right when it never really will with just JFooBars slapped raw into a GridLayout

To add a layout to a JPanel just simply create the JPanel and pass the constructor a new Layout
JPanel myGridPanel = new JPanel(new GridLayout(5, 2));//rows,cols



When you want to add to the grid, if I remember correctly, it populates all columns of a row before advancing to the next row. You don't need to talk to the layout to add things though - just add to the JPanel and the GridLayout will take care of it for you.
JButton fooClicker = new JButton("Foo?");
JButton barClicker = new JButton("Bar?");
JButton foobarClicker = new JButton("Foobar!");
//other JStuff

myGridPanel.add(fooClicker);
myGridPanel.add(barClicker);
myGridPanel.add(foobarClicker);
//add all other JStuff 



Once you get the hang of it enough that you're actually having fun rather than pulling your hair out, then try out the different layout managers. If you really want to get fancy, you can cheat the default auto-max-size behavior of JComponents by placing them in their own JPanel and adding the JPanel to the Layout. That way the layout manager only sees the outside JPanel layer and makes the JPanel stretch, but not what's inside. This can get complicated quite quickly but it can make for a seriously sexy GUI if you work at it.

But remember, GridLayout WILL stretch your JThings, so don't fight it. If things don't show up in the GUI, most times its because you forgot to add it to the JPanel;
Was This Post Helpful? 1
  • +
  • -

#6 AGENT_P6  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 33
  • Joined: 05-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 05 May 2014 - 11:56 PM

I found a perfect little snippet for you in my dropbox. This program should have almost everything you need to learn the basics of GUI through a working example to reference. As you'll see, the GUI shell is less than 100 lines and every line is a no-brainer - so GUI's aren't really all that hard :)/>/>/> It's just the layout manager that throws people for a loop.

Oh, I forgot to mention earlier that you need your GUI class to extend JFrame. Think of the JFrame as the actual program window as a whole, and think of the JPanel as the center...kind of like a window: frame and panel.


import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import java.awt.FlowLayout;
import java.awt.GridLayout;

public class JPanelExercise extends JFrame{

    public static void main(String[] args) {
        
        //Make the overall frame
        JFrame box = new JFrame("Awesomeness");
        box.setLayout(new GridLayout(4, 1));
        box.setSize(550, 300);
        box.setResizable(false);
        box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Make the four major panel areas
        JPanel nameAndLoc = new JPanel(new GridLayout(3, 2));
        JPanel hobbies = new JPanel(new GridLayout(1, 3));
        JPanel education = new JPanel(new FlowLayout());
        JPanel buttons = new JPanel(new FlowLayout());
        
        
        //nameAndLoc JPanel
        JLabel lNameLabel = new JLabel("Last Name: ");
        JLabel fNameLabel = new JLabel("First Name: ");
        JLabel addressLabel = new JLabel("Address: ");
        
        JTextField lNameInput = new JTextField("Enter your last name");
        JTextField fNameInput = new JTextField("Enter your first name");
        JTextField addressInput = new JTextField("Enter you address");
        
        nameAndLoc.add(lNameLabel);
        nameAndLoc.add(lNameInput);
        nameAndLoc.add(fNameLabel);
        nameAndLoc.add(fNameInput);
        nameAndLoc.add(addressLabel);
        nameAndLoc.add(addressInput);
        
        
        //hobbies
        JLabel hobbiesLabel = new JLabel("Hobbies: "); 
        JCheckBox techRelHobbies = new JCheckBox("Technology-related");
        JCheckBox nonTechRelHobbies = new JCheckBox("Not tech-related");
        
        hobbies.add(hobbiesLabel);
        hobbies.add(techRelHobbies);
        hobbies.add(nonTechRelHobbies);
        
        
        //education
        JLabel educationLabel = new JLabel("Education: ");
        JComboBox educationLevel = new JComboBox(new String[]{
            "High School", "2-Year College Degree", "3-Year College Degree",
            "Bachelor's Degree", "Master's Degree", "Ph.D"});
        
        education.add(educationLabel);
        education.add(educationLevel);
        
        
        //buttons
        JButton newButton = new JButton("New");
        JButton saveButton = new JButton("Save");
        JButton cancelButton = new JButton("Cancel");
        JButton exitButton = new JButton("Exit");
       
        // what happens to these buttons if you uncomment?
        // newButton.setPreferredSize(new Dimension(125, 40));
        // saveButton.setPreferredSize(new Dimension(125, 40));
        // cancelButton.setPreferredSize(new Dimension(125, 40));
        // exitButton.setPreferredSize(new Dimension(125, 40));

        buttons.add(newButton);
        buttons.add(saveButton);
        buttons.add(cancelButton);
        buttons.add(exitButton);
        
        
        // now add all of the four main panels with all their
        // individual components to the main JFrame
        box.add(nameAndLoc);
        box.add(hobbies);
        box.add(education);
        box.add(buttons);
        
        // reveal the end product finally!
        box.setVisible(true);
    }
    
}



Was This Post Helpful? 1
  • +
  • -

#7 sewg82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 06 May 2014 - 03:23 AM

Wow, thank you so much for all of that. It really helped to clarify a lot of confusion for me. Quick question though, after reviewing the assignment instructions I've realized that my instructor only wants the GUI to display the inventory information one product at a time, without using the buttons. Apparently I've jumped ahead of myself a bit here. I'm not supposed to add the buttons until part 5. How would I get the inventory information to display product-by-product without having the buttons to navigate through?
Was This Post Helpful? 0
  • +
  • -

#8 AGENT_P6  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 33
  • Joined: 05-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 06 May 2014 - 08:04 AM

View Postsewg82, on 06 May 2014 - 03:23 AM, said:

How would I get the inventory information to display product-by-product without having the buttons to navigate through?


Your teacher likes to leave students guessing...

I don't know if this is what he's getting at but...

If you have learned anything about KeyListener or KeyEvent in the java.awt.event package then I would assume that that is what your prof is asking. If you haven't learned this yet, there are some swing components which, in a way, have built in keyboard navigation. Have a look at this tree and see if there's any JSomethings which look like they might fit the description

Posted Image
Was This Post Helpful? 1
  • +
  • -

#9 sewg82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 06 May 2014 - 09:37 AM

I'm not really sure to be perfectly honest. I read all three of the chapters for the week and for the most part I do see where it talks about creating a GUI, but for some reason it's just not making much sense at all to me. I emailed my instructor asking for help and he told me I can use AWT or Swing for my GUI and that there are examples of both in the book but I don't know if I'm just overlooking it or overthinking it but I'm just stuck.
Was This Post Helpful? 0
  • +
  • -

#10 AGENT_P6  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 33
  • Joined: 05-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 06 May 2014 - 09:57 AM

View Postsewg82, on 06 May 2014 - 09:37 AM, said:

I'm not really sure to be perfectly honest. I read all three of the chapters for the week and for the most part I do see where it talks about creating a GUI, but for some reason it's just not making much sense at all to me. I emailed my instructor asking for help and he told me I can use AWT or Swing for my GUI and that there are examples of both in the book but I don't know if I'm just overlooking it or overthinking it but I'm just stuck.


Well, there are probably a good handful of ways to meet the requirements of what he's asking. I don't want to say though because this is where you need critical and conceptual thinking. As an aspiring programmer, you will need to be able to take a list of somewhat abstract instructions with certain criteria and transform that into a program.

You need to display text which represents data of an object, so begin a process of elimination:

Can a JButton display text? Well...yes...but I don't think your prof wants you to use a JButton to display the data of an inventory record so that can't be right...

Can a JCheckBox display text? Nope, that can't be right...

What about a JPasswordField? Yea but no...and its all asterisks anyway

AbstractButton? No that wouldn't be helpful...

Just go through the tree and cross off the ones that would be absurd to use. If you don't know what one of them is, just do a google image search to quickly see what it is and you'll be able to tell right away.

Honestly, the solutions are so obvious and simple you'll do a facepalm when you figure it out.
Was This Post Helpful? 0
  • +
  • -

#11 sewg82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 06 May 2014 - 10:05 AM

Okay, thanks for your help :smile2:
Was This Post Helpful? 0
  • +
  • -

#12 sewg82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 07 May 2014 - 02:09 PM

I've been working on my program, and so far I've got the majority of it done, but I'm coming up with errors that I'm not exactly 100% sure how to fix. Would anyone mind taking a look over what I've got and help me to fix the errors I've got so my program will run correctly with the GUI please?

This is the code for the main part of the program:

package inventoryprogrampt4;

import java.awt.PopupMenu;

/**
 *
 * @author sgressett
 */
public class InventoryProgramPt4 {
    static int max = 5;
    static final Year[]year = new Year[max];
    private static PopupMenu[] dvdInv;
        
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        year[0] = new Year("The Hunger Games", 4015, 1, 14.99, 2012, 14.99, 0.75);
        year[1] = new Year("Twilight", 2174, 2, 7.99, 2008, 15.98, 0.40);
        year[2] = new Year("The Music Man", 8225, 1, 12.99, 1962, 12.99, 0.65);
        year[3] = new Year("The Other Boleyn Girl", 7453, 1, 13.99, 2008, 13.99, 0.70);
        year[4] = new Year("The Fast & The Furious", 1421, 1, 14.99, 2001, 14.99, 0.75);
        calcinv(year);
        printinv(year);
        sortinv(year);
        printinv(year);    
        
        dvdInv myMovies = new dvdInv();
        for (int x=0; x<max; x++) {
            myMovies.add(dvdInv[x]);
            myMovies.sort();
          new dvdInv(myMovies).setVisible(true);
        }
        
    }
    
    static void printinv(Year[]year) {
        System.out.println();
        
      for (int x=0; x<max; x++) {
          System.out.println("ID: " + year[x].pnumber);
          System.out.println("Name: " + year[x].pname);
          System.out.println("Units: " + year[x].punit);
          System.out.println("Price: $" + year[x].pprice);
          System.out.println("Release Year: " + year[x].movieYear);
          System.out.println("Restocking Fee: $" + year[x].reStockFee);
      }
    }
    
    static void calcinv(Year[]year) {//method calculates total inventory value
        double ivalue = 0;
        
     for (int x=0; x<max; x++) {
         ivalue += year[x].punit + year[x].pprice;
     }
     System.out.println("Total Inventory Value is: $" + ivalue);
    }
    
    static void sortinv(Year[]year) {//method sorts the array
        java.util.Arrays.sort(year);
 
    }
}

class DVD implements Comparable <DVD> {//creation of DVD class
    double punit, pnumber, pprice;
    String pname;
    
    public DVD(String iname, double inumber, double iunit, double iprice) {//defines variables
        pname = iname;
        pnumber = inumber;
        punit = iunit;
        pprice = iprice;
    }
    
    public int compareTo(DVD sortDVD) {
        return this.pname.compareTo(sortDVD.pname);
    }
}

class Year extends DVD {//creation of subclass
    public double movieYear;
    double value;
    double reStockFee;
    
    public Year(String iname, double inumber, double iunit, double iprice, int year,
            double invalue, double RestockFee) {//declaration of new variables created in subclass
        super(iname, inumber, iunit, iprice);
        movieYear = year;
        value = invalue;
        reStockFee = RestockFee;
    }
    
    public void setYear (int year) {//method sets the year
        movieYear = year;
    }
    
    public double getYear() {//method gets the year
        return movieYear;
    }
    
    public void getRestockFee() {//method calculates the restocking fee
        value = super.punit * super.pprice;
        reStockFee = value * 0.05;
    }
}





This is the code for the GUI:

package inventoryprogrampt4;

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

/**
 *
 * @author sgressett
 */
class dvdInv extends JPanel {
    
    public dvdInv() {
         super();
    }
        
    JTextField itemNumber = new JTextField();
    JTextField itemTitle = new JTextField();
    JTextField itemStock = new JTextField();
    JTextField itemPrice = new JTextField();
    JTextField valueOfStock = new JTextField();
    JTextField restockFee = new JTextField();
    JTextField itemYear = new JTextField();
    JTextField totalInv = new JTextField();       
    JButton next = new JButton("Next");
    JButton previous = new JButton("Previous");  
    
    public dvdInv myMovies;
    public int currentIndex = 0;

    public dvdInv(dvdInv inventory) {
   
    this.myMovies = inventory;

    setTitle("DVD Inventory");
    setBounds(600, 400, 0, 50);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    createGUI();
    
    //method called when previous is pressed.  
    previous.addActionListener(new ActionListener()
    {@Override
    public void actionPerformed(ActionEvent e)
        {showPrevious();
    }
});
            
    //method called when next is pressed
    next.addActionListener(new ActionListener()
    {@Override
    public void actionPerformed(ActionEvent e)
    {showNext();
    }
});
    
    updateGUI();
}

public double calcTotalValue() {
    double value = 0;
        for (int i = 0; i < myMovies.size(); i++)
        {value += myMovies.get(i).value();
 }
        return value;
}
private void createGUI() {
            
    JPanel x = new JPanel(new GridLayout(5,0,20,20));
               
        x.add(new JLabel("Item Number:"));
        x.add(itemNumber);

        x.add(new JLabel("Title:"));
        x.add(itemTitle);
      
        x.add(new JLabel("Units In Stock:"));
        x.add(itemStock);

        x.add(new JLabel("Unit Price: $"));
        x.add(itemPrice);

        x.add(new JLabel("Total Value of Units: $"));
        x.add(valueOfStock);

        x.add(new JLabel("Restocking Fee:"));
        x.add(restockFee);
  
        x.add(new JLabel("Release Year:"));
        x.add(itemYear);
        
        x.add(new JLabel("Total Value of Inventory: $"));
        x.add(totalInv);
        
        x.add(previous);
        x.add(next);
        
    JPanel dvdInvPanel = new JPanel();
    dvdInvPanel.add(x);
        setContentPane(dvdInvPanel);
 }

    //method called when next button is pressed
    private void showNext() {
    if (currentIndex < myMovies.size()-1)
    {currentIndex++;
     updateGUI();
    }
}
    
    //method called when previous button is pressed
    private void showPrevious() {
    if (currentIndex > 0)
    { currentIndex--;
      updateGUI();
    }
}
    
    private void updateGUI() {
    dvdInv item = myMovies.get(currentIndex);
    
    itemNumber.setText(String.valueOf(item.getItem()));
    itemTitle.setText(String.valueOf(item.getTitle()));
    itemStock.setText(String.valueOf(item.getUnits()));
    itemPrice.setText(String.format("%8.2f%n", item.getPrice()));
    valueOfStock.setText(String.format("%7.2f", item.value()));
    restockFee.setText(String.format("%8.2f", item.fee()));
    itemYear.setText(item.getYear());
    totalInv.setText(String.format("%8.2f",calcTotalValue()));
    
    }

    void sort() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}



The errors that I'm getting are all coming from the GUI code. There are a total of 14 errors.

Line 36:
cannot find symbol
symbol: method setTitle(String)
location: class dvdInv

Line 38:
cannot find symbol
symbol: method setDefaultCloseOperation(int)
location: class dvdInv

Line 61:
bad operand types for binary operator '<'
first type: int
second type: Dimension

Line 63:
cannot find symbol
symbol: method get(int)
location: variable myMovies of type dvdInv

Line 101:
cannot find symbol
symbol: method setContentPane(JPanel)
location: class dvdInv

Line 106:
bad operand types for binary operator '-'
first type: Dimension
second type: int

Line 121:
cannot find symbol
symbol: method get(int)
location: variable myMovies of type dvdInv

Line 123:
cannot find symbol
symbol: method getItem()
location: variable item of type dvdInv

Line 124:
cannot find symbol
symbol: method getTitle()
location: variable item of type dvdInv

Line 125:
cannot find symbol
symbol: method getUnits()
location: variable item of type dvdInv

Line 126:
cannot find symbol
symbol: method getPrice()
location: variable item of type dvdInv

Line 127:
cannot find symbol
symbol: method value()
location: variable item of type dvdInv

Line 128:
cannot find symbol
symbol: method fee()
location: variable item of type dvdInv

Line 129:
cannot find symbol
symbol: method getYear()
location: variable item of type dvdInv


If anyone could please help me to get all of this straightened out it will be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 624
  • View blog
  • Posts: 2,809
  • Joined: 03-December 12

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 07 May 2014 - 02:40 PM

Where are these defined?

example:
setTitle(String)


Try,
this.setDefaultCloseOperation(int)


Are you sure the methods you are attempting to call are in the class that you are using to call them. What I mean is,

class Animal
{

    function walk(){
        // code
    }

}

class Plant
{
   function grow(){
      // code
   }

}



This will give an error because the walk method is not in that class.
Plant p = new Plant()

p.walk()

This post has been edited by astonecipher: 07 May 2014 - 02:42 PM

Was This Post Helpful? 1
  • +
  • -

#14 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3369
  • View blog
  • Posts: 11,410
  • Joined: 12-December 12

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 07 May 2014 - 02:46 PM

You are extending a JPanel, as has already been mentioned. setTitle and setDefaultCloseOperation are methods of a JFrame.

You can either extend JFrame or construct an instance of your current JPanel and then add it to a JFrame - which will have these methods. (There are other options, but these are the most common.)

This post has been edited by andrewsw: 07 May 2014 - 02:52 PM

Was This Post Helpful? 2
  • +
  • -

#15 sewg82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-May 14

Re: Looking for help with Inventory Program Part 4 - Creating a GUI

Posted 07 May 2014 - 03:20 PM

Thank you both for helping. I changed the whole

class dvdInv extends JPanel {
       }


to this:

class dvdInv extends JFrame {
       }


After doing that, 3 of the errors went away, so now I'm down to 11 errors left.

The errors are on lines 61, 63, 106, 121, and 124-129.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2