Results 1 to 18 of 18
  1. #1
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default help i'm desperate

    hi everyone
    can you help me with this code i spent the past week trying to find out what is wrong with it
    this is what the program should do (the user enters the car specifications and i have to search for it from a car list in a txt file
    i created 3 classes one is Interface ,Car and CarSpec the code is this

    the code for the interface class

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.JOptionPane;
    
    public class Interface implements ActionListener {
    
    	// declaring variables
    	private JPanel firstWindow,secWindow,infoPanel,labelsPanel,headerPanel,specPanel,btnPanel,secHeaderPanel,secListPanel,secBtnPanel;
    	private JButton btnSearch,btnBack,btnCancel;
    	private JLabel lblTitle,lblRequest,lblTypeCar,lblMake,lblModel,lblEngineSize,lblFuel,lblAge,lblColour,lblName,lblPhone,lblEmail,lblMr,lblResult,lblList;
    	private JComboBox typeCB,makeCB,modelCB,engineCB,fuelCB,ageCB,colourCB;
        static String Name,Email,Phone;
        String typeString,makeString,modelString,engineString,fuelString,ageString,colourString;
    	String []type={"Any","Saloon","Estat","SUV","Sports"};
        String []make={"Any","Ford" ,"Honda" ,"Toyota","BMW"};
    	String[]model={"Any","Mondeo" ,"CR-V Sport","Celica Coupe","Series 3","Z4 Coupe","Series 6 M5 5.0","Jazz" ,"Focus C-Max","Aygo","Landcruiser Amazon","Fiesta","X3 Sport"};
    	String []engine={"Any","1.6","1.8","2.0","2.2","3.0","5.0","1.2", "5.0", "1.0", "1.1","4.7","1.4","2.5"};
    	String []fuel={"Any","Diesel","Petrol"};
    	String []age ={"Any","12","6","17","18","14","8","5","25","24","8","28","38", "39","26"};
    	String []colour={"Any","Blue","Red","Silver","Black","White","Green" };
    	String carResult[][]=new String [15][9];
        public static final int BOLD = 0;
    
    	   //the main method of the program
    	   public static void main(String[] args) {
    		   // TODO Auto-generated method stub
    		   Name = JOptionPane.showInputDialog ( "Enter your name ");
    		   Phone = JOptionPane.showInputDialog ( "Enter your Phone number");
    		   Email = JOptionPane.showInputDialog ( "Enter your Email ");
    	         SwingUtilities.invokeLater(new Runnable(){
    	       	 public void  run(){//calling the creatAndShowGUI (the frame) method
    	       		createAndShowGUI();
    	       	 }
    	       	 });
    	       }
    	   
    	   public static void createAndShowGUI() {
    			// TODO Auto-generated method stub
    			JFrame.setDefaultLookAndFeelDecorated(true);//setting the look of the frame
       		JFrame frame = new JFrame("Whizz Cars");//naming the frame
       	    Interface demo = new Interface();
       		frame.setContentPane(demo.createContentPane());
       		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       		frame.setSize(650,520);//setting the size of the frame
       		frame.setVisible(true);
    
    		}
    
    
    	  public JPanel createContentPane() {
    			// TODO Auto-generated method stub
    			JPanel TotalGUI=new JPanel();
    			TotalGUI.setBackground(Color.lightGray);
    
    			firstWindow=new JPanel();
    			firstWindow.setBackground(Color.LIGHT_GRAY);
    			firstWindow.setVisible(true);
    			firstWindow.setLayout(new BorderLayout(15,15));
    			TotalGUI.add(firstWindow);
    
    			headerPanel = new JPanel ();
    			headerPanel.setBackground(Color.LIGHT_GRAY);
    			headerPanel.setPreferredSize(new Dimension(600,80));
    			headerPanel.setVisible(true);
    			firstWindow.add(headerPanel,BorderLayout.PAGE_START);
    
    			Font bigFont = new Font("Braggadocio",BOLD,30);
    			lblTitle = new JLabel("     Whizz Car Sales");
    			lblTitle.setForeground(Color.RED);
    			lblTitle.setPreferredSize(new Dimension(450,50));
    			lblTitle.setFont(bigFont);
    			headerPanel.add(lblTitle);
    
    			Font midFont=new Font("Arial",BOLD,17);
    			lblRequest=new JLabel("Please select the car's specifications form below...                  ");
    			lblRequest.setForeground(Color.black);
    			lblRequest.setFont(midFont);
    			lblRequest.setPreferredSize(new Dimension(600,20));
    			headerPanel.add(lblRequest);
    
    			infoPanel= new JPanel();
    			infoPanel.setBackground(Color.GRAY);
    			infoPanel.setVisible(true);
    			infoPanel.setPreferredSize(new Dimension(250,300));
    			firstWindow.add(infoPanel,BorderLayout.LINE_START);
    
    			lblName=new JLabel("Hello Mr/Mrs : "+Name);
    			lblName.setForeground(Color.BLACK);
    			lblName.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblName);
    
    			lblPhone=new JLabel("Phone Number : "+Phone);
    			lblPhone.setForeground(Color.BLACK);
    			lblPhone.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblPhone);
    
    			lblEmail=new JLabel("Email : "+Email);
    			lblEmail.setForeground(Color.BLACK);
    			lblEmail.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblEmail);
    
    			labelsPanel =new JPanel();
    			labelsPanel.setBackground(Color.GRAY);
    			labelsPanel.setVisible(true);
    			labelsPanel.setPreferredSize(new Dimension(170,300));
    			firstWindow.add(labelsPanel,BorderLayout.CENTER);
    
    			lblTypeCar=new JLabel("Type of the Car");
    			lblTypeCar.setForeground(Color.ORANGE);
    			lblTypeCar.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblTypeCar);
    
    			lblMake=new JLabel("Car Maker");
    			lblMake.setForeground(Color.ORANGE);
    			lblMake.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblMake);
    
    			lblModel=new JLabel("Car Model");
    			lblModel.setForeground(Color.ORANGE);
    			lblModel.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblModel);
    
    			lblEngineSize =new JLabel("Engine Size");
    			lblEngineSize.setForeground(Color.ORANGE);
    			lblEngineSize.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblEngineSize);
    
    			lblFuel =new JLabel("Type of Fuel ");
    			lblFuel.setForeground(Color.ORANGE);
    			lblFuel.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblFuel);
    
    			lblAge =new JLabel("Car's Age");
    			lblAge.setForeground(Color.ORANGE);
    			lblAge.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblAge);
    
    			lblColour =new JLabel(" Colour of the Car ");
    			lblColour.setForeground(Color.ORANGE);
    			lblColour.setPreferredSize(new Dimension(120,40));
    			labelsPanel.add(lblColour);
    
    			specPanel = new JPanel();
    			specPanel.setBackground(Color.GRAY);
    			specPanel.setVisible(true);
    			specPanel.setPreferredSize(new Dimension(170,320));
    			firstWindow.add(specPanel,BorderLayout.LINE_END);
    
    		//	String[]type = {"ANY","Saloon","Estate","SUV","Sports"};
    			typeCB = new JComboBox(type);
    			typeCB.setPreferredSize(new Dimension (150,40));
    			typeCB.setVisible(true);
    			specPanel.add(typeCB);
    
    		//	String[]make = {"ANY","Ford","Toyota","BMW","Honda"};
    			makeCB = new JComboBox(make);
    			makeCB.setPreferredSize(new Dimension (150,40));
    			makeCB.setVisible(true);
    			specPanel.add(makeCB);
    
    		//	String[]model = {"ANY","Mondeo","CR-V Sport","Celica Coupe","Series 3","Z4 Coupe","Series 6 M5 5.0","Jazz","Focus C-Max","Aygo","Landcruiser Amazon","Fiesta","X3 Sport"};
    			modelCB = new JComboBox(model);
    			modelCB.setPreferredSize(new Dimension (150,40));
    			modelCB.setVisible(true);
    			specPanel.add(modelCB);
    
    		//	String[]engin={"ANY","1.2","1.4","1.6","1.8","2.0"};
    			engineCB=new JComboBox(engine);
    			engineCB.setPreferredSize(new Dimension (150,40));
    			engineCB.setVisible(true);
    			specPanel.add(engineCB);
    
    		//	String []fuel={"Any","Unleaded","Diesel"};
    			fuelCB=new JComboBox(fuel);
    			fuelCB.setPreferredSize(new Dimension (150,40));
    			fuelCB.setVisible(true);
    			specPanel.add(fuelCB);
    
    		//	String[]age = {"Any","0-12","13-24","25-36"};
    			ageCB=new JComboBox(age);
    			ageCB.setPreferredSize(new Dimension (150,40));
    			ageCB.setVisible(true);
    			specPanel.add(ageCB);
    
    
    			colourCB=new JComboBox(colour);
    			colourCB.setPreferredSize(new Dimension (150,40));
    			colourCB.setVisible(true);
    			specPanel.add(colourCB);
    
    			btnPanel = new JPanel();
    			btnPanel.setBackground(Color.GRAY);
    			btnPanel.setVisible(true);
    			btnPanel.setPreferredSize(new Dimension(170,50));
    			firstWindow.add(btnPanel,BorderLayout.PAGE_END);
    
    			btnSearch= new JButton("Search...");
    			btnSearch.setPreferredSize(new Dimension (100,40));
    			btnSearch.setVisible(true);
    			btnSearch.addActionListener(this);
    			btnPanel.add(btnSearch);
    
    			secWindow=new JPanel();
    			secWindow.setBackground(Color.LIGHT_GRAY);
    			secWindow.setVisible(false);
    			secWindow.setLayout(new BorderLayout(30,15));
    			TotalGUI.add(secWindow);
    
    			secHeaderPanel = new JPanel();
    			secHeaderPanel.setBackground(Color.LIGHT_GRAY);
    			secHeaderPanel.setVisible(true);
    			secHeaderPanel.setPreferredSize(new Dimension(600,80));
    			secWindow.add(secHeaderPanel,BorderLayout.PAGE_START);
    
    			lblTitle = new JLabel("     Whizz Car Sales");
    			lblTitle.setForeground(Color.RED);
    			lblTitle.setPreferredSize(new Dimension(450,50));
    			lblTitle.setFont(bigFont);
    			secHeaderPanel.add(lblTitle);
    
    			lblMr = new JLabel(" Mr/Mrs : "+Name);
    			lblMr.setForeground(Color.BLACK);
    			lblMr.setFont(midFont);
    			lblMr.setPreferredSize(new Dimension (600,20));
    			secHeaderPanel.add(lblMr);
    
    			secListPanel=new JPanel();
    			secListPanel.setBackground(Color.gray);
    			secListPanel.setVisible(true);
    			secListPanel.setPreferredSize(new Dimension (600,320));
    			secWindow.add(secListPanel);
    
    			lblResult = new JLabel("These are the cars that match your selections ....           ");
    			lblResult.setForeground(Color.BLACK);
    			lblResult.setFont(midFont);
    			lblResult.setPreferredSize(new Dimension(580,20));
    			secListPanel.add(lblResult);
    			
    			lblList =new JLabel();
    			lblList.setForeground(Color.BLACK);
    			lblList.setFont(midFont);
    			lblList.setPreferredSize(new Dimension(580,350));
    			secListPanel.add(lblList);
    			
    
    			secBtnPanel = new JPanel();
    			secBtnPanel.setBackground(Color.GRAY);
    			secBtnPanel.setVisible(true);
    			secBtnPanel.setPreferredSize(new Dimension(170,50));
    			secWindow.add(secBtnPanel,BorderLayout.PAGE_END);
    
    			btnBack= new JButton("Go Back");
    			btnBack.setPreferredSize(new Dimension (100,40));
    			btnBack.setVisible(true);
    			btnBack.addActionListener(this);
    			secBtnPanel.add(btnBack);
    
    			btnCancel= new JButton("Cancel");
    			btnCancel.setPreferredSize(new Dimension (100,40));
    			btnCancel.setVisible(true);
    			btnCancel.addActionListener(this);
    			secBtnPanel.add(btnCancel);
    
    			TotalGUI.setOpaque(true);
    	        return TotalGUI;
    		}
    	  public void display(){
    		  for (int i=0;i<15;i++){
    			  for(int j=0;j<9;j++){
    				 if (carResult[i][0]!="0"){
    					 lblList.setText(carResult[i][j]);
    				 }
    					 
    			  }
    		  }
    			  
    	  }
    
    //	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    
    		if (arg0.getSource()== btnSearch){
    			secWindow.setVisible(true);
    			firstWindow.setVisible(false);
    
    			typeString = (String)typeCB.getSelectedItem();
    			makeString = (String)makeCB.getSelectedItem();
    			modelString = (String)modelCB.getSelectedItem();
    			colourString = (String)colourCB.getSelectedItem();
    			fuelString = (String)fuelCB.getSelectedItem();
    
    			float engSize;
    			if (engineString.equals("ANY")){
    				engSize=0;
    				}
    			else{
    				engSize=Float.valueOf(engineString);
    				}
    
    			int ageInM;
    			if (ageString.equals("Any")){
    				ageInM=0;
    			}
    			else{
    				ageInM=Integer.valueOf(ageString);
    			}
    			CarSpec userCar =new CarSpec (typeString,makeString,modelString,engSize,fuelString,ageInM,colourString);
    			carResult=userCar.search();
    
    		}
    
    		if (arg0.getSource()== btnBack){
    			secWindow.setVisible(false);
    			firstWindow.setVisible(true);
    		}
    
    		if (arg0.getSource()== btnCancel){
    			secWindow.setVisible(false);
    			firstWindow.setVisible(true);
    			typeCB.setSelectedItem(type[0]);
    	    	makeCB.setSelectedItem(make[0]);
    	    	modelCB.setSelectedItem(model[0]);
    	    	engineCB.setSelectedItem(engine[0]);
    	    	fuelCB.setSelectedItem(fuel[0]);
    	    	ageCB.setSelectedItem(age[0]);
    	    	colourCB.setSelectedItem(colour[0]);
    
    		}
    	}
    
    }
    the code for the Car class
    Java Code:
    public class Car {
    	private String regNumber ;
    	private double price;
    	CarSpec spec;
    	String carResult[][]=new String [15][9];
    	
    	String [][]carList={ {"MA06 XVU", "8999.0", "Saloon" ,"Ford"  ,"Mondeo","1.6" ,"Diesel", "12","Blue"},
    			 {"MA06 UXW", "12900.0","Estate","Ford"  ,"Mondeo","1.8" ,"Petrol",  "6",  "Red"},
    			 {"MA04 EST", "15999.0", "SUV"   ,"Honda" ,"CR-V Sport" ,"2.0" ,"Diesel", "17", "Silver" },
    			 {"VZX 9512", "15499.0", "Sports" ,"Toyota","Celica Coupe","1.8" ,"Petrol","18", "Black" },
    			 {"DV06 UHQ", "25499.0","Saloon" ,"BMW"   ,"Series 3" ,"2.2" ,"Diesel", " 14", "Black" },
    			 {"DV55 WVX", "21499.0","Sports" ,"BMW"   ,"Z4 Coupe" ,"3.0" ,"Petrol", "8",  "Silver"}, 
    			 {"BN06 DHQ", "65499.0","Saloon" ,"BMW"   ,"Series 6 M5 5.0" ,"5.0"  ,"Petrol", " 5",  "Black"},
    			 {"LA54 VUN", "4499.0", "Saloon"  ,"Honda" ,"Jazz"  ,"1.2" ,"Diesel", " 25", "Green" },
    			 {"DV06 MHQ", "65499.0", "Saloon" ,"BMW"   ,"Series 6 M5 5.0","5.0" ,"Petrol", " 5",  "Black"}, 
    			 {"MW04 CXW", "6900.0","Estate"  ,"Ford"  ,"Focus C-Max" ,"1.6" ,"Petrol", "24", "Blue"},
    			 {"VZ55 LRT", "5499.0", "Saloon"  ,"Toyota","Aygo","1.0" ,"Petrol"," 8",  "White" },
    			 {"GH03 RPK", "4000.0", "Saloon"  ,"Toyota","Aygo" ,"1.1" ,"Diesel", " 28", "Green" }, 
    			 {"VH03 ZUK", "17500.0","SUV"    ,"Toyota","Landcruiser Amazon", "4.7" ,"Diesel","38", "Black"}, 
    			 {"WT05 KXW", "3900.0", "Estate"  ,"Ford"  ,"Fiesta" ,"1.4" ,"Petrol", "39", "Red"},
    			 {"SA06 JTX", "17499.0","SUV"   ,"BMW"   ,"X3 Sport" ,"2.5" ,"Petrol",  "26", "Blue"},};
    	
    public Car(String regNumber,double price,CarSpec spec) 
    	{
    		this.regNumber=regNumber;
    		this.price = price;
    		this.spec = spec;
    	}
    
    public double getPrice() 
    {// getter method of the price
    	return price;
    }
    
    public void setprice(double price) 
    {// setter method of the price
    	this.price = price;
    }
    
    public String getregNumber() 
    {// getter method of the RegNumber
    	return regNumber;
    }
    
    public CarSpec getspec() 
    {// getter method of the Spec
    	return spec;
    }
    
    public String[][] search (int[]Search){
    
    	int k=0,x=0;
    	for (int i=0;i<15;i++){
    		carResult[i][0]="0";
    	}
    	for (int z=0;z < Search.length;z++){
    		if (x==Search[z]){
    			for (int j=0;j<9;j++){
    				carResult [k][j]=carList[z][j];
    			}k++;
    		}
    	}
    	return carResult;
    	
    }
    
    }
    the code for the CarSpec class

    Java Code:
    public class CarSpec {
    
    	private String type;
    	private String make;
    	private String model;
    	private float engineSize;
    	private String fuel;
    	private int ageInMonths;
    	private String colour;
        Interface g= new Interface();
    	int Search []=new int[15];
        Car c =new Car(null, 0, null);
    	String carResult[][]=new String [15][9];
        String [][]carList={ 
    	 {"MA06 XVU", "8999.0", "Saloon" ,"Ford"  ,"Mondeo","1.6" ,"Diesel", "12","Blue"},
    	 {"MA06 UXW", "12900.0","Estate","Ford"  ,"Mondeo","1.8" ,"Petrol",  "6",  "Red"},
    	 {"MA04 EST", "15999.0", "SUV"   ,"Honda" ,"CR-V Sport" ,"2.0" ,"Diesel", "17", "Silver" },
    	 {"VZX 9512", "15499.0", "Sports" ,"Toyota","Celica Coupe","1.8" ,"Petrol","18", "Black" },
    	 {"DV06 UHQ", "25499.0","Saloon" ,"BMW"   ,"Series 3" ,"2.2" ,"Diesel", " 14", "Black" },
    	 {"DV55 WVX", "21499.0","Sports" ,"BMW"   ,"Z4 Coupe" ,"3.0" ,"Petrol", "8",  "Silver"}, 
    	 {"BN06 DHQ", "65499.0","Saloon" ,"BMW"   ,"Series 6 M5 5.0" ,"5.0"  ,"Petrol", " 5",  "Black"},
    	 {"LA54 VUN", "4499.0", "Saloon"  ,"Honda" ,"Jazz"  ,"1.2" ,"Diesel", " 25", "Green" },
    	 {"DV06 MHQ", "65499.0", "Saloon" ,"BMW"   ,"Series 6 M5 5.0","5.0" ,"Petrol", " 5",  "Black"}, 
    	 {"MW04 CXW", "6900.0","Estate"  ,"Ford"  ,"Focus C-Max" ,"1.6" ,"Petrol", "24", "Blue"},
    	 {"VZ55 LRT", "5499.0", "Saloon"  ,"Toyota","Aygo","1.0" ,"Petrol"," 8",  "White" },
    	 {"GH03 RPK", "4000.0", "Saloon"  ,"Toyota","Aygo" ,"1.1" ,"Diesel", " 28", "Green" }, 
    	 {"VH03 ZUK", "17500.0","SUV"    ,"Toyota","Landcruiser Amazon", "4.7" ,"Diesel","38", "Black"}, 
    	 {"WT05 KXW", "3900.0", "Estate"  ,"Ford"  ,"Fiesta" ,"1.4" ,"Petrol", "39", "Red"},
    	 {"SA06 JTX", "17499.0","SUV"   ,"BMW"   ,"X3 Sport" ,"2.5" ,"Petrol",  "26", "Blue"}};
      
        	
        
    
    
    
    public CarSpec(String type ,String make, String model,float engineSize ,String fuel , int ageInMonths,String colour)
    	{// making the constructor of the class
    		this.type = type;
    		this.make = make;
    		this.model = model;
    		this.engineSize = engineSize;
    		this.fuel= fuel;
    		this.ageInMonths = ageInMonths;
    		this.colour = colour;
    	}
    
    public String gettype()
    {// the getter method of the type
    	return type;
    }
    
    public String getmake() 
    {// the getter method of the make
    	return make;
    }
    
    public String getmodel() 
    {// the getter method of the model
    	return model;
    }
    
    public int getageInMonths()
    {// the getter method of the age
    	return ageInMonths;
    }
    
    public float getengineSize() 
    {// the getter method of the engine size
    	return engineSize;
    }
    
    public String getfuel() 
    {// the getter method of the fuel
    	return fuel;
    }
    
    public String getcolour() 
    {// the getter method of the color
    	return colour;
    }
    
    public String[][] search(){
    	int x=0;
    	for(int y=0;y<=15;y++){
    		Search[y]=0;
    	}
    	for (int i=0;i>0;i++){
    		
    		 if (g.typeString !="Any"){
    			 x++;
    		    if (g.typeString == carList[i][2]){
    		    	Search[i]++;
    		    }
    			 
    			 
    		 }
    	}
    	for (int i=0;i>0;i++){
    		
    		 if (g.makeString !="Any"){
    			 x++;
    		    if (g.makeString == carList[i][2]){
    		    	Search[i]++;
    		    }
    			 
    			 
    		 }
    	}
    	for (int i=0;i>0;i++){
    		
    		 if (g.modelString !="Any"){
    			 x++;
    		    if (g.modelString == carList[i][2]){
    		    	Search[i]++;
    		    }
    			 
    			 
    		 }
    	}
    	for (int i=0;i>0;i++){
    		
    		 if (g.engineString !="Any"){
    			 x++;
    		    if (g.engineString== carList[i][2]){
    		    	Search[i]++;
    		    }
    		 }
    	}
    	for (int i=0;i>0;i++){
    		
    		 if (g.fuelString !="Any"){
    			 x++;
    		    if (g.fuelString== carList[i][2]){
    		    	Search[i]++;
    		    }
    		 }
    	}
    	for (int i=0;i>0;i++){
    		
    		 if (g.ageString !="Any"){
    			 x++;
    		    if (g.ageString== carList[i][2]){
    		    	Search[i]++;
    		    }
    		 }
    	}
    	for (int i=0;i>0;i++){
    		
    		 if (g.colourString !="Any"){
    			 x++;
    		    if (g.colourString== carList[i][2]){
    		    	Search[i]++;
    		    }
    		 }
    	}
    	carResult=c.search(Search);
    	return carResult;
    }
      
    }

    and this is the car list text file
    Java Code:
    // List of cars to use:
     ("MA06 XVU", 8999.0, new CarSpec("Saloon" ,"Ford"  ,"Mondeo"            , (float)1.6 ,"Diesel", (int) 12,"Blue"    ));
     ("MA06 UXW", 12900.0, new CarSpec("Estate","Ford"  ,"Mondeo"            , (float)1.8 ,"Petrol", (int) 6,  "Red"    ));
     ("MA04 EST", 15999.0, new CarSpec("SUV"   ,"Honda" ,"CR-V Sport"        , (float)2.0 ,"Diesel", (int) 17, "Silver" )); 
    ("VZX 9512", 15499.0, new CarSpec("Sports" ,"Toyota","Celica Coupe"      , (float)1.8 ,"Petrol",(int)  18, "Black"  )); 
    ("DV06 UHQ", 25499.0, new CarSpec("Saloon" ,"BMW"   ,"Series 3"          , (float)2.2 ,"Diesel", (int) 14, "Black"  )); 
    ("DV55 WVX", 21499.0, new CarSpec("Sports" ,"BMW"   ,"Z4 Coupe"          , (float)3.0 ,"Petrol", (int) 8,  "Silver" )); 
    ("BN06 DHQ", 65499.0, new CarSpec("Saloon" ,"BMW"   ,"Series 6 M5 5.0"   ,(float)5.0  ,"Petrol", (int) 5,  "Black"  )); 
    ("LA54 VUN", 4499.0, new CarSpec("Saloon"  ,"Honda" ,"Jazz"              , (float)1.2 ,"Diesel", (int) 25, "Green"  )); 
    ("DV06 MHQ", 65499.0, new CarSpec("Saloon" ,"BMW"   ,"Series 6 M5 5.0"   , (float)5.0 ,"Petrol", (int) 5,  "Black"  )); 
    ("MW04 CXW", 6900.0, new CarSpec("Estate"  ,"Ford"  ,"Focus C-Max"       , (float)1.6 ,"Petrol", (int) 24, "Blue"   )); 
    ("VZ55 LRT", 5499.0, new CarSpec("Saloon"  ,"Toyota","Aygo"              , (float)1.0 ,"Petrol",(int)  8,  "White"  ));
    ("GH03 RPK", 4000.0, new CarSpec("Saloon"  ,"Toyota","Aygo"              , (float)1.1 ,"Diesel", (int) 28, "Green"  )); 
    ("VH03 ZUK", 17500.0, new CarSpec("SUV"    ,"Toyota","Landcruiser Amazon", (float)4.7 ,"Diesel", (int) 38, "Black"  )); 
    ("WT05 KXW", 3900.0, new CarSpec("Estate"  ,"Ford"  ,"Fiesta"            , (float)1.4 ,"Petrol", (int) 39, "Red"    ));
     ("SA06 JTX", 17499.0, new CarSpec("SUV"   ,"BMW"   ,"X3 Sport"          , (float)2.5 ,"Petrol", (int) 26, "Blue"   ));

    i'm really really desperate i would have solved it my self but my deadline is close
    thanx

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Red face

    So what happens when you run this? Is there any error? Any exception? Or the code itself doesn't compile?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    A couple of comments (and you haven't actually told us the problem you're having).
    Why are there multiple carList arrays?
    Why are these pre-populated with the same data that is in the file?
    Surely you are expected to read the data in from the file and make a single list of Car objects that you can then search?

  4. #4
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    hi
    when i run the program the GUI runs fine but nothing happens when i search for a car
    and i tried to read from the file but there was too many problems that i couldn't fix so i decided to put the information in an array

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    OK, then put the info an an array, not several.
    At least that way it'll be easier to reference.

    Also it should be at least static. At the moment you have one for every instance of Car and CarSpec which is a bit OTT.

    Anyway, that aside, you're going to have to stick debugging statements in there (Sys out println()), mainly in the search() method at a guess t see what it's doing (and returning).

  6. #6
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    also i have to use these 2 constructors
    Java Code:
    public CarSpec(String type ,String make, String model,float engineSize ,String fuel , int ageInMonths,String colour)
    Java Code:
    public Car(String regNumber,double price,CarSpec spec)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    So use them.
    Make the Car and CarSpecs, based on that array.

  8. #8
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    believe it or not this all array mess was done by my teacher he is the one who told me to do this and now I'm totally lost and i have no clue about what to do

  9. #9
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    my idea is to get the information of the cars and put it in an array
    then get the Spec of the users car and put it in an array the match the 2 array and get the index of the cars that has all the user's car spec and print it
    i just dont know how to start with this

  10. #10
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    You need to look at the error messages you get when it fails.
    A few are:
    1. In Interface.actionPerformed, engineString and ageString are not initialized.
    2. "Any" is typed as "ANY" in at least one place.
    3. In CarSpec.search, first for loop, y<=15 should be y<15 or you go out of bounds
    4. In the for loops below that will never run. I think you need i<15. The != "Any" check will work better outside the for loop for efficiency.

    I stopped there.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    Quote Originally Posted by AniMaind View Post
    my idea is to get the information of the cars and put it in an array
    then get the Spec of the users car and put it in an array the match the 2 array and get the index of the cars that has all the user's car spec and print it
    i just dont know how to start with this
    I can see that can be confusing. Your teacher has possibly muddied the waters a bit and not explained what the arrays are that you've been given.

    They are a stand-in for the text file, so they only need to be used to create the array of Cars and the array of CarSpecs.

    So you only need to use them when your code starts up, and they're only needed until the Car and CarSpec arrays are built.

    Something along the lines of:

    Start program.
    Build CarSpec array from the static (single) array of car data.
    Build Car array from the static array of car data (and CarSpecs).
    Display stuff to user...etc etc.

  12. #12
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Talking JTextField

    hi
    thanx for helping me out every one i solved the problem and the program is working now
    i just have one small problem can you pleas check out the JTextField and the JButton called btnNext i don't know why I'm not getting any thing from the TextFields
    and before i submit the program do you have any comments on the the program that can help me improve it and do you think i should add more comments or the one there are enough
    cheers


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class Interface implements ActionListener {//the class name and the implementation of ActionListener
    
    	// declaring variables
    	private JPanel firstWindow,secWindow,startWindow,fieldPanel,startInfoPanel,txtPanel,btnNextPanel,startheaderPanel,infoPanel,labelsPanel,headerPanel,specPanel,btnPanel,secHeaderPanel,secListPanel,secBtnPanel;
    	private JButton btnNext,btnEnter,btnSearch,btnBack,btnCancel,btnExit,btnExitsec;
    	private JLabel lblTitle,lblStartRequest,lblInformation,lblRequest,lblTypeCar,lblMake,lblModel,lblEngineSize,lblFuel,lblAge,lblColour,lblName,lblPhone,lblEmail,lblMr,lblResult,lblList;
    	private JComboBox typeCB,makeCB,modelCB,engineCB,fuelCB,ageCB,colourCB;
    	private JTextArea carsList;
    	private JTextField nameField,phoneField,emailField;
    	CarSpec userCar;
        String typeString,makeString,modelString,engineString,fuelString,ageString,colourString;
    	String []type={"Any","Saloon","Estate","SUV","Sports"};
        String []make={"Any","Ford" ,"Honda" ,"Toyota","BMW"};
    	String []model={"Any","Mondeo" ,"CR-V Sport","Celica Coupe","Series 3","Z4 Coupe","Series 6 M5 5.0","Jazz" ,"Focus C-Max","Aygo","Landcruiser Amazon","Fiesta","X3 Sport"};
    	String []engine={"Any","1.6","1.8","2.0","2.2","3.0","5.0","1.2", "5.0", "1.0", "1.1","4.7","1.4","2.5"};
    	String []fuel={"Any","Diesel","Petrol"};
    	String []age ={"Any","12","6","17","18","14","8","5","25","24","8","28","38", "39","26"};
    	String []colour={"Any","Blue","Red","Silver","Black","White","Green" };
    	String carResult[][]=new String [15][9];
        public static final int BOLD = 0;
        
    
    	   //the main method of the program
    	   public static void main(String[] args) {
    		   // TODO Auto-generated method stub
    	         SwingUtilities.invokeLater(new Runnable(){
    	       	 public void  run(){//calling the creatAndShowGUI (the frame) method
    	       		createAndShowGUI();
    	       	 }
    	       	 });
    	       }
    	   
    	   public static void createAndShowGUI() {
    			// TODO Auto-generated method stub
    			JFrame.setDefaultLookAndFeelDecorated(true);//setting the look of the frame
       		JFrame frame = new JFrame("Whizz Cars");//naming the frame
       	    Interface demo = new Interface();
       		frame.setContentPane(demo.createContentPane());
       		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       		frame.setSize(650,520);//setting the size of the frame
       		frame.setVisible(true);
    		}
    
    	   //in this method all the JComboBox, JButtons, JTextFields, JTextArea, JLabels And JPanels are added 
    	   //the size location, visibility and the BackGround colour is set and the added to the panels 
    	   //the GUI is returned 
    	  public JPanel createContentPane() {
    			// TODO Auto-generated method stub
    			JPanel TotalGUI=new JPanel();
    			TotalGUI.setBackground(Color.lightGray);
    			
    			startWindow=new JPanel();
    			startWindow.setBackground(Color.LIGHT_GRAY);
    			startWindow.setVisible(true);
    			startWindow.setLayout(new BorderLayout(30,15));
    			TotalGUI.add(startWindow);
    			
    			startheaderPanel = new JPanel ();
    			startheaderPanel.setBackground(Color.LIGHT_GRAY);
    			startheaderPanel.setPreferredSize(new Dimension(600,80));
    			startheaderPanel.setVisible(true);
    			startWindow.add(startheaderPanel,BorderLayout.PAGE_START);
    			
    			Font bigFont = new Font("Braggadocio",BOLD,30);
    			lblTitle = new JLabel("     Whizz Car Sales");
    			lblTitle.setForeground(Color.RED);
    			lblTitle.setPreferredSize(new Dimension(450,50));
    			lblTitle.setFont(bigFont);
    			startheaderPanel.add(lblTitle);
    			
    			Font midFont=new Font("Arial",BOLD,17);
    			lblStartRequest=new JLabel("Please enter your information below...                  ");
    			lblStartRequest.setForeground(Color.black);
    			lblStartRequest.setFont(midFont);
    			lblStartRequest.setPreferredSize(new Dimension(600,20));
    			startheaderPanel.add(lblStartRequest);
    			
    			startInfoPanel = new JPanel();
    			startInfoPanel.setBackground(Color.GRAY);
    			startInfoPanel.setVisible(true);
    			startInfoPanel.setPreferredSize(new Dimension(170,300));
    			startWindow.add(startInfoPanel,BorderLayout.CENTER);
    	        
    			txtPanel = new JPanel();
    			txtPanel.setBackground(Color.GRAY);
    			txtPanel.setVisible(true);
    			txtPanel.setPreferredSize(new Dimension(240,200));
    			startInfoPanel.add(txtPanel);
    			
    			lblName=new JLabel("Name : ");
    			lblName.setForeground(Color.BLACK);
    			lblName.setPreferredSize(new Dimension(240,30));
    			txtPanel.add(lblName);
    
    			lblPhone=new JLabel("Phone Number : ");
    			lblPhone.setForeground(Color.BLACK);
    			lblPhone.setPreferredSize(new Dimension(240,30));
    			txtPanel.add(lblPhone);
    
    			lblEmail=new JLabel("Email : ");
    			lblEmail.setForeground(Color.BLACK);
    			lblEmail.setPreferredSize(new Dimension(240,30));
    			txtPanel.add(lblEmail);
    			
    			fieldPanel=new JPanel();
    			fieldPanel.setBackground(Color.GRAY);
    			fieldPanel.setVisible(true);
    			fieldPanel.setPreferredSize(new Dimension(240,200));
    			startInfoPanel.add(fieldPanel);
    			
    			nameField = new JTextField(19);
    			nameField.setLocation(0, 0);
    			nameField.setText("");
    	        nameField.setSize(150, 40);
    	        fieldPanel.add(nameField);
    	        
    	        phoneField = new JTextField(19);
    			phoneField.setLocation(0, 0);
    			phoneField.setText("");
    	        phoneField.setSize(150, 40);
    	        fieldPanel.add(phoneField);
    	        
    	        emailField = new JTextField(19);
    			emailField.setLocation(0, 0);
    			emailField.setText("");
    	        emailField.setSize(150, 40);
    	        fieldPanel.add(emailField);
    	        
    	        lblInformation=new JLabel("Please enter your personal details first in order to go to the next page ");
    			lblInformation.setForeground(Color.RED);
    			lblInformation.setVisible(false);
    			lblInformation.setPreferredSize(new Dimension(300,30));
    			startInfoPanel.add(lblInformation);
    			
    			btnNextPanel = new JPanel();
    			btnNextPanel.setBackground(Color.GRAY);
    			btnNextPanel.setVisible(true);
    			btnNextPanel.setPreferredSize(new Dimension(170,50));
    			startWindow.add(btnNextPanel,BorderLayout.PAGE_END);
    			
    			btnNext= new JButton("Next...");
    			btnNext.setPreferredSize(new Dimension (100,40));
    			btnNext.setVisible(true);
    			btnNext.addActionListener(this);
    			btnNextPanel.add(btnNext);
    			
    			btnEnter= new JButton("Enter Car Specifications");
    			btnEnter.setPreferredSize(new Dimension (170,40));
    			btnEnter.setVisible(true);
    			btnEnter.setEnabled(false);
    			btnEnter.addActionListener(this);
    			btnNextPanel.add(btnEnter);
    
    			firstWindow=new JPanel();
    			firstWindow.setBackground(Color.LIGHT_GRAY);
    			firstWindow.setVisible(false);
    			firstWindow.setLayout(new BorderLayout(15,15));
    			TotalGUI.add(firstWindow);
    
    			headerPanel = new JPanel ();
    			headerPanel.setBackground(Color.LIGHT_GRAY);
    			headerPanel.setPreferredSize(new Dimension(600,80));
    			headerPanel.setVisible(true);
    			firstWindow.add(headerPanel,BorderLayout.PAGE_START);
    			
    			lblTitle = new JLabel("     Whizz Car Sales");
    			lblTitle.setForeground(Color.RED);
    			lblTitle.setPreferredSize(new Dimension(450,50));
    			lblTitle.setFont(bigFont);
    			headerPanel.add(lblTitle);
    
    			lblRequest=new JLabel("Please select the car's specifications from below...                  ");
    			lblRequest.setForeground(Color.black);
    			lblRequest.setFont(midFont);
    			lblRequest.setPreferredSize(new Dimension(600,20));
    			headerPanel.add(lblRequest);
    
    			infoPanel= new JPanel();
    			infoPanel.setBackground(Color.GRAY);
    			infoPanel.setVisible(true);
    			infoPanel.setPreferredSize(new Dimension(250,300));
    			firstWindow.add(infoPanel,BorderLayout.LINE_START);
    
    			lblName=new JLabel("Hello Mr/Mrs : "+nameField.getText());
    			lblName.setForeground(Color.BLACK);
    			lblName.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblName);
    
    			lblPhone=new JLabel("Phone Number : "+phoneField.getText());
    			lblPhone.setForeground(Color.BLACK);
    			lblPhone.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblPhone);
    
    			lblEmail=new JLabel("Email : "+emailField.getText());
    			lblEmail.setForeground(Color.BLACK);
    			lblEmail.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblEmail);
    
    			labelsPanel =new JPanel();
    			labelsPanel.setBackground(Color.GRAY);
    			labelsPanel.setVisible(true);
    			labelsPanel.setPreferredSize(new Dimension(170,300));
    			firstWindow.add(labelsPanel,BorderLayout.CENTER);
    
    			lblTypeCar=new JLabel("Type of the Car");
    			lblTypeCar.setForeground(Color.ORANGE);
    			lblTypeCar.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblTypeCar);
    
    			lblMake=new JLabel("Car Maker");
    			lblMake.setForeground(Color.ORANGE);
    			lblMake.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblMake);
    
    			lblModel=new JLabel("Car Model");
    			lblModel.setForeground(Color.ORANGE);
    			lblModel.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblModel);
    
    			lblEngineSize =new JLabel("Engine Size");
    			lblEngineSize.setForeground(Color.ORANGE);
    			lblEngineSize.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblEngineSize);
    
    			lblFuel =new JLabel("Type of Fuel ");
    			lblFuel.setForeground(Color.ORANGE);
    			lblFuel.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblFuel);
    
    			lblAge =new JLabel("Car's Age");
    			lblAge.setForeground(Color.ORANGE);
    			lblAge.setPreferredSize(new Dimension(100,40));
    			labelsPanel.add(lblAge);
    
    			lblColour =new JLabel(" Colour of the Car ");
    			lblColour.setForeground(Color.ORANGE);
    			lblColour.setPreferredSize(new Dimension(120,40));
    			labelsPanel.add(lblColour);
    
    			specPanel = new JPanel();
    			specPanel.setBackground(Color.GRAY);
    			specPanel.setVisible(true);
    			specPanel.setPreferredSize(new Dimension(170,320));
    			firstWindow.add(specPanel,BorderLayout.LINE_END);
    
    			typeCB = new JComboBox(type);
    			typeCB.setPreferredSize(new Dimension (150,40));
    			typeCB.setVisible(true);
    			specPanel.add(typeCB);
    
    			makeCB = new JComboBox(make);
    			makeCB.setPreferredSize(new Dimension (150,40));
    			makeCB.setVisible(true);
    			specPanel.add(makeCB);
    
    			modelCB = new JComboBox(model);
    			modelCB.setPreferredSize(new Dimension (150,40));
    			modelCB.setVisible(true);
    			specPanel.add(modelCB);
    
    			engineCB=new JComboBox(engine);
    			engineCB.setPreferredSize(new Dimension (150,40));
    			engineCB.setVisible(true);
    			specPanel.add(engineCB);
    
    			fuelCB=new JComboBox(fuel);
    			fuelCB.setPreferredSize(new Dimension (150,40));
    			fuelCB.setVisible(true);
    			specPanel.add(fuelCB);
    
    			ageCB=new JComboBox(age);
    			ageCB.setPreferredSize(new Dimension (150,40));
    			ageCB.setVisible(true);
    			specPanel.add(ageCB);
    
    			colourCB=new JComboBox(colour);
    			colourCB.setPreferredSize(new Dimension (150,40));
    			colourCB.setVisible(true);
    			specPanel.add(colourCB);
    
    			btnPanel = new JPanel();
    			btnPanel.setBackground(Color.GRAY);
    			btnPanel.setVisible(true);
    			btnPanel.setPreferredSize(new Dimension(170,50));
    			firstWindow.add(btnPanel,BorderLayout.PAGE_END);
    
    			btnSearch= new JButton("Search...");
    			btnSearch.setPreferredSize(new Dimension (100,40));
    			btnSearch.setVisible(true);
    			btnSearch.addActionListener(this);
    			btnPanel.add(btnSearch);
    			
    			btnExit= new JButton("Exit");
    			btnExit.setPreferredSize(new Dimension (100,40));
    			btnExit.setVisible(true);
    			btnExit.addActionListener(this);
    			btnPanel.add(btnExit);
    			
    			secWindow=new JPanel();
    			secWindow.setBackground(Color.LIGHT_GRAY);
    			secWindow.setVisible(false);
    			secWindow.setLayout(new BorderLayout(30,15));
    			TotalGUI.add(secWindow);
    
    			secHeaderPanel = new JPanel();
    			secHeaderPanel.setBackground(Color.LIGHT_GRAY);
    			secHeaderPanel.setVisible(true);
    			secHeaderPanel.setPreferredSize(new Dimension(600,80));
    			secWindow.add(secHeaderPanel,BorderLayout.PAGE_START);
    
    			lblTitle = new JLabel("     Whizz Car Sales");
    			lblTitle.setForeground(Color.RED);
    			lblTitle.setPreferredSize(new Dimension(450,50));
    			lblTitle.setFont(bigFont);
    			secHeaderPanel.add(lblTitle);
    
    			lblMr = new JLabel(" Mr/Mrs : "+nameField.getText());
    			lblMr.setForeground(Color.BLACK);
    			lblMr.setFont(midFont);
    			lblMr.setPreferredSize(new Dimension (600,20));
    			secHeaderPanel.add(lblMr);
    
    			secListPanel=new JPanel();
    			secListPanel.setBackground(Color.gray);
    			secListPanel.setVisible(true);
    			secListPanel.setPreferredSize(new Dimension (600,320));
    			secWindow.add(secListPanel);
    
    			lblResult = new JLabel("These are the cars that match your selections ....           ");
    			lblResult.setForeground(Color.BLACK);
    			lblResult.setFont(midFont);
    			lblResult.setPreferredSize(new Dimension(580,20));
    			secListPanel.add(lblResult);
    			
    			lblList =new JLabel();
    			lblList.setForeground(Color.RED);
    			lblList.setFont(midFont);
    			lblList.setVisible(false);
    			lblList.setPreferredSize(new Dimension(580,350));
    			secListPanel.add(lblList);
    			
    			carsList = new JTextArea(10,40);
    			carsList.setPreferredSize(new Dimension(580,280));
    			carsList.setVisible(false);
    			carsList.setBackground(Color.LIGHT_GRAY);
    			secListPanel.add(carsList);
    
    			secBtnPanel = new JPanel();
    			secBtnPanel.setBackground(Color.GRAY);
    			secBtnPanel.setVisible(true);
    			secBtnPanel.setPreferredSize(new Dimension(170,50));
    			secWindow.add(secBtnPanel,BorderLayout.PAGE_END);
    
    			btnBack= new JButton("Go Back");
    			btnBack.setPreferredSize(new Dimension (100,40));
    			btnBack.setVisible(true);
    			btnBack.addActionListener(this);
    			secBtnPanel.add(btnBack);
    
    			btnCancel= new JButton("Cancel");
    			btnCancel.setPreferredSize(new Dimension (100,40));
    			btnCancel.setVisible(true);
    			btnCancel.addActionListener(this);
    			secBtnPanel.add(btnCancel);
    			
    			btnExitsec= new JButton("Exit");
    			btnExitsec.setPreferredSize(new Dimension (100,40));
    			btnExitsec.setVisible(true);
    			btnExitsec.addActionListener(this);
    			secBtnPanel.add(btnExitsec);
    			
    			TotalGUI.setOpaque(true);
    	        return TotalGUI;//returning the GUI
    		}
    	  
    	  //a display method that prints the list of available cars on the text Area 
    	  public void display(){		
    		 if(carResult[0][0] !="0")
    		 {
    			 carsList.setVisible(true);
    			 for (int i=0;i<15;i++)
    			 {
    			   for(int j=0;j<9;j++)
    			   {
    				 if (carResult[i][0]!="0")
    				 {
    					 carsList.setText(carsList.getText()+carResult[i][j]+"     "); 
    			     } 	 
    			   }
    			  carsList.setText(carsList.getText()+"\n");
    		   }
    	   
    	    }
    		 else
    		 { 
    			 //if there is no cars that match the user's selection this label is shown on the GUI
    			 lblList.setVisible(true);
    			 lblList.setText("Sorry the car you selected is not available");//
    		 }
    	  }
    
    //	@Override
    	  //the actionPreformed of the class 
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub  !=
    		//actions happen when the Next button is clicked
    		if(arg0.getSource()==btnNext){
    			  if (nameField.getText() == null
    			  && (phoneField.getText()== null)
    			  && (emailField.getText()==null))
    			  {
    				 lblInformation.setVisible(true);//if the user did not enter his information in the text fields this label will be printed
    			  }
    			  else
    			  {
    				  btnEnter.setEnabled(true);//else the Enter button is enabled
    			  }
    		}
    		//when the enter button is clicked it will take the user to the next window
    		if(arg0.getSource()==btnEnter){
    			  startWindow.setVisible(false);
    		      firstWindow.setVisible(true);
    		}
    		//actions that happen when the Search button is pressed 
    		if (arg0.getSource()== btnSearch){
    			secWindow.setVisible(true);// the second window appear
    			firstWindow.setVisible(false);//the first window disappear
    			//get the selected item form the comb`box
    			typeString = (String)typeCB.getSelectedItem();
    			makeString = (String)makeCB.getSelectedItem();
    			modelString = (String)modelCB.getSelectedItem();
    			colourString = (String)colourCB.getSelectedItem();
    			fuelString = (String)fuelCB.getSelectedItem();
    			engineString = (String)engineCB.getSelectedItem();
    			ageString = (String)ageCB.getSelectedItem();
    			userCar =new CarSpec (typeString,makeString,modelString,engineString,fuelString,ageString,colourString);
    			carResult=userCar.search();
    			display();
    		}
    
    		if (arg0.getSource()== btnBack){
    			secWindow.setVisible(false);
    			firstWindow.setVisible(true);
    		}
    
    		if (arg0.getSource()== btnCancel){
    			secWindow.setVisible(false);
    			firstWindow.setVisible(true);
    			//the comboBox selected item is set to the index 0 of the related array 
    			typeCB.setSelectedItem(type[0]);
    	    	makeCB.setSelectedItem(make[0]);
    	    	modelCB.setSelectedItem(model[0]);
    	    	engineCB.setSelectedItem(engine[0]);
    	    	fuelCB.setSelectedItem(fuel[0]);
    	    	ageCB.setSelectedItem(age[0]);
    	    	colourCB.setSelectedItem(colour[0]);
    	    	carsList.setVisible(false);//the text area visibility is set to false
    	    	carsList.setText("");
    	    	lblList.setVisible(false);
    	    	lblList.setText("");
    	    	// the search array values are set to 0
    	    	for(int y=0;y<15;y++){
    	    		userCar.Search[y]=0;
    	    	}
    	    	//the carResult array values are set to 0
    	    	for (int i=0;i<15;i++){
    	    		for(int j=0;j<9;j++){
    	    			carResult[i][j]="0";
    	    		}
    	    	}
    	    		
    
    		}
    		//actions happen when the exit buttons on the first and the second window
    		if (arg0.getSource()==btnExit){
    			System.exit(-1); 
    		}
    		if (arg0.getSource()==btnExitsec){
    			System.exit(-1); 
    		}
    	}//end of the actionPreformed mehtod
    }//end of class

  13. #13
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    1. What happen/not happen when you click btnNext?
    2. What do you want happen/ do not want happen when you click btnNext?

  14. #14
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    well there are 2 buttons one is called Next and the second one is called Enter
    what i want to do is when the user clicks button Next it checks the textfield if there is an input button then btnEnter.setEnabled(true); and if there is no input then a message is displayed telling the user to enter input in the text fields
    what is happening now is whenever i click button Next the btnEnter.setEnabled(true); whether there is input in the text fields or not

  15. #15
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Try to also check if the textfield is empty. I am not an expert but base on
    what I have read on some tutorials blank string is different to null string
    Java Code:
        String firstString = "";
        //is different from
        String firstString = null;
    you just have to check the textfield if it is null OR empty
    Java Code:
        [b]EXMAPLE:[/b]
        if(firstString == null || firstString.isEmpty())
        {   //do something
        }
    Good Luck!

  16. #16
    AniMaind is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    thanx man that did help
    but now i have a different problem
    can you check this part from the code above
    Java Code:
    lblName=new JLabel("Hello   Mr/Mrs : "+nameField.getText());
    			lblName.setForeground(Color.BLACK);
    			lblName.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblName);
    
    			lblPhone=new JLabel("Phone Number : "+phoneField.getText());
    			lblPhone.setForeground(Color.BLACK);
    			lblPhone.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblPhone);
    
    			lblEmail=new JLabel("Email : "+emailField.getText());
    			lblEmail.setForeground(Color.BLACK);
    			lblEmail.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblEmail);
    i need to print the information from the JTextFields in these JLabels but it's not doing that

  17. #17
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Java Code:
                            lblName=new JLabel("Hello   Mr/Mrs : "+nameField.getText());
    			lblName.setForeground(Color.BLACK);
    			lblName.setPreferredSize(new Dimension(240,30));
    			infoPanel.add(lblName);
      . . .
    It looks like you are setting these labels at creation time while the textfields are still empty.
    Be sure the labels aren't created before the text is entered into the textfields. You can use an actionlistener for each textfield that re-creates the corresponding label with the additional info.

  18. #18
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    hosscomp is correct. You are trying to give value to lblName on creation time, and at that time nameField is empty or null.
    but you can set the Text at runtime (even after the creation) using setText() method.

    Java Code:
    lblName.setText("Your text here");

Similar Threads

  1. Desperate for some help if possible :(
    By SBOSlayer in forum New To Java
    Replies: 1
    Last Post: 11-30-2010, 10:44 PM
  2. In Desperate need for a good swing website
    By Loop in forum New To Java
    Replies: 4
    Last Post: 09-30-2008, 02:23 PM
  3. Multithreading + Networking (desperate)
    By bluebarca in forum New To Java
    Replies: 1
    Last Post: 11-07-2007, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •