473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need help with java program

need help with inventory part 5 here is what I have to do
Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item
should display. the GUI using Java graphics classes.
• Add a company logo to
here is what i have so far

Expand|Select|Wrap|Line Numbers
  1. // created by Nicholas Baatz on July 24,2007
  2.   import java.awt.BorderLayout;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.Icon;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextArea;
  15.  
  16.  
  17.  
  18. public class Inventory2
  19. {
  20. //main method begins execution of java application
  21. public static void main(final String args[])
  22. {
  23.  
  24. int i; // varialbe for looping
  25. double total = 0; // variable for total inventory
  26. final int dispProd = 0; // variable for actionEvents
  27.  
  28. // Instantiate a product object
  29. final ProductAdd[] nwProduct = new ProductAdd[5];
  30. // Instantiate objects for the array
  31. for (i=0; i<5; i++)
  32. {
  33. nwProduct[0] = new ProductAdd("Paper", 101, 10, 1.00, "Box");
  34. nwProduct[1] = new ProductAdd("Pen", 102, 10, 0.75, "Pack");
  35. nwProduct[2] = new ProductAdd("Pencil", 103, 10, 0.50, "Pack");
  36. nwProduct[3] = new ProductAdd("Staples", 104, 10, 1.00, "Box");
  37. nwProduct[4] = new ProductAdd("Clip Board", 105, 10, 3.00, "Two Pack");
  38. }
  39.  
  40.  
  41.  
  42. for (i=0; i<5; i++)
  43. total += nwProduct.length; // calculate total inventory cost
  44.  
  45.  
  46.  
  47. final JButton firstBtn = new JButton("First"); // first button
  48. final JButton prevBtn = new JButton("Previous"); // previous button
  49. final JButton nextBtn = new JButton("Next"); // next button
  50. final JButton lastBtn = new JButton("Last"); // last button
  51. final JLabel label; // logo
  52. final JTextArea textArea; // text area for product list
  53. final JPanel buttonJPanel; // panel to hold buttons
  54.  
  55. //JLabel constructor for logo
  56. Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
  57. label = new JLabel(logo); // create logo label
  58. label.setToolTipText("Company Logo"); // create tooltip
  59.  
  60.  
  61. buttonJPanel = new JPanel(); // set up panel
  62. buttonJPanel.setLayout( new GridLayout(1, 4)); //set layout
  63. // add buttons to buttonPanel
  64. buttonJPanel.add(firstBtn);
  65. buttonJPanel.add(prevBtn);
  66. buttonJPanel.add(nextBtn);
  67. buttonJPanel.add(lastBtn);
  68.  
  69.  
  70. textArea = new JTextArea(nwProduct[3]+"\n"); // create textArea for product display
  71.  
  72. // add total inventory value to GUI
  73. textArea.append("\nTotal value of Inventory "+new java.text.DecimalFormat("$0.00").format(total)+"\n\n");
  74. textArea.setEditable(false); // make text uneditable in main display
  75. JFrame invFrame = new JFrame(); // create JFrame container
  76. invFrame.setLayout(new BorderLayout()); // set layout
  77. invFrame.getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER); // add textArea to JFrame
  78. invFrame.getContentPane().add(buttonJPanel, BorderLayout.SOUTH); // add buttons to JFrame
  79. invFrame.getContentPane().add(label, BorderLayout.NORTH); // add logo to JFrame
  80. invFrame.setTitle("Office Min Inventory"); // set JFrame title
  81. invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // termination command
  82. //invFrame.pack();
  83. invFrame.setSize(400, 400); // set size of JPanel
  84. invFrame.setLocationRelativeTo(null); // set screem location
  85. invFrame.setVisible(true); // display window
  86.  
  87.  
  88.  
  89. // assign actionListener and actionEvent for each button
  90. firstBtn.addActionListener(new ActionListener()
  91. {
  92. public void actionPerformed(ActionEvent ae)
  93. {
  94. textArea.setText(nwProduct[0]+"\n");
  95. } // end firstBtn actionEvent
  96.  
  97. }); // end firstBtn actionListener
  98. textArea.setText(nwProduct[4]+"n");
  99.  
  100. // prevBtn.addActionListener(new ActionListener()
  101. // {
  102. // public void actionPerformed(ActionEvent ae)
  103. // {
  104. // dispProd = (nwProduct.length+dispProd-1) % nwProduct.length;
  105. // textArea.setText(nwProduct.display(dispProd)+"\n");
  106. // } // end prevBtn actionEvent
  107. // }); // end prevBtn actionListener
  108.  
  109.  
  110.  
  111.  
  112.  
  113. } // end main
  114.  
  115. } // end class Inventory2
  116.  
  117. class Product
  118. {
  119. protected String prodName; // name of product
  120. protected int itmNumber; // item number
  121. protected int units; // number of units
  122. protected double price; // price of each unit
  123. protected double value; // value of total units
  124.  
  125.  
  126. public Product(String name, int number, int unit, double each) // Constructor for class Product
  127. {
  128. prodName = name;
  129. itmNumber = number;
  130. units = unit;
  131. price = each;
  132.  
  133. } // end constructor
  134.  
  135. public void setProdName(String name) // method to set product name
  136. {
  137. prodName = name;
  138. }
  139.  
  140. public String getProdName() // method to get product name
  141. {
  142. return prodName;
  143. }
  144.  
  145. public void setItmNumber(int number) // method to set item number
  146. {
  147. itmNumber = number;
  148. }
  149.  
  150. public int getItmNumber() // method to get item number
  151. {
  152. return itmNumber;
  153. }
  154.  
  155. public void setUnits(int unit) // method to set number of units
  156. {
  157. units = unit;
  158. }
  159.  
  160. public int getUnits() // method to get number of units
  161. {
  162. return units;
  163. }
  164.  
  165. public void setPrice(double each) // method to set price
  166. {
  167. price = each;
  168. }
  169.  
  170. public double getPrice() // method to get price
  171. {
  172. return price;
  173. }
  174.  
  175. public double calcValue() // method to set value
  176. {
  177. return units * price;
  178. }
  179.  
  180.  
  181.  
  182. } // end class Product
  183.  
  184.  
  185.  
  186. class ProductAdd extends Product
  187. {
  188. private String feature; // variable for added feature
  189.  
  190.  
  191. public ProductAdd(String name, int number, int unit, double each, String addFeat)
  192. {
  193. // call to superclass Product constructor
  194. super(name, number, unit, each);
  195.  
  196. feature = addFeat;
  197. }// end constructor
  198.  
  199. public void setFeature(String addFeat) // method to set added feature
  200. {
  201. feature = addFeat;
  202. }
  203.  
  204. public String getFeature() // method to get added feature
  205. {
  206. return feature;
  207. }
  208.  
  209. public double calcValueRstk() // method to set value and add restock fee
  210. {
  211. return units * price * 0.05;
  212. }
  213.  
  214. public String toString()
  215. {
  216. return String.format("Product: %s\nItem Number: %d\nIn Stock: %d\nPrice: $%.2f\nType: %s\nTotal Value of Stock: $%.2f\nRestock Cost: $%.2f\n\n\n",
  217. getProdName(), getItmNumber(), getUnits(), getPrice(), getFeature(), calcValue(), calcValueRstk());
  218. }
  219.  
  220.  
  221. } // end class ProductAdd
This code compiles and runds but cant get all my buttons to work any help would be nice thxs
Aug 19 '07 #1
1 2800
r035198x
13,262 8TB
You haven't been specific enough with your problem. Which button doesn't work? What is it doing now vs what you want it to do?
Aug 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chad | last post by:
I need my java program to exec an external .exe file, but the problem is that this program being exec()'ed prompts the user for two pieces of information. I am trying to get my java program to...
3
by: rjaw | last post by:
Hi there, using the udb-type2-driver on z/OS DB version 7, we have a problem getting the connection to the database. The small program we use looks like this: ...
7
by: fidlee | last post by:
i am new to learning jython... i just tried compiling a small piece of code that is given below: def fac(x) if x<=1:return 1 return x*fac(x-1) on
1
by: sylsau | last post by:
Hello, I wrote a JAVA program which uses the JAVA API JDOM 1.0 (of this site www.jdom.org) I put the archive jdom.jar in the directory /usr/share/java/jdom.jar and I added this path in the...
1
by: Unebrion | last post by:
Alright im working on a program that prints out user imput in a frame, along with a barcode.. it is like the front of an envelope. Here is the description for the program. This...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
6
crystal2005
by: crystal2005 | last post by:
Hello guys, I'm a beginner in Java application programming. I started to write a Java application in which link to MS Access database. I encountered a problem in deletion function. E.g. I would...
3
by: 100grand | last post by:
Modify the Inventory Program 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...
0
by: Siyodia | last post by:
This is a java program which i need to run facing compilation error Its consuming a third party web service method I have the supported files(folder) which contain necessary class files...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.