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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3524
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

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

Posted 07 May 2014 - 03:33 PM

Quote

The errors are on lines 61, 63, 106, 121, and 124-129.

What are the errors - you should always provide them - and are these the same line numbers in your posted code?

But you cannot just change extends JPanel to extends JFrame and expect it to work. You need to review and revise the code.
Was This Post Helpful? 1
  • +
  • -

#17 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:46 PM

Here is the updated code:

package inventoryprogrampt4;

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

/**
 *
 * @author sgressett
 */
class dvdInv extends JFrame {
    
    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 I'm still getting are:

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

Line 64:
cannot find symbol
symbol: method get(int)
location: variable myMovies of type 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 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
Was This Post Helpful? 0
  • +
  • -

#18 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3524
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

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

Posted 07 May 2014 - 04:00 PM

myMovies is an instance of your class dvdInv which extends JFrame. Neither your class nor a JFrame have any of the methods you are attempting to use. (I'm not sure why the error messages use the term Dimension.)

BTW Class-names should begin with a capital letter.
Was This Post Helpful? 1
  • +
  • -

#19 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3524
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

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

Posted 07 May 2014 - 04:08 PM

"cannot find symbol" means, as far as the compiler is concerned, "this does not exist".

It means either:

The method (or member) does not exist,
The method call has been mis-spelt,
The parameters do not match a method-signature, or
The method cannot be found in the current scope.

(It also occurs if a variable is used without being declared.)

This post has been edited by andrewsw: 07 May 2014 - 04:15 PM

Was This Post Helpful? 1
  • +
  • -

#20 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 - 04:27 PM

Okay, thank you for pointing me in the right direction.
Was This Post Helpful? 0
  • +
  • -

#21 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3524
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

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

Posted 07 May 2014 - 04:37 PM

No problem, but I think you should step away from your assignment for a while and go through one of the tutorials linked to earlier. If you have already done this then you might go through it again, or a different one.

Good luck.
Was This Post Helpful? 1
  • +
  • -

#22 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 10 May 2014 - 09:11 PM

Thank you all for helping me with this program before. I have made a bit of progress with it, however now I am coming up with 5 errors, all of which are the same error and I'm not sure how to fix it. If someone could please take a look at what I have and help me out, I would really appreciate it.


This is the main code:

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);
        
        DvdGUI gui = new DvdGUI();
        gui.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;
    String textArea;
    
    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 my GUI:

package inventoryprogrampt4;

import static inventoryprogrampt4.InventoryProgramPt4.calcinv;
import static inventoryprogrampt4.InventoryProgramPt4.printinv;
import static inventoryprogrampt4.InventoryProgramPt4.sortinv;
import javax.swing.*;

/**
 *
 * @author sgressett
 */
public class DvdGUI extends JFrame {
    static int max = 5;
    static final Year[]year = new Year[max];
    int counter;
    /**
     * Creates new form DvdGUI
     */
    public DvdGUI() {
        super();
        this.counter = - 1;
        JFrame gui = new JFrame();
        
        initComponents();
        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);
        gui.add(textArea);
    }
    
    static void textArea() {
        JTextArea textArea = new JTextArea(year[max].textArea);
        textArea.setVisible(true);
}

    /**
     * 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() {

        nextButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        titleLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        nextButton.setText("Next");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Close Program");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        titleLabel.setText("DVD Inventory Program");

        textArea.setEditable(false);
        textArea.setColumns(20);
        textArea.setRows(5);
        jScrollPane1.setViewportView(textArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(backButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                                .addComponent(closeButton))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(titleLabel)))
                        .addGap(85, 85, 85)
                        .addComponent(nextButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addGap(9, 9, 9)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nextButton)
                    .addComponent(closeButton)
                    .addComponent(backButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        System.exit(0); 
    }                                           

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if (counter < 4) {
            counter = counter + 1;
        }else {
            counter = 0;
        }
        
     textArea.setText(year[counter].pnumber);
     textArea.setText(year[counter].pname);
     textArea.setText(year[counter].pprice);
     textArea.setText(year[counter].punit);
     textArea.setText(year[counter].reStockFee);
     textArea.setText(year[counter].value);
         
        
    }                                          
    //this section noted out for Week 8 assignment.
    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
      //  if (counter > 0) {
           // counter = counter - 1;
       // }else {
         //   counter = 0;
        //}
    }                                          

    /**
     * @param args the command line arguments
     */
    public void main(String args[]) {
        
        
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DvdGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DvdGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DvdGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DvdGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
                new DvdGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton backButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nextButton;
    private javax.swing.JTextArea textArea;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration                   
}




The errors are on lines 138, 140, 141, 142, and 143 and when I hover over them I get the following message:

incompatible types: double cannot be converted to String


I'm not sure where I'm making my mistake, but I would really appreciate it if someone could help me to fix this so I can get the assignment turned in.

Thanks :bigsmile:/>
Was This Post Helpful? 0
  • +
  • -

#23 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 705
  • View blog
  • Posts: 3,034
  • Joined: 03-December 12

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

Posted 11 May 2014 - 06:18 AM

You can convert those doubles to strings for display using

Tostring(yourDouble)
Was This Post Helpful? 1
  • +
  • -

#24 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 629
  • Joined: 24-April 10

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

Posted 11 May 2014 - 06:34 AM

138     textArea.setText(year[counter].pnumber);
140     textArea.setText(year[counter].pprice);
141     textArea.setText(year[counter].punit);
142     textArea.setText(year[counter].reStockFee);
143     textArea.setText(year[counter].value);


These 4 lines, the values inside the ( ) are double values. E.g: year[counter].pprice is a double. setText requires a String, so you have the problem where you require a String but you are supplying a Double. A quick hack way around it, which I don't recommend is to append an empty string:
138     textArea.setText(year[counter].pnumber + "");

a more correct way is to use the toString method of whatever the value is:
Double.toString(655.568);
Integer.toString(6);

This post has been edited by mike73: 11 May 2014 - 06:35 AM

Was This Post Helpful? 1
  • +
  • -

#25 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 11 May 2014 - 07:38 AM

View Postmike73, on 11 May 2014 - 06:34 AM, said:

A quick hack way around it, which I don't recommend is to append an empty string:
138     textArea.setText(year[counter].pnumber + "");


I wonder if this would actually perform better
Was This Post Helpful? 1
  • +
  • -

#26 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 11 May 2014 - 08:56 AM

Thank you all for your suggestions. I managed to get it fixed and the program runs fine now without any errors. However for the next part of the assignment (which is actually part 5 of the program), I'm supposed to add a "company logo" to the GUI, but I'm not sure how to do that. I read about using ImageIcon, but it's still a little confusing to me. How would I go about adding an image to the GUI, and also where does the image file need to be located in order for me to add it? Does it have to be saved in a specific file or not?

Thanks again for all the help!

~Susan
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2