Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-16-2008, 06:49 PM
Member
 
Join Date: Jan 2008
Posts: 4
glenrowan is on a distinguished road
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.

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 Movie
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help using Buttons/Actions with Drawings Deathmonger New To Java 0 04-08-2008 04:11 PM
Buttons to show new panels Lehane_9 AWT / Swing 1 03-06-2008 05:22 PM
Wep page navigation buttons anthony417 Advanced Java 1 07-26-2007 09:40 PM
Next, Finish Buttons !!! pele SWT / JFace 1 07-14-2007 06:22 PM
Question mark colon operator question orchid Advanced Java 3 04-30-2007 11:37 PM


All times are GMT +3. The time now is 11:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org