Results 1 to 5 of 5
  1. #1
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default Problem with displaying search results from an array

    I have a program which takes in certain information from a user an does that perfect. My only problem is when I don't know how to do search for the certain flight id and then display all of the records in that search. Bellow I have enclosed the program and if anybody knows how to do this search please show me.

    Java Code:
    
    public class FlightReservationMain extends javax.swing.JPanel {
    	private static javax.swing.JTable jTable;
    	private javax.swing.JScrollPane jScrollPane;
    	private javax.swing.JPanel jPanel1;
    	private javax.swing.JPanel jPanel2;
    	private javax.swing.JButton jButton1;
    	private javax.swing.JButton jButton2;
    	private javax.swing.JButton jButton3;
    	private javax.swing.JButton jButton4;
    	private javax.swing.JButton jButton5;
    	private static int rowCnt = 0;
    	private static int selectedRow;
    
    	public FlightReservationMain() {
    		jTable = new javax.swing.JTable(new AbstractTable());
    
    		//Table Column size
    		javax.swing.table.TableColumn column = null;
    		for(int i = 0; i < 5; i++) {
    			column = jTable.getColumnModel().getColumn(i);
    			if (i == 0) {
    				column.setPreferredWidth(10);
    			} else if(i == 1) {
    				column.setPreferredWidth(150);
    			} else if(i == 3) {
    				column.setPreferredWidth(100);
    			} else {
    				column.setPreferredWidth(100);
    			}
    		}
    
    		jScrollPane = new javax.swing.JScrollPane(jTable);
    
    		jPanel1 = new javax.swing.JPanel(new java.awt.BorderLayout());
    
    		jPanel1.add(jScrollPane, java.awt.BorderLayout.CENTER);
    
    		jButton1 = new javax.swing.JButton("Add Entry");
    		jButton2 = new javax.swing.JButton("Update");
    		jButton3 = new javax.swing.JButton("Delete");
    		jButton4 = new javax.swing.JButton("Close");
    		jButton5 = new javax.swing.JButton("Search");
    
    		jPanel2 = new javax.swing.JPanel(new java.awt.FlowLayout());
    
    		jPanel2.add(jButton1);
    		jPanel2.add(jButton2);
    		jPanel2.add(jButton3);
    		jPanel2.add(jButton4);
    		jPanel2.add(jButton5);
    
    		jButton1.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				new AddEntry().setVisible(true);
    			}
    		});
    
    		jButton2.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				new UpdateEntry(jTable.getValueAt(getSelectedRow(), 0).toString(),
    								jTable.getValueAt(getSelectedRow(), 1).toString(),
    								jTable.getValueAt(getSelectedRow(), 2).toString(),
    								jTable.getValueAt(getSelectedRow(), 3).toString(),
    								jTable.getValueAt(getSelectedRow(), 4).toString()).setVisible(true);
    			}
    		});
    
    		jButton3.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				removeEntry();
    			}
    		});
    
    		jButton4.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				setVisible(true);
    				System.exit(0);
    			}
    		});
    
    		jButton5.addActionListener(new java.awt.event.ActionListener() {
    					public void actionPerformed(java.awt.event.ActionEvent e) {
    
    					}
    		});
    
    		jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);
    		jPanel1.setPreferredSize(new java.awt.Dimension(750, 300));
    
    		add(jPanel1);
    	}
    
    	public static void addEntry(FlightReservation flightReservation) {
    		jTable.setValueAt(flightReservation.getFlightId(), rowCnt, 0);
    		jTable.setValueAt(flightReservation.getName(), rowCnt, 1);
    		jTable.setValueAt(flightReservation.getAddress(), rowCnt, 2);
    		jTable.setValueAt(flightReservation.getTelNo(), rowCnt, 3);
    		jTable.setValueAt(flightReservation.getEmail(), rowCnt, 4);
    
    		rowCnt++;
    	}
    
    	public static void editEntry(FlightReservation flightReservation) {
    		jTable.setValueAt(flightReservation.getFlightId(), getSelectedRow(), 0);
    		jTable.setValueAt(flightReservation.getName(), getSelectedRow(), 1);
    		jTable.setValueAt(flightReservation.getAddress(), getSelectedRow(), 2);
    		jTable.setValueAt(flightReservation.getTelNo(), getSelectedRow(), 3);
    		jTable.setValueAt(flightReservation.getEmail(), getSelectedRow(), 4);
    	}
    
    	public void removeEntry() {
    		for(int i = getSelectedRow(); i < rowCnt; i++) {
    			jTable.setValueAt(jTable.getValueAt(i + 1, 0), i, 0);
    			jTable.setValueAt(jTable.getValueAt(i + 1, 1), i, 1);
    			jTable.setValueAt(jTable.getValueAt(i + 1, 2), i, 2);
    			jTable.setValueAt(jTable.getValueAt(i + 1, 3), i, 3);
    			jTable.setValueAt(jTable.getValueAt(i + 1, 3), i, 4);
    		}
    		rowCnt--;
    	}
    
    	public static int getSelectedRow() {
    		jTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    
    		javax.swing.ListSelectionModel rowSel = jTable.getSelectionModel();
    		rowSel.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
    			public void valueChanged(javax.swing.event.ListSelectionEvent e) {
    				if (e.getValueIsAdjusting()) return;
    
    				javax.swing.ListSelectionModel sel = (javax.swing.ListSelectionModel)e.getSource();
    				if (!sel.isSelectionEmpty()) {
    					selectedRow = sel.getMinSelectionIndex();
    				}
    			}
    		});
    
    		return selectedRow;
    	}
    
    	class AbstractTable extends javax.swing.table.AbstractTableModel {
    		private String[] columnNames = { "Flight Id", "Name", "Address", "Tel No.", "Email" };
    		private Object[][] data = new Object[100][6];
    
    		public int getColumnCount() {
    			return columnNames.length;
    		}
    
    		public int getRowCount() {
    			return data.length;
    		}
    
    		public String getColumnName(int col) {
    			return columnNames[col];
    		}
    
    		public Object getValueAt(int row, int col) {
    			return data[row][col];
    		}
    
    		public void setValueAt(Object value, int row, int col) {
    			data[row][col] = value;
    			fireTableCellUpdated(row, col);
    		}
    	}
    
    	public static void main(String args[]) {
    		javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);
    
    		javax.swing.JFrame jFrame = new javax.swing.JFrame("Flight Reservation System by Maja Julardzija");
    
    		jFrame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    
    		jFrame.setResizable(false);
    
    		jFrame.getContentPane().add(new FlightReservationMain());
    
    		jFrame.pack();
    		jFrame.setVisible(true);
    	}
    }
    Java Code:
    
    
    public class FlightReservation {
    	private String flightid;
    	private String name;
    	private String address;
    	private String telNo;
    	private String email;
    
    	public FlightReservation() {
    		flightid = "";
    		name = "";
    		address = "";
    		telNo = "";
    		email = "";
    	}
    
    	public FlightReservation(String flightid, String name, String address, String telNo, String email) {
    		this.flightid = flightid;
    		this.name = name;
    		this.address = address;
    		this.telNo = telNo;
    		this.email = email;
    	}
    
    	//setters
    	public void setFlightId(String flightid) {
    			this.flightid = flightid;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    
    	public void setTelNo(String telNo) {
    		this.telNo = telNo;
    	}
    
    	public void setEmail(String email) {
    		this.email = email;
    	}
    
    	//getters
    	public String getFlightId() {
    			return flightid;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public String getAddress() {
    		return address;
    	}
    
    	public String getTelNo() {
    		return telNo;
    	}
    
    	public String getEmail() {
    		return email;
    	}
    }
    Java Code:
    
    public class UpdateEntry extends javax.swing.JFrame {
    	private javax.swing.JLabel jLabel0;
    	private javax.swing.JLabel jLabel1;
    	private javax.swing.JLabel jLabel2;
    	private javax.swing.JLabel jLabel3;
    	private javax.swing.JLabel jLabel4;
    	private javax.swing.JTextField jTextField0;
    	private javax.swing.JTextField jTextField1;
    	private javax.swing.JTextField jTextField2;
    	private javax.swing.JTextField jTextField3;
    	private javax.swing.JTextField jTextField4;
    	private javax.swing.JButton jButton1;
    	private javax.swing.JButton jButton2;
    	private javax.swing.JPanel jPanel0;
    	private javax.swing.JPanel jPanel1;
    	private javax.swing.JPanel jPanel2;
    	private javax.swing.JPanel jPanel3;
    	private javax.swing.JPanel jPanel4;
    	private javax.swing.JPanel jPanel5;
    
    	public UpdateEntry(String flightid, String name, String address, String telNo, String email){
    		super("Add Contacr " + name);
    
    		setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
    
    		setResizable(true);
    
    		jLabel0 = new javax.swing.JLabel("Flight Id: ");
    		jLabel1 = new javax.swing.JLabel("Name: ");
    		jLabel2 = new javax.swing.JLabel("Address: ");
    		jLabel3 = new javax.swing.JLabel("Tel. No.: ");
    		jLabel4 = new javax.swing.JLabel("Email: ");
    
    		jTextField0 = new javax.swing.JTextField(10);
    		jTextField1 = new javax.swing.JTextField(15);
    		jTextField2 = new javax.swing.JTextField(30);
    		jTextField3 = new javax.swing.JTextField(10);
    		jTextField4 = new javax.swing.JTextField(15);
    
    		jTextField1.setText(flightid);
    		jTextField1.setText(name);
    		jTextField2.setText(address);
    		jTextField3.setText(telNo);
    		jTextField4.setText(email);
    
    		jButton1 = new javax.swing.JButton("Update");
    		jButton2 = new javax.swing.JButton("Cancel");
    
    		jPanel1 = new javax.swing.JPanel(new java.awt.GridLayout(5, 1, 1, 5));
    
    		jPanel1.add(jLabel0);
    		jPanel1.add(jLabel1);
    		jPanel1.add(jLabel2);
    		jPanel1.add(jLabel3);
    		jPanel1.add(jLabel4);
    
    		jPanel2 = new javax.swing.JPanel(new java.awt.GridLayout(5, 1));
    
    		jPanel2.add(jTextField0);
    		jPanel2.add(jTextField1);
    		jPanel2.add(jTextField2);
    		jPanel2.add(jTextField3);
    		jPanel2.add(jTextField4);
    
    		jPanel3 = new javax.swing.JPanel(new java.awt.FlowLayout());
    
    		jPanel3.add(jPanel1);
    		jPanel3.add(jPanel2);
    
    		jPanel4 = new javax.swing.JPanel(new java.awt.FlowLayout());
    
    		jPanel4.add(jButton1);
    		jPanel4.add(jButton2);
    
    		jButton1.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				FlightReservationMain.editEntry(new FlightReservation(jTextField0.getText(), jTextField1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText()));
    				setVisible(false);
    				dispose();
    			}
    		});
    
    		jButton2.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				setVisible(false);
    				dispose();
    			}
    		});
    
    		jPanel5 = new javax.swing.JPanel(new java.awt.BorderLayout());
    
    		jPanel5.add(jPanel3, java.awt.BorderLayout.CENTER);
    		jPanel5.add(jPanel4, java.awt.BorderLayout.SOUTH);
    
    		getContentPane().add(jPanel5);
    
    		pack();
    		setVisible(true);
    	}
    
    	public static void main(String args[]) {
    		javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);
    
    		new UpdateEntry("", "", "", "", "").setVisible(true);
    	}
    }
    Java Code:
    
    public class AddEntry extends javax.swing.JFrame {
    	private javax.swing.JLabel jLabel0;
    	private javax.swing.JLabel jLabel1;
    	private javax.swing.JLabel jLabel2;
    	private javax.swing.JLabel jLabel3;
    	private javax.swing.JLabel jLabel4;
    	private javax.swing.JTextField jTextField0;
    	private javax.swing.JTextField jTextField1;
    	private javax.swing.JTextField jTextField2;
    	private javax.swing.JTextField jTextField3;
    	private javax.swing.JTextField jTextField4;
    	private javax.swing.JButton jButton1;
    	private javax.swing.JButton jButton2;
    	private javax.swing.JPanel jPanel0;
    	private javax.swing.JPanel jPanel1;
    	private javax.swing.JPanel jPanel2;
    	private javax.swing.JPanel jPanel3;
    	private javax.swing.JPanel jPanel4;
    	private javax.swing.JPanel jPanel5;
    
    	public AddEntry(){
    		super("Add New Contact");
    
    		setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
    
    		setResizable(true);
    
    		jLabel0 = new javax.swing.JLabel("Flight Id: ");
    		jLabel1 = new javax.swing.JLabel("Name: ");
    		jLabel2 = new javax.swing.JLabel("Address: ");
    		jLabel3 = new javax.swing.JLabel("Tel. No.: ");
    		jLabel4 = new javax.swing.JLabel("Email: ");
    
    		jTextField0 = new javax.swing.JTextField(10);
    		jTextField1 = new javax.swing.JTextField(15);
    		jTextField2 = new javax.swing.JTextField(30);
    		jTextField3 = new javax.swing.JTextField(10);
    		jTextField4 = new javax.swing.JTextField(15);
    
    		jButton1 = new javax.swing.JButton("Add");
    		jButton2 = new javax.swing.JButton("Cancel");
    
    		jPanel1 = new javax.swing.JPanel(new java.awt.GridLayout(5, 1, 1, 5));
    
    		jPanel1.add(jLabel0);
    		jPanel1.add(jLabel1);
    		jPanel1.add(jLabel2);
    		jPanel1.add(jLabel3);
    		jPanel1.add(jLabel4);
    
    		jPanel2 = new javax.swing.JPanel(new java.awt.GridLayout(5, 1));
    
    		jPanel2.add(jTextField0);
    		jPanel2.add(jTextField1);
    		jPanel2.add(jTextField2);
    		jPanel2.add(jTextField3);
    		jPanel2.add(jTextField4);
    
    		jPanel3 = new javax.swing.JPanel(new java.awt.FlowLayout());
    
    		jPanel3.add(jPanel1);
    		jPanel3.add(jPanel2);
    
    		jPanel4 = new javax.swing.JPanel(new java.awt.FlowLayout());
    
    		jPanel4.add(jButton1);
    		jPanel4.add(jButton2);
    
    		jButton1.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				FlightReservationMain.addEntry(new FlightReservation(jTextField0.getText(), jTextField1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText()));
    			}
    		});
    
    		jButton2.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				setVisible(true);
    				dispose();
    			}
    		});
    
    		jPanel5 = new javax.swing.JPanel(new java.awt.BorderLayout());
    
    		jPanel5.add(jPanel3, java.awt.BorderLayout.CENTER);
    		jPanel5.add(jPanel4, java.awt.BorderLayout.SOUTH);
    
    		getContentPane().add(jPanel5);
    
    		pack();
    		setVisible(true);
    	}
    
    	public static void main(String args[]) {
    		javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);
    
    		new AddEntry().setVisible(true);
    	}
    }
    Java Code:
    
    public class SearchFlight extends javax.swing.JFrame {
    	private javax.swing.JLabel jLabel0;
    	private javax.swing.JLabel jLabel1;
    	private javax.swing.JTextField jTextField1;
    	private javax.swing.JTextArea jTextArea1;
    	private javax.swing.JButton jButton1;
    	private javax.swing.JButton jButton2;
    	private javax.swing.JPanel jPanel0;
    	private javax.swing.JPanel jPanel1;
    	private javax.swing.JPanel jPanel2;
    	private javax.swing.JPanel jPanel3;
    	private javax.swing.JPanel jPanel4;
    	private javax.swing.JPanel jPanel5;
    
    	public SearchFlight(){
    		super("Search by Flight Id");
    
    		setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
    
    		setResizable(true);
    
    		jLabel0 = new javax.swing.JLabel("Flight Id: ");
    		jLabel1 = new javax.swing.JLabel("Results: ");
    
    
    		jTextField1 = new javax.swing.JTextField(10);
    		jTextArea1 = new javax.swing.JTextArea(0,10);
    		jTextArea1.setLineWrap(true);
    
    
    		jButton1 = new javax.swing.JButton("Search");
    		jButton2 = new javax.swing.JButton("Cancel");
    
    		jPanel1 = new javax.swing.JPanel(new java.awt.GridLayout(2, 1, 1, 2));
    
    		jPanel1.add(jLabel0);
    		jPanel1.add(jLabel1);
    
    
    		jPanel2 = new javax.swing.JPanel(new java.awt.GridLayout(2, 2));
    
    		jPanel2.add(jTextField1);
    		jPanel2.add(jTextArea1);
    
    
    		jPanel3 = new javax.swing.JPanel(new java.awt.FlowLayout());
    
    		jPanel3.add(jPanel1);
    		jPanel3.add(jPanel2);
    
    		jPanel4 = new javax.swing.JPanel(new java.awt.FlowLayout());
    
    		jPanel4.add(jButton1);
    		jPanel4.add(jButton2);
    
    		jButton1.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    
    			}
    		});
    
    		jButton2.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    				setVisible(true);
    				dispose();
    			}
    		});
    
    		jPanel5 = new javax.swing.JPanel(new java.awt.BorderLayout());
    
    		jPanel5.add(jPanel3, java.awt.BorderLayout.CENTER);
    		jPanel5.add(jPanel4, java.awt.BorderLayout.SOUTH);
    
    		getContentPane().add(jPanel5);
    
    		pack();
    		setVisible(true);
    	}
    
    	public static void main(String args[]) {
    		javax.swing.JFrame.setDefaultLookAndFeelDecorated(true);
    
    		new SearchFlight().setVisible(true);
    	}
    }
    Thank you
    Last edited by BHCluster; 04-23-2008 at 05:23 PM.

  2. #2
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    This is alot of code.. you should be more specific. I still dont know whats the problem with the code.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes BHCluster, can you more specific with your question. :) It's too long for me to check at least :(

  4. #4
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    My main problem is I dont know how to search from FlightReservationMain.java
    the code bellow is part where all of the data is being stored.
    Java Code:
    private String[] columnNames = { "Flight Id", "Name", "Address", "Tel No.", "Email" };
    		private Object[][] data = new Object[100][6];
    
    		public int getColumnCount() {
    			return columnNames.length;
    		}
    
    		public int getRowCount() {
    			return data.length;
    		}
    
    		public String getColumnName(int col) {
    			return columnNames[col];
    		}
    
    		public Object getValueAt(int row, int col) {
    			return data[row][col];
    		}
    
    		public void setValueAt(Object value, int row, int col) {
    			data[row][col] = value;
    			fireTableCellUpdated(row, col);
    		}
    	}
    And I am trying to search it with this code

    Java Code:
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent e) {
    
    
    
    			}
    		});
    Please Help

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Array Search Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:45 PM
  2. date and calender not getting the right results
    By valoyivd in forum New To Java
    Replies: 4
    Last Post: 04-14-2008, 11:51 AM
  3. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 PM
  4. problem displaying GUI from jbase
    By diveshsingh in forum AWT / Swing
    Replies: 0
    Last Post: 01-30-2008, 08:07 PM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:08 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
  •