main class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.table.*;
public class CarSearch implements ActionListener
{
static boolean printErr = false;
JFrame frame = new JFrame ("Car Search");
Container pane = frame.getContentPane ();
myPanel mypane = new myPanel ();
JPanel searchPane = new JPanel ();
JLabel lbl1 = new JLabel ("Make");
JLabel lbl2 = new JLabel ("Model");
JLabel lbl3 = new JLabel ("Year");
JLabel lbl4 = new JLabel ("Min Price");
JLabel lbl5 = new JLabel ("Max Price");
JLabel lbl6 = new JLabel (" ");
JLabel lbl7 = new JLabel (" ");
JComboBox make = new JComboBox ();
JComboBox model = new JComboBox ();
JComboBox year = new JComboBox ();
JTextField minp = new JTextField (10);
JTextField maxp = new JTextField (10);
JButton search = new JButton ("Search");
JButton clear = new JButton ("Clear");
String makeS;
String modelS;
String yearS;
String priceS;
public CarSearch ()
{
frame.setSize (700, 500);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
pane.add (searchPane, "North");
pane.add (mypane, "Center");
searchPane.setLayout (new GridLayout (2, 6));
searchPane.add (lbl1);
searchPane.add (lbl2);
searchPane.add (lbl3);
searchPane.add (lbl4);
searchPane.add (lbl5);
searchPane.add (search);
searchPane.add (make);
searchPane.add (model);
searchPane.add (year);
searchPane.add (minp);
searchPane.add (maxp);
searchPane.add (clear);
make.addActionListener (this);
model.addActionListener (this);
year.addActionListener (this);
search.addActionListener (this);
clear.addActionListener (this);
frame.setVisible (true);
checkCombo (make, 0);
checkCombo (model, 1);
checkCombo (year, 2);
}
public void checkCombo (JComboBox box, int pos)
{
box.removeAllItems ();
box.addItem ("");
try
{
FileReader fr = new FileReader ("catalogue.txt");
BufferedReader in = new BufferedReader (fr);
while (true)
{
String lineIn = in.readLine ();
if (lineIn != null && !lineIn.startsWith ("//"))
{
String[] result = lineIn.split ("#");
box.addItem (result [pos]);
}
else
if (lineIn == null)
break;
}
for (int y = 0 ; y < box.getItemCount () ; y++)
for (int x = 0 ; x < box.getItemCount () ; x++)
if (x != y)
if (box.getItemAt (y).equals (box.getItemAt (x)))
box.removeItemAt (x);
}
catch (Exception e)
{
System.out.println (e);
}
minp.setText ("");
maxp.setText ("");
}
public void actionPerformed (ActionEvent e)
{
//Make combo
if (e.getSource () == make)
{
String temp = (String) make.getSelectedItem ();
if (temp != null && !temp.equals (""))
{
makeS = temp;
model.removeAllItems ();
model.addItem ("");
try
{
FileReader fr = new FileReader ("catalogue.txt");
BufferedReader in = new BufferedReader (fr);
while (true)
{
String lineIn = in.readLine ();
if (lineIn != null && !lineIn.startsWith ("//"))
{
String[] result = lineIn.split ("#");
if (result [0].equals (makeS))
model.addItem (result [1]);
}
else
if (lineIn == null)
break;
}
}
catch (Exception ex)
{
System.out.println (e);
}
}
else
{
checkCombo (model, 1);
}
}
//model combo
if (e.getSource () == model)
{
String temp = (String) model.getSelectedItem ();
if (temp != null && !temp.equals (""))
{
modelS = (String) model.getSelectedItem ();
year.removeAllItems ();
year.addItem ("");
try
{
FileReader fr = new FileReader ("catalogue.txt");
BufferedReader in = new BufferedReader (fr);
while (true)
{
String lineIn = in.readLine ();
if (lineIn != null && !lineIn.startsWith ("//"))
{
String[] result = lineIn.split ("#");
if (result [1].equals (modelS))
year.addItem (result [2]);
}
else
if (lineIn == null)
break;
}
}
catch (Exception ex)
{
System.out.println (e);
}
}
else
{
checkCombo (year, 2);
}
}
//year combo
if (e.getSource () == year)
{
String temp = (String) year.getSelectedItem ();
if (temp != null && !temp.equals (""))
{
yearS = (String) year.getSelectedItem ();
}
}
//search
if (e.getSource () == search)
{
mypane.removeAllRows ();
try
{
FileReader fr = new FileReader ("catalogue.txt");
BufferedReader in = new BufferedReader (fr);
while (true)
{
String lineIn = in.readLine ();
if (lineIn != null && !lineIn.startsWith ("//"))
{
String[] result = lineIn.split ("#");
String[] toSearch = {"", "", "", ""};
if (makeS != null)
{
toSearch [0] = makeS;
}
else if (modelS != null)
{
toSearch [1] = modelS;
}
else if (yearS != null)
{
toSearch [2] = yearS;
}
if (result [0].startsWith (toSearch [0]) && result [1].startsWith (toSearch [1]) && result [2].startsWith (toSearch [2]) && priceC (result [4]))
{
printArray (result);
}
}
else
if (lineIn == null)
break;
}
}
catch (Exception ex)
{
System.out.println (ex);
}
clear (0);
}
//clear
if (e.getSource () == clear)
{
clear (1);
}
}
private void clear (int x)
{
makeS = null;
modelS = null;
yearS = null;
priceS = null;
minp.setText ("");
maxp.setText ("");
checkCombo (make, 0);
checkCombo (model, 1);
checkCombo (year, 2);
if (x == 1)
{
mypane.removeAllRows ();
}
}
private boolean priceC (String ps)
{
try
{
int price = Integer.parseInt (ps);
int min = Integer.parseInt (minp.getText ());
int max = Integer.parseInt (maxp.getText ());
if (ps != null)
{
if (price >= min && price <= max)
{
return true;
}
}
return false;
}
catch (Exception e)
{
return true;
}
}
private void printArray (String[] result)
{
mypane.addRow (result);
}
public static void main (String args[])
{
CarSearch con = new CarSearch ();
}
}
myPanel class:
/**
* myPanel.java
* This class shows how to create a simple JTable
* using table model
*
* author Kanad Deshpande
*/
import javax.swing.*;
import java.util.*;
class myPanel extends JPanel
{
Vector head = new Vector ();
Vector data = new Vector ();
Vector rowData = new Vector ();
JTable mytable;
JScrollPane jsp;
myPanel ()
{
head.add ("Manufacturer:");
head.add ("Model:");
head.add ("Colour:");
head.add ("Price:");
head.add ("Owner:");
mytable = new JTable (data, head);
jsp = new JScrollPane (mytable);
jsp.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add (jsp);
}
public void addRow (String[] result)
{
Vector rowData = new Vector ();
for (int x = 0 ; x < 6 ; x++)
{
rowData.add (result [x]);
}
data.add (rowData);
mytable.updateUI ();
}
public void removeAllRows ()
{
while (data.size () > 0)
{
data.remove (0);
}
mytable.updateUI ();
}
}
catalogue.txt:
//Make#model#year#colour#price#owner Toyota#Yaris#2007#Black#200000#Piet Toyota#Auris#2008#Green#180000#Kyle Ford#Mustang#2004#White#250000#mike Bmw#m5#2008#Red#600000#Ryan Audi#a4#2008#Black#450000#William Porshe#911 Turbo#2010#Black#9000000#Andreas
sorry for all the white space in my code

New Topic/Question
Reply



MultiQuote



|