By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

adding a search button

P: 20
I need to add a search button to this and have no clue how to do it. Can some one please help me on this?
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import javax.swing.JFrame.*;
  6.  
  7.  
  8. class Testing
  9. {
  10.   java.util.List<DVD> dvds = new java.util.ArrayList<DVD>();
  11.  
  12.   JTextField tfTitle = new JTextField(5);
  13.   JTextField tfGenre = new JTextField();
  14.   JTextField tfProductNumber = new JTextField();
  15.   JTextField tfPrice = new JTextField();
  16.   JTextField tfQuantity = new JTextField();
  17.   JTextField tfvalue = new JTextField();
  18.   JTextField tfrestock = new JTextField();
  19.   DefaultListModel dlm = new DefaultListModel();
  20.   JList list = new JList(dlm);
  21.   public void buildGUI()
  22.   {
  23.     JButton btnAdd = new JButton("Add");
  24.     JButton btnPrevious = new JButton ("Previous");
  25.     JButton btnNext = new JButton ("Next");
  26.     JButton btnFirst = new JButton ("First");
  27.     JButton btnLast = new JButton ("Last");
  28.     JPanel p1 = new JPanel(new BorderLayout());
  29.     JPanel p = new JPanel(new GridLayout(8,4));
  30.     p.add(new JLabel("DVD Title: "));
  31.     p.add(tfTitle);
  32.     p.add(new JLabel("Genre: "));
  33.     p.add(tfGenre);
  34.     p.add(new JLabel("Product Number: "));
  35.     p.add(tfProductNumber);
  36.     p.add(new JLabel("Price per Unit: "));
  37.     p.add(tfPrice);
  38.     p.add(new JLabel("Quantity on Hand: "));
  39.     p.add(tfQuantity);
  40.     p.add(new JLabel("Value: "));
  41.     p.add(tfvalue);
  42.     p.add(new JLabel("Restock-fee: "));
  43.     p.add(tfrestock);
  44.     p1.add(p,BorderLayout.NORTH);
  45.     JPanel p2 = new JPanel();
  46.     p2.add(btnAdd);
  47.     p2.add(btnPrevious);
  48.     p2.add(btnNext);
  49.     p2.add(btnFirst);
  50.     p2.add(btnLast);
  51.     p1.add(p2,BorderLayout.CENTER);
  52.     JFrame f = new JFrame();
  53.     f.setIconImage(new ImageIcon("bullet.gif").getImage());
  54.     f.getContentPane().add(p1,BorderLayout.NORTH);
  55.     JScrollPane sp = new JScrollPane(list);
  56.     f.getContentPane().add(sp,BorderLayout.CENTER);
  57.     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.     f.pack();
  59.     f.setLocationRelativeTo(null);
  60.     f.setVisible(true);
  61.     f.setIconImage(new ImageIcon("Images/bullet.gif").getImage());
  62.     btnAdd.addActionListener(new ActionListener(){
  63.       public void actionPerformed(ActionEvent ae){
  64.         dvds.add(new DVD(tfTitle.getText(),tfGenre.getText(),
  65.                                 tfProductNumber.getText(),
  66.                                 Double.parseDouble(tfPrice.getText()),
  67.                                 Double.parseDouble(tfvalue.getText()),
  68.                                 Double.parseDouble(tfrestock.getText()),
  69.                                Integer.parseInt(tfQuantity.getText())));
  70.         setList();
  71.         clearTextFields();
  72.               }
  73.     });
  74.  
  75.     btnPrevious.addActionListener(new ActionListener(){
  76.           public void actionPerformed(ActionEvent ae){
  77.             DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
  78.                         int index = (list.getSelectedIndex()-1) % dvds.size();
  79.                         if(index < 0) index = dvds.size()-1;
  80.                         list.setSelectedIndex(index);                   
  81.       }
  82.     });
  83.  
  84.     btnNext.addActionListener(new ActionListener(){
  85.           public void actionPerformed(ActionEvent ae){
  86.             DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
  87.                         int index = (list.getSelectedIndex()+1) % dvds.size();
  88.                         if(index < 0) index = dvds.size()+1;
  89.                         list.setSelectedIndex(index);
  90.  
  91.       }
  92.     });
  93.     btnFirst.addActionListener(new ActionListener(){
  94.           public void actionPerformed(ActionEvent ae){
  95.                 DVD dvd = (DVD)dvds.get(0);
  96.                 list.setSelectedIndex(0);
  97.       }
  98.     });
  99.  
  100.      btnLast.addActionListener(new ActionListener(){
  101.           public void actionPerformed(ActionEvent ae){
  102.             DVD dvd = (DVD)dvds.get(5);
  103.             list.setSelectedIndex(5);
  104.       }
  105.      });
  106.  
  107.     list.addListSelectionListener(new ListSelectionListener(){
  108.       public void valueChanged(ListSelectionEvent lse){
  109.         if(lse.getValueIsAdjusting() == false)
  110.         {
  111.           DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
  112.           tfTitle.setText(dvd.title);
  113.           tfGenre.setText(dvd.genre);
  114.           tfProductNumber.setText(dvd.productNumber);
  115.           tfPrice.setText(""+dvd.price);
  116.           tfQuantity.setText(""+dvd.quantity);
  117.           tfvalue.setText(""+dvd.value);
  118.           tfrestock.setText(""+dvd.restock);
  119.         }
  120.       }
  121.     });
  122.   }
  123.  
  124.   public void setList()
  125.   {
  126.     dlm.clear();
  127.     for(int x = 0, y = dvds.size(); x < y; x++)
  128.     {
  129.       dlm.addElement((DVD)dvds.get(x));
  130.     }
  131.   }
  132.  
  133.   public void clearTextFields()
  134.   {
  135.     tfTitle.setText("");
  136.     tfGenre.setText("");
  137.     tfProductNumber.setText("");
  138.     tfPrice.setText("");
  139.     tfQuantity.setText("");
  140.     tfvalue.setText("");
  141.     tfrestock.setText("");
  142.     tfTitle.requestFocusInWindow();
  143.   }
  144.   public static void main(String[] args)
  145.   {
  146.     EventQueue.invokeLater(new Runnable(){
  147.       public void run(){
  148.         new Testing().buildGUI();
  149.       }
  150.     });
  151.   }
  152. }
  153. class DVD
  154. {
  155.   String title;
  156.   String genre;
  157.   String productNumber;
  158.   double price;
  159.   double value;
  160.   double restock;
  161.   int quantity;
  162.   public DVD(String t, String g, String pn, double p, double v, double r, int q)
  163.   {
  164.     title = t; genre = g; productNumber = pn; price = p; value = v; restock = r; quantity = q;
  165.     value = p * q;
  166.     restock = v * 1.05;
  167.   }
  168.   public String toString(){return title;}
  169. }
Nov 19 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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