Results 1 to 4 of 4
Thread: GUI buttons question...
- 01-16-2008, 05:49 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 4
- Rep Power
- 0
GUI buttons question...
Hello everyone,
Hopefully a quick and simple question someone can assist me with. I have an inventory program that has an next, previous, first, last (these buttons work) and now I added a add, delete, modify, search and save button. Do I need to create a c:\data\inventory.dat file before implementing them? Also if someone could point me to a decent example of how to implement those buttons I would greatly appreciate it. I have searched but the tutorials I cam across didn't help me much. I have added my code in case you need to look at it for my question to make sense. :)
Java Code:import java.util.Arrays; // program uses arrays public class Inventory5 { // main method begins execution of java application public static void main(String[] args) { System.out.println("\nCheckPoint: Inventory Part 6"); System.out.println("My DVD Collection\n"); Movie dvd = null; Inventory inventory = new Inventory(); dvd = new Movie(1, "Superman Returns", 5, 12.99f, 2007); System.out.println(dvd); inventory.addMovie(dvd); dvd = new Movie(2, "Resident Evil", 7, 14.99f, 2000); System.out.println(dvd); inventory.addMovie(dvd); dvd = new Movie(3, "Hostel", 1, 21.99f, 2001); System.out.println(dvd); inventory.addMovie(dvd); dvd = new Movie(4, "Office Space", 3, 15.99f, 1990); inventory.addMovie(dvd); System.out.println(dvd); dvd = new Movie(5, "Rambo", 8, 11.99f, 1984); System.out.println(dvd); inventory.addMovie(dvd); dvd = new Movie(6, "Spiderman", 2, 7.99f, 2005); System.out.println(dvd); System.out.println("\nEnd of DVD collection!\n"); inventory.addMovie(dvd); inventory.printInventory(); GUI gui = new GUI(inventory); // Start the GUI } // end main } // end Inventory5 /**** Class decribes DVD while demostrating polymorphism and inheritance**/ class DVD { private int itemNo; private String title; private int inStock; private float unitPrice; DVD(int itemNo, String title, int inStock, float unitPrice) { this.itemNo = itemNo; this.title = title; this.inStock = inStock; this.unitPrice = unitPrice; } public int getItemNo() { return itemNo; } public String getTitle() { return title; } public int getInStock() { return inStock; } public float getUnitPrice() { return unitPrice; } public float value() { return inStock * unitPrice; } public String toString() { return String.format("itemNo=%2d title=%-22s inStock=%3d price=$%7.2f value=$%8.2f", itemNo, title, inStock, unitPrice, value()); } } // end DVD /*****class has inventory of DVDs. * This class has methods to add and display dvds****/ class Inventory { private final int INVENTORY_SIZE = 30; private DVD[] items; private int numItems; Inventory() { items = new Movie[INVENTORY_SIZE]; numItems = 0; } public int getNumItems() { return numItems; } public DVD getDVD(int n) { return items[n]; } // Adds a Movie to the array of Movies. public void addMovie(DVD item) { items[numItems] = item; ++numItems; } // Loop through our array of Movies and add up the total value. public double value() { double sumOfInventory = 0.0; for (int i = 0; i < numItems; i++) sumOfInventory += items[i].value(); return sumOfInventory; } // Prints the inventory list including name, quantity, price, // and total stock value for each item. public void printInventory() { System.out.println("\nMatt's DVD Inventory\n"); // If no items were found, print a message saying the inventory is empty. if (numItems <= 0) { System.out.println("Inventory is empty at the moment.\n"); } else { for (int i = 0; i < numItems; i++) System.out.printf("%3d %s\n", i, items[i]); System.out.printf("\nTotal value in inventory is $%,.2f\n\n", value()); } } } // end Inventory /**** This is a subclass that adds 5% restocking fee and new feature***/ class Movie extends DVD { // Holds movie year and adds restocking fee private int movieYear; // Constructor, calls the constructor of Movie first public Movie(int MovieID, String itemName, int quantityOnHand, float itemPrice, int year) { super(MovieID, itemName, quantityOnHand, itemPrice); // Pass on the values needed for creating the Movie class first thing this.movieYear = movieYear; } // To set the year manually public void setYear(int year) { movieYear = year; } // Get the year of this DVD Movie public int getMovieYear() { return movieYear; } // Overrides value() in Movie class by calling the base class version and // adding a 5% restocking fee on top public float value() { return super.value() + restockingFee(); } // Simply gets the base class's value, and figures out the 5% restocking fee only public float restockingFee() { return super.value() * 0.05f; } } // end MovieJava Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; // GUI class creates and maintains the GUI public class GUI extends JFrame { // access inventory for DVD Collection private Inventory theInventory; // index in the inventory of the currently displayed DVD. // the index starts at 0, goes to the number of DVDs in the inventory minus 1 private int index = 0; // GUI elements to display currently selected DVD information private final JLabel itemNumberLabel = new JLabel(" Item Number:"); private JTextField itemNumberText; private final JLabel prodnameLabel = new JLabel(" Product Name:"); private JTextField prodnameText; private final JLabel prodpriceLabel = new JLabel(" Price:"); private JTextField prodpriceText; private final JLabel numinstockLabel = new JLabel(" Number in Stock:"); private JTextField numinstockText; private final JLabel valueLabel = new JLabel(" Value:"); private JTextField valueText; private final JLabel restockingFeeLabel = new JLabel(" Restocking Fee:"); private JTextField restockingFeeText; private final JLabel totalValueLabel = new JLabel(" Inventory Total Value:"); private JTextField totalValueText; private JPanel buttonPanel; private JPanel centerPanel; private JLabel logoLabel; // the logo private JPanel jpInstructsPanel; // the instructions // constructor for the GUI, in charge of creating all GUI elements GUI(Inventory inventory) { super("Movie Inventory"); 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 = inventory; // setup the GUI // panel setup centerPanel = new JPanel(); centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS)); //Image and title jpInstructsPanel = new JPanel(); jpInstructsPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); ImageIcon logo = new ImageIcon("logo.gif"); JLabel jLabel = new JLabel(" DVD Inventory", logo, SwingConstants.LEFT); jpInstructsPanel.add(jLabel); centerPanel.add(jpInstructsPanel); buttonPanel = new JPanel(); jp = new JPanel(flo); itemNumberLabel.setPreferredSize(dim); jp.add(itemNumberLabel); itemNumberText = new JTextField(3); itemNumberText.setEditable(false); jp.add(itemNumberText); centerPanel.add(jp); jp = new JPanel(flo); prodnameLabel.setPreferredSize(dim); jp.add(prodnameLabel); prodnameText = new JTextField(17); prodnameText.setEditable(false); jp.add(prodnameText); centerPanel.add(jp); jp = new JPanel(flo); prodpriceLabel.setPreferredSize(dim); jp.add(prodpriceLabel); prodpriceText = new JTextField(17); prodpriceText.setEditable(false); jp.add(prodpriceText); centerPanel.add(jp); jp = new JPanel(flo); numinstockLabel.setPreferredSize(dim); jp.add(numinstockLabel); numinstockText = new JTextField(5); numinstockText.setEditable(false); jp.add(numinstockText); centerPanel.add(jp); jp = new JPanel(flo); restockingFeeLabel.setPreferredSize(dim); jp.add(restockingFeeLabel); restockingFeeText = new JTextField(17); restockingFeeText.setEditable(false); jp.add(restockingFeeText); 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); // add the overall inventory information to the panel jp = new JPanel(flo); totalValueLabel.setPreferredSize(dim); jp.add(totalValueLabel); totalValueText = new JTextField(17); totalValueText.setEditable(false); jp.add(totalValueText); centerPanel.add(jp); buttonPanel = new JPanel(); centerPanel.add(buttonPanel); 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); JButton searchButton = new JButton("Search"); lastButton.addActionListener(new SearchButtonHandler()); buttonPanel.add(searchButton); buttonPanel = new JPanel(); centerPanel.add(buttonPanel); JButton addButton = new JButton("Add"); addButton.addActionListener(new AddButtonHandler()); buttonPanel.add(addButton); JButton deleteButton = new JButton("Delete"); deleteButton.addActionListener(new DeleteButtonHandler()); buttonPanel.add(deleteButton); JButton modifyButton = new JButton("Modify"); modifyButton.addActionListener(new ModifyButtonHandler()); buttonPanel.add(modifyButton); JButton saveButton = new JButton("Save"); saveButton.addActionListener(new SaveButtonHandler()); buttonPanel.add(saveButton); // add the panel to the GUI display //setContentPane(centerPanel); repaintGUI(); setDefaultCloseOperation(EXIT_ON_CLOSE); setContentPane(centerPanel); setBounds(200, 200, 450, 600); setResizable(false); setLocationRelativeTo(null); setVisible(true); } // (re)display the GUI with current product's information public void repaintGUI() { Movie temp = (Movie) theInventory.getDVD(index); if (temp != null) { itemNumberText.setText("" + temp.getItemNo()); prodnameText.setText(temp.getTitle()); prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice())); restockingFeeText.setText(String.format("$%.2f", temp.restockingFee())); numinstockText.setText("" + temp.getInStock()); valueText.setText(String.format("$%.2f", temp.value())); } totalValueText.setText(String.format("$%.2f", theInventory.value())); } class FirstButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { index = 0; repaintGUI(); } } class PreviousButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e){ --index; if (index < 0) index = theInventory.getNumItems()-1; repaintGUI(); } } class NextButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int numItems = theInventory.getNumItems(); index = (++index) % numItems; repaintGUI(); } } class LastButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int numItems = theInventory.getNumItems(); index = (numItems -1) % numItems; repaintGUI(); } } class SearchButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e){ repaintGUI(); } } class AddButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e){ repaintGUI(); } } class ModifyButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e){ repaintGUI(); } } class SaveButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e){ repaintGUI(); } } class DeleteButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e){ repaintGUI(); } } }// end class GUI
- 01-18-2011, 01:21 PM #2
no, since when they are instantiated there are no access to the inventory.dat. only when you push a button, ex. next the code will produce a ArrayIndexOutOfBoundsException because the array is empty and you increment your index. so before the code manipulates the index make a check if there are elements in the array.
for your other question perhaps this link may help.
- 01-18-2011, 02:34 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
This is a dead thread, resurrected by a spam post (in Russian at that).
-
spam removed, spammer banned, thread locked.
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Help using Buttons/Actions with Drawings
By Deathmonger in forum New To JavaReplies: 0Last Post: 04-08-2008, 03:11 PM -
Buttons to show new panels
By Lehane_9 in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 04:22 PM -
Wep page navigation buttons
By anthony417 in forum Advanced JavaReplies: 1Last Post: 07-26-2007, 08:40 PM -
Next, Finish Buttons !!!
By pele in forum SWT / JFaceReplies: 1Last Post: 07-14-2007, 05:22 PM


LinkBack URL
About LinkBacks


Bookmarks