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

    Default Problem with searching while using GUI

    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.


    The code bellow compiles and runs ythe reason why I have included it is so you see what I am doing.
    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);
    	}
    }
    What I am trying to do is use the SearchFilight.java to access FlightReservationMain.java so it displays results back in SearchFlight. If anybody know how to do this please help.
    Thank you

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public class FlightReservationMain extends JPanel {
        FlightReservationMain enclosingClassRef = this;
        private static JTable jTable;
        private JButton jButton5;
    
    public FlightReservationMain() {
        jButton5 = new JButton("Search");
        jButton5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SearchFlight search = new SearchFlight();
                // call methods/access fields in search...
                // If the SearchFlight class needs a reference to this
                // enclosing class, viz, FlightReservationMain, you can
                // send a reference to it via a (new) constructor:
                SearchFlight search = new SearchFlight(enclosingClassRef);
                // Or if SearchFlight needs only a reference to the table
                // you can send a reference to it along in a constructor:
                SearchFlight search = new SearchFlight(jTable);
                // or perhaps in a method:
                search.setTable(jTable);
            }
        });
    }

Similar Threads

  1. Searching XML file using DOM
    By simon in forum XML
    Replies: 5
    Last Post: 03-30-2009, 01:27 AM
  2. Searching an arraylist
    By adelgado0723 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 01:09 PM
  3. recursively searching through arraylists
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:36 AM
  4. searching within a JList
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 12:12 AM
  5. searching
    By nalinda in forum New To Java
    Replies: 3
    Last Post: 12-06-2007, 02:56 AM

Posting Permissions

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