Can someone look at my code and see if anything stands out that I might try correcting.
First, I am getting
Inventory2.java:187: actionPerformed(java.awt.event.ActionEvent) in cannot implement actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; attempting to assign weaker access privileges; was public
final void actionPerformed(ActionEvent evt)
and have no idea what this means.
Second, I am getting
Inventory2.java:189: local variable currentCD is accessed from within inner class; needs to be declared final
valueField.setValue("$"+new Float(currentCD.getValue()));
and although I have some idea as to what this means, I have NO idea how to remedy it. Possibly the placement of my cast to the sub class, but again, I am unsure of any action to take.
//Instantiate object for JList
CdwArtist currentCD = (CdwArtist) listModel.get( currCD );
The class I am working in is as follows: (There are two subclasses if anyone would like to see them, but I do not think they apply here.)
Any help at all would be welcome.
Expand|Select|Wrap|Line Numbers
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.text.*;
- import java.lang.*;
- public class Inventory2 extends JFrame
- {
- private JLabel cdNameLabel; // name label
- private JLabel artistLabel; // item number label
- private JLabel nstockLabel; // units in stock label
- private JLabel priceLabel; // price each label
- private JLabel itemLabel; // item number label
- private JLabel valueLabel; // value of that item label
- private JLabel rstkLabel; // cost to restock label
- private JLabel totalLabel; // total value of inventory label
- private JTextField cdNameField; // name display
- private JTextField artistField; // artist display
- private JFormattedTextField nstockField; // units in stock display
- private JFormattedTextField priceField; // price each display
- private JTextField itemField; // item number display
- private JFormattedTextField valueField; // value of that item display
- private JFormattedTextField rstkField; // cost to restock display
- private JFormattedTextField totalField; // value of all inventory combine
- private NumberFormat nstockFormat; // format field and parse numbers
- private NumberFormat priceFormat; // format field and parse numbers
- private NumberFormat valueFormat; // format field and parse numbers
- private NumberFormat rstkFormat; // format field and parse numbers
- private NumberFormat totalFormat;
- private JButton btnAdd; // first button
- private JButton btnPrev; // previous button
- private JButton btnNext; // next button
- private JButton btnDel; // last button
- private JPanel buttonJPanel; // JPanle to hold buttons
- private JPanel fieldJPanel; // JPanel to hold labels and displays
- private JPanel fontJPanel; // JPanel to display logo
- private int currCD; // current product display to use for button action
- private int i; // iterator
- private double total = 0; // variable for total inventory
- private JList Inventorylist; // JList to take place of old array
- private DefaultListModel listModel;
- private JScrollPane jScrollPanel;
- public Inventory2() // create class and method to perform GUI build
- {
- initComponents();
- }
- private void initComponents()
- {
- // create label names
- cdNameLabel = new JLabel("CD Name:");
- artistLabel = new JLabel("Artist:");
- nstockLabel = new JLabel("In Stock:");
- priceLabel = new JLabel("Each Item Cost:$");
- itemLabel = new JLabel("Item Number:");
- valueLabel = new JLabel("Value of Item Inventory:$");
- rstkLabel = new JLabel("Cost to Re-Stock Item:$");
- totalLabel = new JLabel("Total Value of Inventory:$");
- // initial fields
- cdNameField = new JTextField(25);
- cdNameField.setEditable(true);
- artistField = new JTextField(15);
- artistField.setEditable(true);
- nstockField = new JFormattedTextField(nstockFormat);
- nstockField.setEditable(true);
- nstockField.setColumns(10);
- priceField = new JFormattedTextField(priceFormat);
- priceField.setEditable(true);
- priceField.setColumns(10);
- itemField = new JTextField(5);
- itemField.setEditable(true);
- valueField = new JFormattedTextField(valueFormat);
- valueField.setEditable(true);
- valueField.setColumns(10);
- rstkField = new JFormattedTextField(rstkFormat);
- rstkField.setEditable(true);
- rstkField.setColumns(5);
- totalField = new JFormattedTextField(totalFormat);
- totalField.setColumns (10);
- totalField.setEditable(true);
- // JList
- jScrollPanel = new JScrollPane();
- Inventorylist = new JList();
- //Instantiate object for JList
- CdwArtist currentCD = (CdwArtist) listModel.get( currCD );
- // add values to textFields
- // cdNameField.setText(currentCD.getcdName());
- // artistField.setText(currentCD.getArtist());
- // nstockField.setValue(new Float(currentCD.getValue()));
- // priceField.setValue(new Float("$"+curentrCD.getPrice()));
- // itemField.setText(currCD.getItem());
- // valueField.setValue(new Float("$" + currentCD.getValue()));
- // rstkField.setValue(new Float(currentCD.getRestock()));
- // totalField.setValue(new Float("$" + currentCD.getValue()));
- // buttons
- btnAdd = new JButton();
- btnNext = new JButton();
- btnPrev = new JButton();
- btnDel = new JButton();
- getContentPane().setLayout(new FlowLayout());
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- // place textFields and labels
- //artist
- artistLabel.setText("Artist");
- getContentPane().add(artistLabel);
- artistField.setMinimumSize(new Dimension(75,19));
- artistField.setPreferredSize(new Dimension(75,19));
- getContentPane().add(artistField);
- // cd name
- cdNameLabel.setText("CD Name");
- getContentPane().add(cdNameLabel);
- cdNameField.setMinimumSize(new Dimension(75,19));
- cdNameField.setPreferredSize(new Dimension(75,19));
- getContentPane().add(cdNameField);
- // copies in stock
- nstockLabel.setText("Copies In Stock");
- getContentPane().add(nstockLabel);
- nstockField.setMinimumSize(new Dimension(75,19));
- nstockField.setPreferredSize(new Dimension(75,19));
- getContentPane().add(nstockField);
- //price of cd
- priceLabel.setText("Price");
- getContentPane().add(priceLabel);
- priceField.setMinimumSize(new Dimension(75,19));
- priceField.setPreferredSize(new Dimension(75,19));
- getContentPane().add(priceField);
- //item number of cd
- itemLabel.setText("Item Number");
- getContentPane().add(itemLabel);
- itemField.setMinimumSize(new Dimension(75,19));
- itemField.setPreferredSize(new Dimension(75,19));
- getContentPane().add(itemField);;
- // value of individual cd in stock
- valueLabel.setText("Value");
- getContentPane().add(valueLabel);
- valueField.setMinimumSize(new Dimension(75,19));
- valueField.setPreferredSize(new Dimension(75,19));
- getContentPane().add(valueField);
- // restocking fee
- rstkLabel.setText("Restock Fee");
- getContentPane().add(rstkLabel);
- rstkField.setMinimumSize(new Dimension(75,19));
- rstkField.setPreferredSize(new Dimension(75,19));
- getContentPane().add(rstkField);
- // total value of inventory
- totalLabel.setText("Total Inventory Value");
- getContentPane().add(totalLabel);
- totalField.setMinimumSize(new Dimension(75,19));
- totalField.setPreferredSize(new Dimension(75,19));
- getContentPane().add(totalField);
- //ADD listeners
- // listener to perform calc upon leaving the price field
- priceField.addActionListener(new ActionListener()
- {
- final void actionPerformed(ActionEvent evt)
- {
- valueField.setValue("$"+new Float(currentCD.getValue()));
- }
- });
- btnAdd.setText("Add");
- btnAdd.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- btnAddActionPerformed(evt);
- }
- });
- getContentPane().add(btnAdd);
- // PREVIOUS
- btnPrev.setText("Previous");
- btnPrev.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- btnAddActionPerformed(evt);
- }
- });
- getContentPane().add(btnPrev);
- // NEXT
- btnNext.setText("Next"); btnNext.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- btnAddActionPerformed(evt);
- }
- });
- getContentPane().add(btnNext);
- // DELETE
- btnDel.setText("Delete");
- btnDel.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- btnAddActionPerformed(evt);
- }
- });
- getContentPane().add(btnDel);
- // new Jlist model
- listModel = new DefaultListModel();
- Inventorylist.setModel(listModel);
- jScrollPanel.setViewportView(Inventorylist);
- getContentPane().add(jScrollPanel);
- pack();
- }// close
- private void btnAddActionPerformed(ActionEvent evt)
- {
- // Create cd to add
- CdwArtist cd = new CdwArtist(artistField.getText());
- cd.setName(cdNameField.getText());
- cd.setItemno(Integer.parseInt(itemField.getText()));
- cd.setNstock(Integer.parseInt(nstockField.getText()));
- cd.setPrice(Float.parseFloat(priceField.getText()));
- cd.setValue(Float.parseFloat(valueField.getText()));
- cd.setTotal(Float.parseFloat(totalField.getText()));
- cd.setRestock(Float.parseFloat(rstkField.getText()));
- // Add cd to list
- listModel.addElement(cd);
- // Clear the text fields after add
- artistField.setText(null);
- cdNameField.setText(null);
- itemField.setText(null);
- nstockField.setText(null);
- priceField.setText(null);
- valueField.setText(null);
- rstkField.setText(null);
- totalField.setText(null);
- }// end ADD
- // run it
- public static void main(String args[])
- {
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- new Inventory2().setVisible(true);
- }
- });
- }
- } // close clas