Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2006

Tip: Looking for answers? Try searching our database.

gui help

Thread view: 
porky008 - 10 Nov 2006 20:18 GMT
I can not figure out how to add a way to get the following program to
display the total value of the inventory or a re-stocking fee. Any help
would be greatly appreciated.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Testing
{
 java.util.List dvds = new java.util.ArrayList();

 JTextField tfTitle = new JTextField(15);
 JTextField tfProductNumber = new JTextField();
 JTextField tfPrice = new JTextField();
 JTextField tfQuantity = new JTextField();
 DefaultListModel dlm = new DefaultListModel();
 JList list = new JList(dlm);
 public void buildGUI()
 {
   JButton btn = new JButton("Add");
   JPanel p1 = new JPanel(new BorderLayout());
   JPanel p = new JPanel(new GridLayout(4,2));
   p.add(new JLabel("DVD Title: "));
   p.add(tfTitle);
   p.add(new JLabel("Product Number: "));
   p.add(tfProductNumber);
   p.add(new JLabel("Price per Unit: "));
   p.add(tfPrice);
   p.add(new JLabel("Quantity on Hand: "));
   p.add(tfQuantity);
   p1.add(p,BorderLayout.NORTH);
   JPanel p2 = new JPanel();
   p2.add(btn);
   p1.add(p2,BorderLayout.CENTER);
   JFrame f = new JFrame();
   f.getContentPane().add(p1,BorderLayout.NORTH);
   JScrollPane sp = new JScrollPane(list);
   f.getContentPane().add(sp,BorderLayout.CENTER);
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.pack();
   f.setLocationRelativeTo(null);
   f.setVisible(true);
   btn.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent ae){
       dvds.add(new DVD(tfTitle.getText(),tfProductNumber.getText(),
                               Double.parseDouble(tfPrice.getText()),

Integer.parseInt(tfQuantity.getText())));
       setList();
       clearTextFields();
     }
   });
   list.addListSelectionListener(new ListSelectionListener(){
     public void valueChanged(ListSelectionEvent lse){
       if(lse.getValueIsAdjusting() == false)
       {
         DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
         tfTitle.setText(dvd.title);
         tfProductNumber.setText(dvd.productNumber);
         tfPrice.setText(""+dvd.price);
         tfQuantity.setText(""+dvd.quantity);
       }
     }
   });
 }
 public void setList()
 {
   dlm.clear();
   for(int x = 0, y = dvds.size(); x < y; x++)
   {
     dlm.addElement((DVD)dvds.get(x));
   }
 }
 public void clearTextFields()
 {
   tfTitle.setText("");
   tfProductNumber.setText("");
   tfPrice.setText("");
   tfQuantity.setText("");
   tfTitle.requestFocusInWindow();
 }
 public static void main(String[] args)
 {
   EventQueue.invokeLater(new Runnable(){
     public void run(){
       new Testing().buildGUI();
     }
   });
 }
}
class DVD
{
 String title;
 String productNumber;
 double price;
 int quantity;
 public DVD(String t,String pn, double p, int q)
 {
   title = t; productNumber = pn; price = p; quantity = q;
 }
 public String toString(){return title;}
}
IchBin - 10 Nov 2006 23:02 GMT
> I can not figure out how to add a way to get the following program to
> display the total value of the inventory or a re-stocking fee. Any help
[quoted text clipped - 99 lines]
>   public String toString(){return title;}
> }

Just add 2 methods to your DVD class to add it and get that total value.

Example..

In your DVD Class
 - add an class instance field to hold total price value
 - add a getter method say called getTotalPrice() to allow users
retrieve that information.
 - in your DVD method add a call to say a method called
addTotoalPrice() to add to the instance total price field.

Signature

Thanks in Advance...                  http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA          http://weconsultants.phpnet.us
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

porky008 - 11 Nov 2006 00:18 GMT
> > I can not figure out how to add a way to get the following program to
> > display the total value of the inventory or a re-stocking fee. Any help
[quoted text clipped - 117 lines]
> 'If there is one, Knowledge is the "Fountain of Youth"'
> -William E. Taylor, Regular Guy (1952-)

I have been trying for a while now to add a method to the dvd class and
can not seem to get the methods right. Can I get a little more help on
this? Thanks
IchBin - 11 Nov 2006 07:43 GMT
>>> I can not figure out how to add a way to get the following program to
>>> display the total value of the inventory or a re-stocking fee. Any help
[quoted text clipped - 121 lines]
> can not seem to get the methods right. Can I get a little more help on
> this? Thanks

Sorry, I did not realize you were creating a DVD instance for each record.

I would create one instance of DVD for all of you records. Maintain all
the data (private ArrayList) and functions in that class with getter()
and setter() methods for the data. In the DVD class you should create
private methods to accumulate the data you want. Again give access to
that data with public getters (accessors).

Signature

Hope this helps...                    http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA          http://weconsultants.phpnet.us
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.