Results 1 to 9 of 9
Thread: adding GUI to Inventory
- 01-23-2009, 03:40 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
adding GUI to Inventory
I am trying to add a GUI to my Inventory Program but keep getting errors. Can someone look at it and tell me what I am doing wrong. This assignment is due tomorrow.
Here is my code:
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
// InventoryProgram.java
// Tammy Frownfelter - Inventory Part 4
// import java.util.Scanner;
// import java.util.Arrays;
public class InventoryProgram implements ActionListener
{
Version[] product;
JTextField[] fields;
NumberFormat nf;
public void actionPerformed(ActionEvent e)
{
int index = ((JComboBox)e.getSource()).getSelectedIndex();
populateFields(index);
}
// main method begins program execution
public static void main(String args[] )
{
try
{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFee l");
}
catch (Exception e)
{
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
InventoryProgram test = new InventoryProgram();
test.initStock();
test.showGUI();
test.populateFields(0);
}
// DVDs in stock
public void initStock(){
Version myStock[] = new Version[5]; // an array of 100 dvds
myStock[0] = new Version( "Shrek", "12345", "full screen", 25, 19.95 );
myStock[1] = new Version( "Titanic", "23456", "full screen", 16, 19.95 );
myStock[2] = new Version( "Cars", "43234", "wide screen", 22, 9.95 );
myStock[3] = new Version( "Antz", "49294", "full screen", 12, 9.95 );
myStock[4] = new Version( "Incredibles", "69594", "wide screen", 16, 9.95 );
double array[] = { 498.75, 319.20, 218.90, 119.40, 159.20 };
double total = 0;
// add each element's value to the total
for ( int counter = 0; counter < array.length; counter++ )
total += array[ counter ];
// System.out.printf( "\nTotal inventory value is $%.2f/n", total );
}
private void showGUI()
{
JLabel l;
JButton b;
fields = new JTextField[9];
JComboBox combo = new JComboBox();
for(int j = 0; j < product.length; j++)
combo.addItem(product[j].getDvdName());
combo.addActionListener(this);
JFrame f = new JFrame("InventoryGUI");
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("Name of DVD:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('b');
cp.add(l = new JLabel("Number in Stock:",
SwingConstants.CENTER), c);
l.setDisplayedMnemonic('c');
cp.add(l = new JLabel("Price per Item: $",
SwingConstants.CENTER), c);
l.setDisplayedMnemonic('d');
cp.add(l = new JLabel("Total value: $", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('e');
cp.add(l = new JLabel("Movie format:", SwingConstants.CENTER), c);
l.setDisplayedMnemonic('f');
cp.add(l = new JLabel("Product with Restocking Fee: $",
SwingConstants.CENTER), c);
l.setDisplayedMnemonic('g');
cp.add(l = new JLabel("Total Value of All Merchandise in " +
"Inventory: " + "$", 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;
cp.add(b = new JButton("OK"), c);
cp.add(b = new JButton("First"), c); // set up panel
cp.add(b = new JButton("Prev"), c);
cp.add(b = new JButton("Next"), c);
cp.add(b = new JButton("Last"), c);
b.setMnemonic('o');
f.pack();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
f.setVisible(true);
}
private void populateFields(int index)
{
Version myStock[] = product[index];
fields[0].setText(Long.toString(Version.getItemIDNumber())) ;
fields[1].setText(Version.getDVDName());
fields[2].setText(Long.toString(Version.getnumberInStock()) );
fields[3].setText(Double.toString(Version.itemPrice()));
fields[4].setText(Double.toString(Version.value()));
fields[5].setText(Version.movieType());
fields[6].setText(Double.toString(Version.itemValue()));
fields[7].setText(Double.toString(Version.total(product)));
}
// } // end method main
} // end class InventoryProgram
class Stock
{
// Declare variables
String dvdName; // name of dvd
String itemIDNumber; // product number indentifying specific dvd
double numberInStock; // number of specific dvd in stock
double itemPrice; // price of dvd
double value; // price multiplied by number in stock
// argument constructor
Stock( String dvd, String id, double inStock, double price )
{
// implicit call to object constructor
dvdName = dvd;
itemIDNumber = id;
numberInStock = inStock ; // validate and store number of product in stock
itemPrice = price; // validate and store price of product
value = numberInStock * itemPrice; // compute value of items in stock
} // end argument constructor
// set dvd name
public void setDvdName( String dvd )
{
dvdName = dvd;
} // end method setDvdName
// return dvd name
public String getDvdName()
{
return dvdName;
} // end method getDvdName
// set item ID number
public void setitemIDNumber( String id )
{
itemIDNumber = id;
} // end method setItemIDNumber
// return item ID number
public String getItemIDNumber()
{
return itemIDNumber;
} // end method getItemIDNumber
// set numberInStock
public void setnumberInStock( double inStock )
{
numberInStock = ( inStock );
} // end method set numberInStock
// return number in stock
public double getNumberInStock( double inStock )
{
return numberInStock;
} // end method getNumberInStock
// set itemPrice
public void setItemPrice( double price )
{
itemPrice = ( price );
} // end method setItemPrice
// return itemPrice
public double getItemPrice()
{
return itemPrice;
} // end method getItemPrice
// calculate inventory value
public double value()
{
return numberInStock * itemPrice;
} // end calculate inventory value
// return String representation of Inventory Program
public String toString()
{
return String.format( "%s: %s\n%s: %s\n%s: %.2f\n: %.2f",
"Name of DVD", dvdName,
"Item number", itemIDNumber,
"Number in Stock", numberInStock,
"Price per Item", itemPrice,
"Total Value", value );
}
// display inventory
public void showInventory()
{
System.out.printf( "Name of DVD: %s\n", dvdName );
System.out.printf( "Item number: %s\n", itemIDNumber );
System.out.printf( "Number in Stock: %.2f\n", numberInStock );
System.out.printf( "Price per Item: $%.2f\n", itemPrice );
System.out.printf("Total value: $%.2f\n\n", value );
} // end display inventory
} // end class Stock
class Version extends Stock
{
private String movieType;
double itemValue;
public Version( String dvd, String id, String type, double inStock, double price )
{
super( dvd, id, inStock, price );
movieType = type;
itemValue = value * 1.05;
}
public double itemValue()
{
// double fee = value multiplied by 105% ;
return value * 1.05 ;
} // end method itemValue
@Override
public String toString()
{
String s = String.format("Movie version=%s\n", movieType);
s = s + " " + super.toString();
return s;
}
// display inventory
@Override
public void showInventory()
{
System.out.printf( "Name of DVD: %s\n", dvdName );
System.out.printf( "Item number: %s\n", itemIDNumber );
System.out.printf("Movie format: %s\n", movieType );
System.out.printf( "Number in Stock: %.2f\n", numberInStock );
System.out.printf( "Price per Item: $%.2f\n", itemPrice );
System.out.printf("Total value: $%.2f\n", value );
System.out.printf("Total value with 5 percent restocking fee: %.2f\n\n", itemValue );
} // end display inventory
} // end class Version
I get multiple errors.
Any help is appreciated.
-
Whoa. That's a lot of unformatted code that you're asking folks to wade through and I don't see where you tell us what the specific errors are.
Suggestions:
1) Use code tags to allow your code to retain its formatting and thus be readable.
2) Show the error messages
3) Indicate by way of comments in your code where the errors are occurring.
Best of luck.
-
I'm looking through your code and there are a mess of errors there, including declaring some variables but initializing other variables such that you leave variables hanging and non-initialized. There are so many that I recommend that you scrap the gui and start over.
Your code would benefit tremendously if you would plan out what you are going to do on a pad of paper before trying to commit it to code. Also you need to never add code to bad code. In other words, when creating a program, you need to add only a small bit of code at a time. After adding this small bit, you must then compile the code. Now the key is this: do not add any more code until the code just added compiles and runs. If you don't code this way, you'll end up with a mess of errors that will be unfixable.
Best of luck.
- 01-23-2009, 04:43 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
1) check the data type is correct or not
2) non static method must call by class object such as Version.getItemIDNumber()... getItemIDNumber is non-static method
-
I also recommend that you use Version and Stock to create two more classes. First would be Inventory which would be a nonGUI class to store an array or better an ArrayList<Version>) of Version. This class would allow you to get the first and last items in the collection and could have an index variable that allows you to retrieve the "current" Version object, and would allow you to step through the collection by calling next() or previous().
I'd then use this Inventory class as a nucleus with which to create your InventoryGUI class. The latter class would not inherit from Inventory but instead would hold an Inventory variable and would call the appropriate Inventory method to update the display.
Again, best of luck.
- 01-23-2009, 04:52 PM #6
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
thanks
thank you everyone for your feed back. I decided to scratch everything and start over in the simplest way possible. I now have everything functioning. Maybe I just needed a good night sleep.
Voyager91
-
Congrats! Solving it on your own is the best possible outcome here.
- 02-01-2009, 02:03 AM #8
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
adding buttons
I am trying to add buttons to my GUI but I don't get anything when i run the program. Can someone look at it and give me some pointers, please? I do not get any errors when I compile but I don't get a GUI when I run the program either.
All input is appreciated.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
// Tammy Frownfelter - Inventory Part 5
// import java.util.Scanner;
// import java.util.Arrays;
public class InventoryProgram
{
// main method begins program execution
public static void main(String args[] )
{
// DVDs in stock
Version myStock[] = new Version[5]; // an array of 100 dvds
myStock[0] = new Version( "Shrek", "12345", "full screen", 25, 19.95 );
myStock[1] = new Version( "Titanic", "23456", "full screen", 16, 19.95 );
myStock[2] = new Version( "Cars", "43234", "wide screen", 22, 9.95 );
myStock[3] = new Version( "Antz", "49294", "full screen", 12, 9.95 );
myStock[4] = new Version( "Incredibles", "69594", "wide screen", 16, 9.95 );
// for(Version Version : myStock)
{
// display the inventory all at one time
myStock[0].showInventory();
myStock[1].showInventory();
myStock[2].showInventory();
myStock[3].showInventory();
myStock[4].showInventory();
}
{
myStock[0].getDvdName().compareTo(myStock[1].getDvdName());
// sort stock by name
for ( int i = 0; i< args.length; i++ )
System.out.println( args[i] + ", " );
}
} // end method main
} // end class InventoryProgram
class Stock
{
// Declare variables
String dvdName; // name of dvd
String itemIDNumber; // product number indentifying specific dvd
double numberInStock; // number of specific dvd in stock
double itemPrice; // price of dvd
double value; // price multiplied by number in stock
double total; // total value of inventory
// argument constructor
Stock( String dvd, String id, double inStock, double price )
{
// implicit call to object constructor
dvdName = dvd;
itemIDNumber = id;
numberInStock = inStock ; // validate and store number of product in stock
itemPrice = price; // validate and store price of product
value = numberInStock * itemPrice; // compute value of items in stock
} // end argument constructor
// set dvd name
public void setDvdName( String dvd )
{
dvdName = dvd;
} // end method setDvdName
// return dvd name
public String getDvdName()
{
return dvdName;
} // end method getDvdName
// set item ID number
public void setitemIDNumber( String id )
{
itemIDNumber = id;
} // end method setItemIDNumber
// return item ID number
public String getItemIDNumber()
{
return itemIDNumber;
} // end method getItemIDNumber
// set numberInStock
public void setnumberInStock( double inStock )
{
numberInStock = ( inStock );
} // end method set numberInStock
// return number in stock
public double getNumberInStock( double inStock )
{
return numberInStock;
} // end method getNumberInStock
// set itemPrice
public void setItemPrice( double price )
{
itemPrice = ( price );
} // end method setItemPrice
// return itemPrice
public double getItemPrice()
{
return itemPrice;
} // end method getItemPrice
// calculate inventory value
public double value()
{
return numberInStock * itemPrice;
} // end calculate inventory value
// return String representation of Inventory Program
public String toString()
{
return String.format( "%s: %s\n%s: %s\n%s: %.2f\n: %.2f",
"Name of DVD", dvdName,
"Item number", itemIDNumber,
"Number in Stock", numberInStock,
"Price per Item", itemPrice,
"Total Value", value );
}
{
double array[] = { 498.75, 319.20, 218.90, 119.40, 159.20 };
// add each element's value to the total
for ( int counter = 0; counter < array.length; counter++ )
total += array[ counter ];
}
// display inventory
public void showInventory()
{
System.out.printf( "Name of DVD: %s\n", dvdName );
System.out.printf( "Item number: %s\n", itemIDNumber );
System.out.printf( "Number in Stock: %.2f\n", numberInStock );
System.out.printf( "Price per Item: $%.2f\n", itemPrice );
System.out.printf("Total value: $%.2f\n\n", value );
} // end display inventory
} // end class Stock
class Version extends Stock
{
String movieType;
double itemValue;
public Version( String dvd, String id, String type, double inStock, double price )
{
super( dvd, id, inStock, price );
movieType = type;
itemValue = value * 1.05;
}
public double itemValue()
{
// double fee = value multiplied by 105% ;
return value * 1.05 ;
} // end method itemValue
@Override
public String toString()
{
String s = String.format("Movie version=%s\n", movieType);
s = s + " " + super.toString();
return s;
}
public double total()
{
// return total of all inventory
return total;
}// end method total
// display inventory
// @Override
// public void showInventory()
// {
// String message =
// String.format( "Name of DVD: %s\n Item number: %s\n Movie format: %s\n Number in Stock: %.2f\n Price per Item: $%.2f\n Total value: $%.2f\n Total value with 5 percent restocking fee: $%.2f\n\n Total inventory Value$%.2f\n",
// dvdName, itemIDNumber, movieType, numberInStock, itemPrice, value, itemValue, total );
// JOptionPane.showMessageDialog(null, message);
// } // end display inventory
} // end class Version
class InventoryGUI extends JFrame
{
// access to inventory for DVD collection
private Version theInventory;
// index the inventory of the currently displayed DVD
private int index = 0;
// GUI elements to display currently selected DVD info
private final JLabel dvdNameLabel = new JLabel(" Name of DVD:");
private JTextField dvdNameText;
private final JLabel itemIDNumberLabel = new JLabel(" Item number:");
private JTextField itemIDNumberText;
private final JLabel movieTypeLabel = new JLabel("Movie format:");
private JTextField movieTypeText;
private final JLabel numberInStockLabel = new JLabel("Number in Stock");
private JTextField numberInStockText;
private final JLabel itemPriceLabel = new JLabel("Price per Item:");
private JTextField itemPriceText;
private final JLabel valueLabel = new JLabel("Total value:");
private JTextField valueText;
private final JLabel itemValueLabel = new JLabel("Total item value with 5%:");
private JTextField itemValueText;
private final JLabel totalLabel = new JLabel("Total Inventory:");
private JTextField totalText;
private JPanel centerPanel;
private JPanel buttonPanel;
InventoryGUI(Version version)
{
// constructor for GUI
super("Inventory Program by Tammy Frownfelter");
final Dimension dim = new Dimension(140, 20);
final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
JPanel jp;
// create the inventory object that will hold the product information
theInventory = version;
// setup teh GUI
// product info - setup panel to collect components
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
buttonPanel = new JPanel();
JButton firstButton = new JButton("First");
firstButton.addActionListener(new FirstButtonHandler());
buttonPanel.add(firstButton);
JButton previousButton = new JButton("Previous");
previousButton.addActionListener(new PreviousButtonHandler());
buttonPanel.add(previousButton);
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new NextButtonHandler());
buttonPanel.add(nextButton);
JButton lastButton = new JButton("Last");
lastButton.addActionListener(new LastButtonHandler());
buttonPanel.add(lastButton);
centerPanel.add(buttonPanel);
jp = new JPanel(flo);
dvdNameLabel.setPreferredSize(dim);
jp.add(dvdNameLabel);
dvdNameText = new JTextField(20);
dvdNameText.setEditable(false);
jp.add(dvdNameText);
centerPanel.add(jp);
jp = new JPanel(flo);
itemIDNumberLabel.setPreferredSize(dim);
jp.add(itemIDNumberLabel);
itemIDNumberText = new JTextField(5);
itemIDNumberText.setEditable(false);
jp.add(itemIDNumberText);
centerPanel.add(jp);
jp = new JPanel(flo);
movieTypeLabel.setPreferredSize(dim);
jp.add(movieTypeLabel);
movieTypeText = new JTextField(15);
movieTypeText.setEditable(false);
jp.add(movieTypeText);
centerPanel.add(jp);
jp = new JPanel(flo);
numberInStockLabel.setPreferredSize(dim);
jp.add(numberInStockLabel);
numberInStockText = new JTextField(5);
numberInStockText.setEditable(false);
jp.add(numberInStockText);
centerPanel.add(jp);
jp = new JPanel(flo);
itemPriceLabel.setPreferredSize(dim);
jp.add(itemPriceLabel);
itemPriceText = new JTextField(17);
itemPriceText.setEditable(false);
jp.add(itemPriceText);
centerPanel.add(jp);
jp = new JPanel(flo);
valueLabel.setPreferredSize(dim);
jp.add(valueLabel);
valueText = new JTextField(17);
valueText.setEditable(false);
jp.add(valueText);
centerPanel.add(jp);
jp = new JPanel(flo);
itemValueLabel.setPreferredSize(dim);
jp.add(itemValueLabel);
itemValueText = new JTextField(17);
itemValueText.setEditable(false);
jp.add(itemValueText);
centerPanel.add(jp);
jp = new JPanel(flo);
totalLabel.setPreferredSize(dim);
jp.add(totalLabel);
totalText = new JTextField(17);
totalText.setEditable(false);
jp.add(totalText);
centerPanel.add(jp);
// add the panel to the GUI display
setContentPane(centerPanel);
repaintGUI();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(420, 480);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
// display the GUI with current product's info
public void repaintGUI()
{
Version temp = theInventory;
if (temp != null){
dvdNameText.setText("" + temp.getDvdName());
itemIDNumberText.setText(temp.getItemIDNumber());
movieTypeText.setText(String.format("%s", temp.movieType));
numberInStockText.setText(String.format("%.2f", temp.numberInStock));
itemPriceText.setText(String.format("$%.2f", temp.itemPrice));
valueText.setText(String.format("$%.2f", temp.value));
itemValueText.setText(String.format("$%.2f", temp.itemValue));
totalText.setText(String.format("$%.2f", temp.total));
}
}
class FirstButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
index = 0;
repaintGUI();
}
}
class PreviousButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
index = (--index);
repaintGUI();
}
}
class NextButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
index = (++index);
repaintGUI();
}
}
class LastButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
index = (index -1);
repaintGUI();
}
}
}
-
Your main method is what is going to run no matter what, and if you are going to display a GUI, then either the main method has to declare and initialize the GUI class or it must call a method or other class that will do the same. Your main method doesn't do this. Also, your GUI gets passed a Version object rather than a Version array or collection. Methinks that this means it will display one and only one item.I do not get any errors when I compile but I don't get a GUI when I run the program either.
Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
Similar Threads
-
How come it does not display the inventory
By sparkyii in forum New To JavaReplies: 1Last Post: 10-29-2008, 02:24 PM -
Final Inventory tweeks
By badness in forum New To JavaReplies: 1Last Post: 01-20-2008, 08:18 AM -
Inventory Program modification help
By badness in forum Java AppletsReplies: 1Last Post: 01-17-2008, 05:24 AM -
Inventory part 2 help please
By badness in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:51 AM -
Inventory program
By Nexcompac in forum New To JavaReplies: 3Last Post: 07-27-2007, 05:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks