473,385 Members | 2,014 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,385 software developers and data experts.

adding a search button

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
0 2188

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

Similar topics

4
by: Chris | last post by:
I think I must be going crazy. All I want to do is add a button to the bottom of a datagrid which will later run some code for a web based project. I've created a custom control that inherits from...
1
by: fong.yang | last post by:
I'm trying to set up a search button on a form. I went thru the FindRecord macro wizards and it's not working correctly or maybe it is and I just don't understand all the arguements I have to...
0
by: porky008 | last post by:
I have this search button working for the most part. I would like it if some one could take a look at it and let me know what I am doing wrong it though. Basically I want it to display movie not...
1
by: tagnum | last post by:
I noticed a strange behaviour in IE 6. Not sure whether it's a bug in my code or because of IE 6. I have written a search function using a standard search input box and search button. (HTML input...
1
by: Ledmark | last post by:
I have a database that uses four forms and each form has it's own table and each form has it's own search button to find a specific record within that table. I would like to use one search button...
1
by: cbones | last post by:
Hello, I am trying to create a search button in a windows forms application. I used a Microsoft Access 2007 file as the database and would like to be able to search the file by specific fields. ...
22
by: GoodGirl | last post by:
Hi : I would like to ask about : how can I write the (simplist) code for search button. I'm bignner in using Access 2007 & VBA . This button will search by name in a query which contains all...
3
by: munkee | last post by:
Using Allen Brownes advanced filter I would like to add the option of an advanced search button. When this is clicked a popup opens with further search options. How would I go about joining the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.