•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,471 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,275 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 9617 | Replies: 44
![]() |
•
•
Join Date: Mar 2007
Posts: 25
Reputation:
Rep Power: 2
Solved Threads: 0
I finaly got my buttons to work and now I have added 3 more buttons which are not working and I added them the same way the other buttons that are working were added. the code compiles and runs with no errors but the 3 new buttons are not working the way I need them to. I think that its the action listener that is not workinng right.
here is what I have so far:
here is what I have so far:
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat; // used to format currency
import javax.swing.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class InventoryMain extends Frame implements ActionListener
{
CD[] product;
JTextField[] fields;
NumberFormat nf;
JButton jbtAdd, jbtFirst, jbtPrev, jbtNext, jbtLast, jbtDelete, jbtModify;
JLabel label; // logo
JPanel buttonJPanel; // panel to hold buttons
String nameText;
JComboBox combo;
private int index;
int i; // varialbe for looping
double total = 0; // variable for total inventory
static int dispProd = 0; // variable for actionEvents
static JTextArea textArea;
static int addProd = 0;
static int deleteProd = 0;
static int modifyProd = 0;
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());
}
InventoryMain test = new InventoryMain();
test.initCD();
test.showGUI();
test.populateFields(0);
}
public void initCD() {
//Create an array
product = new CD[10];
//fill in the classes
product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722");
product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263");
product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142");
product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553");
product[4] = new CD( 5, "Down", 24.00, 10, "00789");
product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712");
product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" );
product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889");
product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897");
product[9] = new CD( 10, "Testament" , 15.00, 10, "00656");
//Sort elements in array in alphabetical order by product name
// product[0].sortItems(product);
}
private void showGUI() {
JLabel l;
JButton b;
fields = new JTextField[9];
combo = new JComboBox();
for(int j = 0; j < product.length; j++)
combo.addItem(product[j].getName());
combo.addActionListener(this);
Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
label = new JLabel(logo); // create logo label
label.setToolTipText("Company Logo"); // create tooltip
textArea = new JTextArea(product[3] + "\n"); // create textArea for
// product display
JFrame f = new JFrame("CD Inventory");
Container cp = f.getContentPane();
cp.setLayout(new GridBagLayout());
cp.setBackground(UIManager.getColor("control"));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = GridBagConstraints.RELATIVE;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(2, 2, 2, 2);
c.anchor = GridBagConstraints.EAST;
cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('a');
cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('b');
cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('c');
cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('d');
cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('e');
cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $",
SwingConstants.CENTER), c); l.setDisplayedMnemonic('f');
cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('g');
cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('h');
cp.add(combo, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
cp.add(fields[0] = new JTextField(), c);
fields[0].setFocusAccelerator('a');
c.gridx = 1;
c.gridy = GridBagConstraints.RELATIVE;
cp.add(fields[1] = new JTextField(), c); fields[1].setFocusAccelerator('b');
cp.add(fields[2] = new JTextField(), c); fields[2].setFocusAccelerator('c');
cp.add(fields[3] = new JTextField(), c); fields[3].setFocusAccelerator('d');
cp.add(fields[4] = new JTextField(), c); fields[4].setFocusAccelerator('e');
cp.add(fields[5] = new JTextField(), c); fields[5].setFocusAccelerator('f');
cp.add(fields[6] = new JTextField(), c); fields[6].setFocusAccelerator('g');
cp.add(fields[7] = new JTextField(), c); fields[7].setFocusAccelerator('h');
cp.add(fields[8] = new JTextField(), c); fields[8].setFocusAccelerator('i');
c.weightx = 0.0;
c.fill = GridBagConstraints.NONE;
jbtAdd = new JButton("Add");
jbtAdd.addActionListener(this);
jbtFirst = new JButton("First");
jbtFirst.addActionListener(this);
jbtPrev = new JButton("Previous");
jbtPrev.addActionListener(this);
jbtNext = new JButton("Next");
jbtNext.addActionListener(this);
jbtLast = new JButton("Last");
jbtLast.addActionListener(this);
jbtDelete = new JButton("Delete");
jbtDelete.addActionListener(this);
jbtModify = new JButton("Modify");
jbtModify.addActionListener(this);
cp.add(jbtAdd, c);
cp.add(jbtFirst, c);
cp.add(jbtPrev, c);
cp.add(jbtNext, c);
cp.add(jbtLast, c);
cp.add(jbtDelete, c);
cp.add(jbtModify, c);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
// assign actionListener and actionEvent for each button
jbtAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
addProd = 0;
populateFields(dispProd);
} // end addBtn actionEvent
}); // end addBtn actionListener
jbtFirst.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd = 0;
populateFields(dispProd);
} // end firstBtn actionEvent
}); // end firstBtn actionListener
jbtPrev.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd--;
if (dispProd < 0)
{
dispProd = product.length - 1;
}
populateFields(dispProd);
} // end prevBtn actionEvent
}); // end prevBtn actionListener
jbtLast.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd = product.length - 1;
populateFields(dispProd);
} // end lastBtn actionEvent
}); // end lastBtn actionListener
jbtNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd++;
if (dispProd >= product.length - 1)
{
dispProd = 0;
}
populateFields(dispProd);
} // end nextBtn actionEvent
}); // end nextBtn actionListener
jbtDelete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
deleteProd = 0;
} // end deleteBtn actionEvent
}); // end deleteBtn actionListener
jbtModify.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
modifyProd = 0;
populateFields(dispProd);
} // end modifyBtn actionEvent
}); // end modifyBtn actionListener
}
// event handler for combo box
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == combo)
{
System.out.println("Selected option: " + combo.getSelectedIndex() );
}
}
private void populateFields(int index) {
CD cd = product[index];
fields[0].setText(Long.toString(cd.getNumberCode()));
fields[1].setText(cd.getName());
fields[2].setText(Long.toString(cd.getUnits()));
fields[3].setText(Double.toString(cd.getPrice()));
fields[4].setText(Double.toString(cd.getSum()));
fields[5].setText(Double.toString(cd.totalAllInventory(product)));
fields[6].setText(cd.getCode());
fields[7].setText(Double.toString(cd.getSum()*.05));
}
}public class InventoryI {
String productnumber;
String name;
String Text;
int numberofunits;
double priceperunit;
String itemcode;
String command;
// Create a new instance of Inventory
// main constructor for the class
public InventoryI(String Item_Number, String Item_Name, int Items_in_Stock,
double Item_Price, String Item_Code) {
productnumber = Item_Number;
name = Item_Name;
numberofunits = Items_in_Stock;
priceperunit = Item_Price;
itemcode = Item_Code;
}
public void setItemName(String Item_Name) {
// sets the items name
name = Item_Name;
}
public void setItemCode(String Item_Code) {
// sets the items name
itemcode = Item_Code;
}
public void setItemNumber(String Item_Number) { // Sets the Product =Number
// for the item
productnumber = Item_Number;
}
public void setItemsInStock(int Items_in_Stock) { // sets the =number of
// units in stock
numberofunits = Items_in_Stock;
}
public void setItemPrice(double Item_Price) { // sets the price of =the
// item
priceperunit = Item_Price;
}
public String getItemName() { // returns the Product Name of this item
return name;
}
public String getItemCode() { // returns the Product Name of this item
return itemcode;
}
public String getItemNumber() { // returns the Product Number of the =item
return productnumber;
}
public int getItemsInStock() { // returns how many units are in stock
return numberofunits;
}
public double getItemPrice() { // returns the price of the item
return priceperunit;
}
public double getInventoryIValue() { // returns the total value of =the stock
// for this item
return priceperunit * numberofunits;
}
public static double getTotalValueOfAllInventory(InventoryI [] inv) {
double tot = 0.0;
for(int i = 0; i < inv.length; i++)
tot += inv[i].getInventoryIValue();
return tot;
}
public static InventoryI[] sort(InventoryI [] inventory)
{
InventoryI temp[] = new InventoryI[1];
//sorting the array using Bubble Sort
for(int j = 0; j < inventory.length - 1; j++)
{
for(int k = 0; k < inventory.length - 1; k++)
{
if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0)
{
temp[0] = inventory[k];
inventory[k] = inventory[k+1];
inventory[k+1] = temp[0];
}//end if
}//end for loop
}//end for loop
return inventory;
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("CD Title: \t").append(name).append("\n");
sb.append("Item Code: \t").append(itemcode).append("\n");
sb.append("Item #: \t").append(productnumber).append("\n");
sb.append("Number in stock:\t").append(numberofunits).append("\n");
sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit));
sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryIValue()));
return sb.toString();
}
} See comments bolded in the code:
•
•
•
•
I finaly got my buttons to work and now I have added 3 more buttons which are not working and I added them the same way the other buttons that are working were added. the code compiles and runs with no errors but the 3 new buttons are not working the way I need them to. I think that its the action listener that is not workinng right.
here is what I have so far:
import java.awt.*; import java.awt.event.*; import java.text.NumberFormat; // used to format currency import javax.swing.*; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JComboBox; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; class InventoryMain extends Frame implements ActionListener { CD[] product; JTextField[] fields; NumberFormat nf; JButton jbtAdd, jbtFirst, jbtPrev, jbtNext, jbtLast, jbtDelete, jbtModify; JLabel label; // logo JPanel buttonJPanel; // panel to hold buttons String nameText; JComboBox combo; private int index; int i; // varialbe for looping double total = 0; // variable for total inventory static int dispProd = 0; // variable for actionEvents static JTextArea textArea; static int addProd = 0; static int deleteProd = 0; static int modifyProd = 0; 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()); } InventoryMain test = new InventoryMain(); test.initCD(); test.showGUI(); test.populateFields(0); } public void initCD() { //Create an array product = new CD[10]; //fill in the classes product[0] = new CD( 1, "Crowbar" , 20.00, 10, "00722"); product[1] = new CD( 2, "Pantera" , 20.00, 10, "00263"); product[2] = new CD( 3, "Ozzy" , 15.00, 10, "00142"); product[3] = new CD( 4, "Soulfly" , 18.00, 10, "00553"); product[4] = new CD( 5, "Down", 24.00, 10, "00789"); product[5] = new CD( 6, "God Forbid" , 10.00, 10, "00712"); product[6] = new CD( 7, "Black Label Society" , 16.00, 10, "00458" ); product[7] = new CD( 8, "Saint Vitus" , 15.00, 10, "00889"); product[8] = new CD( 9, "Clearlight" , 15.00, 10, "00897"); product[9] = new CD( 10, "Testament" , 15.00, 10, "00656"); //Sort elements in array in alphabetical order by product name // product[0].sortItems(product); } private void showGUI() { JLabel l; JButton b; fields = new JTextField[9]; combo = new JComboBox(); for(int j = 0; j < product.length; j++) combo.addItem(product[j].getName()); combo.addActionListener(this); Icon logo = new ImageIcon("C:/logo.jpg"); // load logo label = new JLabel(logo); // create logo label label.setToolTipText("Company Logo"); // create tooltip textArea = new JTextArea(product[3] + "\n"); // create textArea for // product display JFrame f = new JFrame("CD Inventory"); Container cp = f.getContentPane(); cp.setLayout(new GridBagLayout()); cp.setBackground(UIManager.getColor("control")); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = GridBagConstraints.RELATIVE; c.gridwidth = 1; c.gridheight = 1; c.insets = new Insets(2, 2, 2, 2); c.anchor = GridBagConstraints.EAST; cp.add(l = new JLabel("Item Number:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('a'); cp.add(l = new JLabel("Item Name:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('b'); cp.add(l = new JLabel("Number of Units in Stock:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('c'); cp.add(l = new JLabel("Price per Unit: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('d'); cp.add(l = new JLabel("Total cost of This Item: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('e'); cp.add(l = new JLabel("Total Value of All Merchandise in Inventory: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('f'); cp.add(l = new JLabel("Item Code:", SwingConstants.CENTER), c); l.setDisplayedMnemonic('g'); cp.add(l = new JLabel("Product Restocking Fee: $", SwingConstants.CENTER), c); l.setDisplayedMnemonic('h'); cp.add(combo, c); c.gridx = 1; c.gridy = 0; c.weightx = 1.0; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; cp.add(fields[0] = new JTextField(), c); fields[0].setFocusAccelerator('a'); c.gridx = 1; c.gridy = GridBagConstraints.RELATIVE; cp.add(fields[1] = new JTextField(), c); fields[1].setFocusAccelerator('b'); cp.add(fields[2] = new JTextField(), c); fields[2].setFocusAccelerator('c'); cp.add(fields[3] = new JTextField(), c); fields[3].setFocusAccelerator('d'); cp.add(fields[4] = new JTextField(), c); fields[4].setFocusAccelerator('e'); cp.add(fields[5] = new JTextField(), c); fields[5].setFocusAccelerator('f'); cp.add(fields[6] = new JTextField(), c); fields[6].setFocusAccelerator('g'); cp.add(fields[7] = new JTextField(), c); fields[7].setFocusAccelerator('h'); cp.add(fields[8] = new JTextField(), c); fields[8].setFocusAccelerator('i'); c.weightx = 0.0; c.fill = GridBagConstraints.NONE; jbtAdd = new JButton("Add"); jbtAdd.addActionListener(this); // None of these statements are needed. You are essentially adding your combobox listener to these buttons. jbtFirst = new JButton("First"); jbtFirst.addActionListener(this); jbtPrev = new JButton("Previous"); jbtPrev.addActionListener(this); jbtNext = new JButton("Next"); jbtNext.addActionListener(this); jbtLast = new JButton("Last"); jbtLast.addActionListener(this); jbtDelete = new JButton("Delete"); jbtDelete.addActionListener(this); jbtModify = new JButton("Modify"); jbtModify.addActionListener(this); cp.add(jbtAdd, c); cp.add(jbtFirst, c); cp.add(jbtPrev, c); cp.add(jbtNext, c); cp.add(jbtLast, c); cp.add(jbtDelete, c); cp.add(jbtModify, c); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); // assign actionListener and actionEvent for each button jbtAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { addProd = 0; // this isn't really doing anything at all // you need to add a new object to your array here populateFields(dispProd); } // end addBtn actionEvent }); // end addBtn actionListener jbtFirst.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { dispProd = 0; populateFields(dispProd); } // end firstBtn actionEvent }); // end firstBtn actionListener jbtPrev.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { dispProd--; if (dispProd < 0) { dispProd = product.length - 1; } populateFields(dispProd); } // end prevBtn actionEvent }); // end prevBtn actionListener jbtLast.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { dispProd = product.length - 1; populateFields(dispProd); } // end lastBtn actionEvent }); // end lastBtn actionListener jbtNext.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { dispProd++; if (dispProd >= product.length - 1) // should just be >, not >=. You are missing the last item in the array with >= (since that is the index of the last item and is a valid entry) { dispProd = 0; } populateFields(dispProd); } // end nextBtn actionEvent }); // end nextBtn actionListener jbtDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { deleteProd = 0; // this isn't doing anything either // you need to remove an object from the array } // end deleteBtn actionEvent }); // end deleteBtn actionListener jbtModify.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { modifyProd = 0; // again nothing here // needs to update the values on the object that is in this position populateFields(dispProd); } // end modifyBtn actionEvent }); // end modifyBtn actionListener } // event handler for combo box public void actionPerformed(ActionEvent e) { if(e.getSource() == combo) { System.out.println("Selected option: " + combo.getSelectedIndex() ); } } private void populateFields(int index) { CD cd = product[index]; fields[0].setText(Long.toString(cd.getNumberCode())); fields[1].setText(cd.getName()); fields[2].setText(Long.toString(cd.getUnits())); fields[3].setText(Double.toString(cd.getPrice())); fields[4].setText(Double.toString(cd.getSum())); fields[5].setText(Double.toString(cd.totalAllInventory(product))); fields[6].setText(cd.getCode()); fields[7].setText(Double.toString(cd.getSum()*.05)); } }
public class InventoryI { String productnumber; String name; String Text; int numberofunits; double priceperunit; String itemcode; String command; // Create a new instance of Inventory // main constructor for the class public InventoryI(String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price, String Item_Code) { productnumber = Item_Number; name = Item_Name; numberofunits = Items_in_Stock; priceperunit = Item_Price; itemcode = Item_Code; } public void setItemName(String Item_Name) { // sets the items name name = Item_Name; } public void setItemCode(String Item_Code) { // sets the items name itemcode = Item_Code; } public void setItemNumber(String Item_Number) { // Sets the Product =Number // for the item productnumber = Item_Number; } public void setItemsInStock(int Items_in_Stock) { // sets the =number of // units in stock numberofunits = Items_in_Stock; } public void setItemPrice(double Item_Price) { // sets the price of =the // item priceperunit = Item_Price; } public String getItemName() { // returns the Product Name of this item return name; } public String getItemCode() { // returns the Product Name of this item return itemcode; } public String getItemNumber() { // returns the Product Number of the =item return productnumber; } public int getItemsInStock() { // returns how many units are in stock return numberofunits; } public double getItemPrice() { // returns the price of the item return priceperunit; } public double getInventoryIValue() { // returns the total value of =the stock // for this item return priceperunit * numberofunits; } public static double getTotalValueOfAllInventory(InventoryI [] inv) { double tot = 0.0; for(int i = 0; i < inv.length; i++) tot += inv[i].getInventoryIValue(); return tot; } public static InventoryI[] sort(InventoryI [] inventory) { InventoryI temp[] = new InventoryI[1]; //sorting the array using Bubble Sort for(int j = 0; j < inventory.length - 1; j++) { for(int k = 0; k < inventory.length - 1; k++) { if(inventory[k].getItemName().compareToIgnoreCase(inventory[k+1].getItemName()) > 0) { temp[0] = inventory[k]; inventory[k] = inventory[k+1]; inventory[k+1] = temp[0]; }//end if }//end for loop }//end for loop return inventory; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("CD Title: \t").append(name).append("\n"); sb.append("Item Code: \t").append(itemcode).append("\n"); sb.append("Item #: \t").append(productnumber).append("\n"); sb.append("Number in stock:\t").append(numberofunits).append("\n"); sb.append("Price: \t").append(String.format("$%.2f%n", priceperunit)); sb.append("Inventory Value:\t").append(String.format("$%.2f%n", this.getInventoryIValue())); return sb.toString(); } }
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,458
Reputation:
Rep Power: 11
Solved Threads: 296
Well spotted adding of Action Listeners is my mistake, I did not take it out when I edited code last time.I'm used to top level event handling insted of dkdeleon68 anonymous...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
•
•
Well spotted adding of Action Listeners is my mistake, I did not take it out when I edited code last time.I'm used to top level event handling insted of dkdeleon68 anonymous...
Well, he can do it either way: with the single listener that checks the source of the ActionEvent and operates accordingly, or with a separate listener for each component - but having both is probably causing the confusion right now. It's probably best to pick one way and stick with it at this point
•
•
Join Date: Mar 2007
Posts: 25
Reputation:
Rep Power: 2
Solved Threads: 0
still not working right.
here is the part of code that I changed:
here is the part of code that I changed:
// assign actionListener and actionEvent for each button
jbtAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd = (product.length - 0);
populateFields(dispProd);
} // end addBtn actionEvent
}); // end addBtn actionListener
jbtFirst.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd = 0;
populateFields(dispProd);
} // end firstBtn actionEvent
}); // end firstBtn actionListener
jbtPrev.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd--;
if (dispProd < 0)
{
dispProd = product.length - 1;
}
populateFields(dispProd);
} // end prevBtn actionEvent
}); // end prevBtn actionListener
jbtLast.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd = product.length - 1;
populateFields(dispProd);
} // end lastBtn actionEvent
}); // end lastBtn actionListener
jbtNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd++;
if (dispProd > product.length - 1)
{
dispProd = 0;
}
populateFields(dispProd);
} // end nextBtn actionEvent
}); // end nextBtn actionListener
jbtDelete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
dispProd = 0;
} // end deleteBtn actionEvent
}); // end deleteBtn actionListener
jbtModify.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
for(int j = 0; j < product.length; j++)
populateFields(dispProd);
} // end modifyBtn actionEvent
}); // end modifyBtn actionListener
}•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,458
Reputation:
Rep Power: 11
Solved Threads: 296
dkdeleon68 you better pay more attention in classes...
Ofcourse it will do nothing, because in this case you are supposed to read value/values from your JTextField and update selected CD with new data. However what you do is just updating view, notging more. Same will go for your delete function.
WHAT TO DO ! ! !
Create method which will handle delete process and update process
Ofcourse it will do nothing, because in this case you are supposed to read value/values from your JTextField and update selected CD with new data. However what you do is just updating view, notging more. Same will go for your delete function.
WHAT TO DO ! ! !
Create method which will handle delete process and update process
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
Join Date: Mar 2007
Posts: 25
Reputation:
Rep Power: 2
Solved Threads: 0
I have been paying attention, its just that I am not getting feedback from my instructor. I have learned more from you guys in the past couple of days than I have learned this hole class. thank you very much it is very appreciated.
Last edited by dkdeleon68 : Sep 14th, 2007 at 8:21 am. Reason: misspelled words
Ok, think about the operations that need to be performed to add, modify, and delete:
Add: Create a new product object and place it in your array. Since your array is currently full, you will have to make it larger.
Modify: The easiest. Just replace the values of the current product object with the values from your fields.
Delete: Remove an object from your array. There are a couple of ways to handle this, but probably easiest to shift all objects in positions higher than current index down and make the empty position at the end = null. Deleting things in this way will leave nulls in your array, which your navigation operations will probably need to check for and skip over.
I don't know if your classes have gotten to collections yet, but the things you are doing are easier to implement with a List. Look at the javadocs for ArrayList (or Vector would work too) and this tutorial is a very basic example of using it: http://www.anyexample.com/programmin...st_example.xml
Add: Create a new product object and place it in your array. Since your array is currently full, you will have to make it larger.
Modify: The easiest. Just replace the values of the current product object with the values from your fields.
Delete: Remove an object from your array. There are a couple of ways to handle this, but probably easiest to shift all objects in positions higher than current index down and make the empty position at the end = null. Deleting things in this way will leave nulls in your array, which your navigation operations will probably need to check for and skip over.
I don't know if your classes have gotten to collections yet, but the things you are doing are easier to implement with a List. Look at the javadocs for ArrayList (or Vector would work too) and this tutorial is a very basic example of using it: http://www.anyexample.com/programmin...st_example.xml
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- PLEASE HELP about Using C# to add an executable file to my program! (C#)
- help with counting letters part of program (C++)
- VB Programming - Inventory Program (Visual Basic 4 / 5 / 6)
- WinXP installation program (Windows NT / 2000 / XP / 2003)
- Need help with this conversion program (C++)
- help writing a grep program (C)
- Moving IE to a different Program Folder (Windows NT / 2000 / XP / 2003)
- how can i make the program depend on time (C)
- cant remove program from ad and remove programs (Windows NT / 2000 / XP / 2003)
Other Threads in the Java Forum
- Previous Thread: Inventory part 6
- Next Thread: Pls Help Me!



Linear Mode