Flight Booking

Flight Booking

Above UI we are going to create by Awt for Filght Booking Window in our Flight Booking System desktop application. For that first we create a window and configure its title, size and color. Here we are creating a window at Top Level so we can add this window at the top of any other window.

    
	// frame with control options
		bookingFrame = new JFrame();
		Utils.centeredFrame(bookingFrame, windowWidth, windowHeight, "Flight Booking");
		bookingFrame.setResizable(false);
		
		Container frameContainer = bookingFrame.getContentPane();
		frameContainer.setBackground(Color.gray);

		// Changeable According to What size of screen You Want?
		int containerWidth = windowWidth*94/100;
		int containerHeight = windowHeight*90/100;
		
		int panelWidth = containerWidth*98/100;
		int panelHeight = containerHeight*96/100;
		int posX = windowWidth / 2 - panelWidth / 2 ;
		int posY = windowHeight / 2 - panelHeight / 2 - frameControllerSize / 2;

		// frame without control options
		panel = new JPanel(); 
		panel.setLayout(null);//new BorderLayout()); 
		//panel.setOpaque(false);
		panel.setBackground(Color.decode("#FFFFFF")); 
		panel.setSize(new Dimension(panelWidth,panelHeight)); 
		panel.setLocation(posX, posY);
		frameContainer.add(panel);
		
		// for Rounded Background
		posX = windowWidth / 2 - containerWidth / 2 ;
		posY = windowHeight / 2 - containerHeight / 2 - frameControllerSize / 2;
		JComponent jcomponent = new RoundedBackground(posX, posY, containerWidth, containerHeight);
		jcomponent.setLayout(null);
		frameContainer.add(jcomponent, BorderLayout.CENTER);
		

    

Here we create a Label for our heading.

    
		// for heading Layout
		posY = panelHeight * 2/100;
		Font headingFont = new Font("Serif", Font.PLAIN, 22);
		// header
		//borderline = BorderFactory.createLineBorder(Color.black);
				
		JLabel heading_text = new JLabel("Flight Booking System");
		heading_text.setHorizontalAlignment(JLabel.CENTER);
		heading_text.setVerticalAlignment(JLabel.CENTER);
		heading_text.setBackground(Color.decode("#80c2b2"));
		heading_text.setForeground(Color.black);
		heading_text.setFont(headingFont);
		heading_text.setBounds(0,0, panelWidth, panelHeight / 20);
		panel.add(heading_text);
				
		panel.add(Utils.getSeparator(0, panelHeight / 18, panelWidth, 5,"000000"));	
        		

	

Here we create Flight Tabel Base JPanel and its UI & functionality.

    

				// flight table base panel
				Border borderline = BorderFactory.createLineBorder(Color.black);
				Font textFont = new Font("Serif",0,10);
			  

				int flightPanelWidth = panelWidth*70/100;
				int flightPanelHeight = panelHeight*45/100;
				flightPanel = new JPanel();
			    flightPanel.setLayout(new FlowLayout());
			    //controlPanel.add(new MyCanvas());
			    flightPanel.setBounds(0,panelHeight*6/100, flightPanelWidth, flightPanelHeight);
			    flightPanel.setLayout(null);
			    flightPanel.setName("Search Flight");
			    flightPanel.setBorder(Utils.getTitledBorder(10, "Search Flight"));
			    panel.add(flightPanel);
			    
			    headingFont = new Font("Serif",Font.BOLD,16);
			    JLabel searchLabel = new JLabel("Search Flight");
			    searchLabel.setAlignmentX(JLabel.LEFT);
			    searchLabel.setBackground(Color.decode("#80c2b2"));
			    searchLabel.setForeground(Color.black);
			    searchLabel.setFont(headingFont);
			    searchLabel.setBounds(flightPanelWidth*3/100,flightPanelHeight*6/100, flightPanelWidth*18/100, flightPanelHeight*8/100);
			    flightPanel.add(searchLabel);
			    
			    ImageIcon backround_img = new ImageIcon("images/ic_search.png");
				Image img = backround_img.getImage();
				Image temp_img = img.getScaledInstance(flightPanelWidth*3/100,flightPanelWidth*3/100, Image.SCALE_SMOOTH);
				backround_img = new ImageIcon(temp_img);
				JLabel background = new JLabel("", backround_img, JLabel.CENTER);
				background.setBounds(flightPanelWidth*3/100,flightPanelHeight*14/100, flightPanelWidth*3/100,flightPanelWidth*3/100 );
				flightPanel.add(background);
			    
			    JTextField flightSearch = new JTextField(15);
			    flightSearch.setBackground(Color.decode("#FFFFFF"));
			    flightSearch.setForeground(Color.black);
			    flightSearch.setBounds(flightPanelWidth*6/100,flightPanelHeight*14/100, flightPanelWidth*20/100, flightPanelWidth*3/100);
			    flightSearch.setOpaque(false);
			    flightSearch.setBorder(BorderFactory.createLineBorder(Color.white, 0));
			    flightSearch.setBackground(null);
			    flightPanel.add(flightSearch);
			    
			    flightPanel.add(Utils.getSeparator(flightPanelWidth*3/100, flightPanelHeight*22/100, flightPanelWidth*25/100, 2,"000000"));	

			   
			    flightSearch.getDocument().addDocumentListener(new DocumentListener() {
			         @Override
			         public void insertUpdate(DocumentEvent e) {
			            search(flightSearch.getText());
			         }
			         @Override
			         public void removeUpdate(DocumentEvent e) {
			            search(flightSearch.getText());
			         }
			         @Override
			         public void changedUpdate(DocumentEvent e) {
			            search(flightSearch.getText());
			         }
			         public void search(String str) {
			            if (str.length() == 0) {
			               sorter.setRowFilter(null);
			            } else {
			               sorter.setRowFilter(RowFilter.regexFilter(str));
			            }
			         }
			      });
			    

			    flight_table(flightPanelWidth, flightPanelHeight);
			    

         

Here we creating a base JPanel & Passenger Table and adding a button .

    
				// passenger table Base panel
			  
			    int passengerPanelWidth = panelWidth*70/100;
				int passengerPanelHeight = panelHeight*49/100;
			    passengerPanel = new JPanel();
			    passengerPanel.setLayout(new FlowLayout());
			    //passengerPanel.setBorder(borderline);
			    //controlPanel.add(new MyCanvas());
			    passengerPanel.setLayout(null);
			    passengerPanel.setBounds(0,panelHeight*51/100, passengerPanelWidth, passengerPanelHeight);
			    passengerPanel.setBorder(Utils.getTitledBorder(10, "Passengers"));
			    panel.add(passengerPanel);
			    
			   
			    passenger_table(passengerPanelWidth, passengerPanelHeight);
			    
			    
			    addPassengerBtn = new JButton(new ImageIcon(((new ImageIcon("images/button40.png")).getImage()).getScaledInstance(panelWidth*16/100, panelHeight*6/100, java.awt.Image.SCALE_SMOOTH)));
			    addPassengerBtn.setContentAreaFilled(false);
			    addPassengerBtn.setBorder(null);
			    addPassengerBtn.setText("ADD PASSENGER");
			    addPassengerBtn.setHorizontalTextPosition(JButton.CENTER);
			    addPassengerBtn.setVerticalTextPosition(JButton.CENTER);
			    addPassengerBtn.setBounds(passengerPanelWidth*70/100,passengerPanelHeight*85/100, panelWidth*16/100, panelHeight*6/100); 
			    addPassengerBtn.setFont(new java.awt.Font("Serif",1, 12));
			    addPassengerBtn.setOpaque(false);
			    addPassengerBtn.setContentAreaFilled(false);
			    addPassengerBtn.setBorderPainted(false);
			    addPassengerBtn.setForeground(Color.white);
			    passengerPanel.add(addPassengerBtn);    
			    
			    addPassengerBtn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						new AddPassenger(bookingFrame);
					}
				});
			    
     

Here we create JLabel and JTextField for flight deatils so, you can enter which route flight you want to travel.

    
				// Action table Base panel
			   
			   int actionPanelWidth = panelWidth*30/100;
			   int actionPanelHeight = panelHeight*94/100;
			   actionPanel = new JPanel();
			   actionPanel.setBorder(Utils.getTitledBorder(10, "Action"));
			   actionPanel.setBounds(panelWidth*70/100,panelHeight*6/100,actionPanelWidth, actionPanelHeight);
			   actionPanel.setLayout(null);

			   panel.add(actionPanel);
			   
			   // flight detail frame
			   flightDetailPanel = new JPanel();
			   flightDetailPanel.setBorder(Utils.getTitledBorder(10, ""));
			   flightDetailPanel.setBounds(actionPanelWidth*2/100,actionPanelHeight*3/100, actionPanelWidth*96/100, actionPanelHeight*38/100);
			   flightDetailPanel.setLayout(null);
			   actionPanel.add(flightDetailPanel);
			   
			   headingFont = new Font("Serif",Font.BOLD,15);

			   Label flight_detail_L = new Label("Flight Details");
			   flight_detail_L.setAlignment(Label.CENTER);
			   flight_detail_L.setForeground(Color.white);
			   flight_detail_L.setFont(headingFont);
			   flight_detail_L.setBounds(actionPanelWidth*2/100,actionPanelHeight*1/100, actionPanelWidth*92/100, actionPanelHeight*6/100);
			   flight_detail_L.setBackground(Color.decode("#f2410a"));
			   flightDetailPanel.add(flight_detail_L);
			   
			   textFont = new Font("Serif",0,12);

			   JLabel flightNameL = new JLabel("Flight Name:-");
			   flightNameL.setAlignmentX(flightNameL.CENTER_ALIGNMENT);
			   flightNameL.setAlignmentY(Component.CENTER_ALIGNMENT);
			   flightNameL.setForeground(Color.black);
			   flightNameL.setFont(textFont);
			   flightNameL.setBounds(actionPanelWidth*3/100,actionPanelHeight*8/100, actionPanelWidth*35/100, actionPanelHeight*5/100);
			   flightNameL.setBackground(Color.black);
			   flightDetailPanel.add(flightNameL);
			  
			   
			   flightNameField = new TextField();
			   flightNameField.setFocusable(false);
			   flightNameField.setForeground(Color.black);
			   flightNameField.setBounds(actionPanelWidth*40/100,actionPanelHeight*8/100, actionPanelWidth*53/100, actionPanelHeight*5/100);
			   flightDetailPanel.add(flightNameField);
			   
			   JLabel flightRouteL = new JLabel("Flight Route:-");
			   flightRouteL.setAlignmentX(flightRouteL.CENTER_ALIGNMENT);
			   flightRouteL.setAlignmentY(flightRouteL.CENTER_ALIGNMENT);
			   flightRouteL.setForeground(Color.black);
			   flightRouteL.setFont(textFont);
			   flightRouteL.setBounds(actionPanelWidth*3/100,actionPanelHeight*14/100, actionPanelWidth*35/100, actionPanelHeight*5/100);
			   flightRouteL.setBackground(Color.black);
			   flightDetailPanel.add(flightRouteL);
			   
			   flightRouteField = new TextField();
			   flightRouteField.setFocusable(false);
			   flightRouteField.setForeground(Color.black);
			   flightRouteField.setBounds(actionPanelWidth*40/100,actionPanelHeight*14/100, actionPanelWidth*53/100, actionPanelHeight*5/100);
			   flightDetailPanel.add(flightRouteField);
			   
			   JLabel deptTimeL = new JLabel("Departure Time:-");
			   deptTimeL.setAlignmentX(deptTimeL.CENTER_ALIGNMENT);
			   deptTimeL.setAlignmentY(deptTimeL.CENTER_ALIGNMENT);
			   deptTimeL.setForeground(Color.black);
			   deptTimeL.setFont(textFont);
			   deptTimeL.setBounds(actionPanelWidth*3/100,actionPanelHeight*20/100, actionPanelWidth*35/100, actionPanelHeight*5/100);
			   deptTimeL.setBackground(Color.black);
			   flightDetailPanel.add(deptTimeL);
			   
			   deptTimeField = new TextField();
			   deptTimeField.setFocusable(false);
			   deptTimeField.setForeground(Color.black);
			   deptTimeField.setBounds(actionPanelWidth*40/100,actionPanelHeight*20/100, actionPanelWidth*53/100, actionPanelHeight*5/100);
			   flightDetailPanel.add(deptTimeField);
			   
			   JLabel arrvTimeL = new JLabel("Arrival Time:-");
			   arrvTimeL.setAlignmentX(arrvTimeL.CENTER_ALIGNMENT);
			   arrvTimeL.setAlignmentY(arrvTimeL.CENTER_ALIGNMENT);
			   arrvTimeL.setForeground(Color.black);
			   arrvTimeL.setFont(textFont);
			   arrvTimeL.setBounds(actionPanelWidth*3/100,actionPanelHeight*26/100, actionPanelWidth*35/100, actionPanelHeight*5/100);
			   arrvTimeL.setBackground(Color.black);
			   flightDetailPanel.add(arrvTimeL);
			   
			   arrvTimeField = new TextField();
			   arrvTimeField.setFocusable(false);
			   arrvTimeField.setForeground(Color.black);
			   arrvTimeField.setBounds(actionPanelWidth*40/100,actionPanelHeight*26/100, actionPanelWidth*53/100, actionPanelHeight*5/100);
			   flightDetailPanel.add(arrvTimeField);
			   
			   JLabel fareL = new JLabel("Fare:-");
			   fareL.setAlignmentX(fareL.CENTER_ALIGNMENT);
			   fareL.setForeground(Color.black);
			   fareL.setFont(textFont);
			   fareL.setBounds(actionPanelWidth*3/100,actionPanelHeight*32/100, actionPanelWidth*35/100, actionPanelHeight*5/100);
			   fareL.setBackground(Color.black);
			   flightDetailPanel.add(fareL);
			   
			   fareField = new TextField();
			   fareField.setFocusable(false);
			   fareField.setForeground(Color.black);
			   fareField.setBounds(actionPanelWidth*40/100,actionPanelHeight*32/100, actionPanelWidth*53/100, actionPanelHeight*5/100);
			   flightDetailPanel.add(fareField);

	

Here we create JLabel and JTextField for passenger deatils so, you can enter all details for book a ticket.

    
				// passenger detail frame
			    passengerDetailPanel = new JPanel();
			    passengerDetailPanel.setBorder(borderline);
			    passengerDetailPanel.setBounds(actionPanelWidth*2/100,actionPanelHeight*42/100, actionPanelWidth*96/100, actionPanelHeight*26/100);
			    passengerDetailPanel.setLayout(null);
			    actionPanel.add(passengerDetailPanel);
			    
			    
			    Label passenger_detail_L = new Label("Passenger Details");
			    passenger_detail_L.setAlignment(Label.CENTER);
			    passenger_detail_L.setForeground(Color.white);
			    passenger_detail_L.setFont(headingFont);
			    passenger_detail_L.setBounds(actionPanelWidth*2/100,actionPanelHeight*1/100, actionPanelWidth*92/100, actionPanelHeight*6/100);
			    passenger_detail_L.setBackground(Color.decode("#f2410a"));
			    passengerDetailPanel.add(passenger_detail_L);
			    
			    textFont = new Font("Serif",0,12);

			    JLabel passengerNameL = new JLabel("Passenger Name:-");
			    passengerNameL.setAlignmentX(passengerNameL.CENTER_ALIGNMENT);
			    passengerNameL.setAlignmentY(passengerNameL.CENTER_ALIGNMENT);
			    passengerNameL.setForeground(Color.black);
			    passengerNameL.setFont(textFont);
			    passengerNameL.setBounds(actionPanelWidth*3/100,actionPanelHeight*8/100, actionPanelWidth*35/100, actionPanelHeight*5/100);
			    passengerNameL.setBackground(Color.black);
			    passengerDetailPanel.add(passengerNameL);
			    
			    passengerNameField = new TextField();
			    passengerNameField.setFocusable(false);
			    passengerNameField.setForeground(Color.black);
			    passengerNameField.setBounds(actionPanelWidth*40/100,actionPanelHeight*8/100, actionPanelWidth*53/100, actionPanelHeight*5/100);
			    passengerDetailPanel.add(passengerNameField);
			    
			    JLabel passengerContactL = new JLabel("Contact:-");
			    passengerContactL.setAlignmentX(passengerContactL.CENTER_ALIGNMENT);
			    passengerContactL.setAlignmentY(passengerContactL.CENTER_ALIGNMENT);
			    passengerContactL.setForeground(Color.black);
			    passengerContactL.setFont(textFont);
			    passengerContactL.setBounds(actionPanelWidth*3/100,actionPanelHeight*14/100, actionPanelWidth*35/100, actionPanelHeight*5/100);
			    passengerContactL.setBackground(Color.black);
			    passengerDetailPanel.add(passengerContactL);
			    
			    passengerContactField = new TextField();
			    passengerContactField.setFocusable(false);
			    passengerContactField.setForeground(Color.black);
			    passengerContactField.setBounds(actionPanelWidth*40/100,actionPanelHeight*14/100, actionPanelWidth*53/100, actionPanelHeight*5/100);
			    passengerDetailPanel.add(passengerContactField);
			    
			    JLabel seletedDateL = new JLabel("Seleted Date:-");
			    seletedDateL.setAlignmentX(seletedDateL.CENTER_ALIGNMENT);
			    seletedDateL.setAlignmentY(seletedDateL.CENTER_ALIGNMENT);
			    seletedDateL.setForeground(Color.black);
			    seletedDateL.setFont(textFont);
			    seletedDateL.setBounds(actionPanelWidth*3/100,actionPanelHeight*20/100, actionPanelWidth*35/100, actionPanelHeight*5/100);
			    seletedDateL.setBackground(Color.black);
			    passengerDetailPanel.add(seletedDateL);
			    
			    seletedDateField = new TextField();
			    seletedDateField.setFocusable(false);
			    seletedDateField.setForeground(Color.black);
			    seletedDateField.setBounds(actionPanelWidth*40/100,actionPanelHeight*20/100, actionPanelWidth*53/100, actionPanelHeight*5/100);
			    passengerDetailPanel.add(seletedDateField);
			    
	

Here we are adding a calander and bookflight button

    
    		actionPanel.add(getBookFlightBtn(panelWidth, panelHeight, actionPanelWidth, actionPanelHeight));
			    
			actionPanel.add(getCalenderBtn(panelWidth, panelHeight, actionPanelWidth, actionPanelHeight));


				bookingFrame.addWindowListener(new WindowAdapter(){  
		            public void windowClosing(WindowEvent e) { 
		            		           
		                bookingFrame.dispose();
		                dashboardFrame.setVisible(true);
		            }  
		        });  
				
				
				bookingFrame.setVisible(true);
                   
	

At last we will define here all methods used above code

    
	private JButton getBookFlightBtn(int panelWidth, int panelHeight, int actionPanelWidth, int actionPanelHeight) {
		bookFlightBtn = new JButton(new ImageIcon(((new ImageIcon("images/button40.png")).getImage()).getScaledInstance(panelWidth*16/100, panelHeight*6/100, java.awt.Image.SCALE_SMOOTH)));
		bookFlightBtn.setContentAreaFilled(false);
		bookFlightBtn.setBorder(null);
		bookFlightBtn.setText("BOOK FLIGHT");
		bookFlightBtn.setHorizontalTextPosition(JButton.CENTER);
		bookFlightBtn.setVerticalTextPosition(JButton.CENTER);
		bookFlightBtn.setBounds(actionPanelWidth*50/100 - panelWidth*8/100,actionPanelHeight*88/100, panelWidth*16/100, panelHeight*6/100); 
		bookFlightBtn.setFont(new java.awt.Font("Serif",1, 12));
		bookFlightBtn.setForeground(Color.white);

			bookFlightBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				
				
				String bookingId = "TB";
				String flightNo = "";
				String passengerId= "";
				String bookingDate = seletedDateField.getText();
				int id_count = 0; 
				String var = "";
				boolean is_flight_tree_focused = false;
				boolean is_passenger_tree_focused = false;
				boolean is_date_selected = false;

				String key = "BOOKING_LAST_COUNT";
				
				
				if(flightTable.getSelectionModel().isSelectionEmpty()) {
					var += "Flight Not Selected, ";
					
		        }else {
		        	flightNo = flightTable.getValueAt(flightTable.getSelectedRow(),0).toString();
		        	is_flight_tree_focused = true;
		        }
				
				if(passengerTable.getSelectionModel().isSelectionEmpty()) {
					var += "Passenger Not Selected, ";
					
		        }else {
		        	passengerId= passengerTable.getValueAt(passengerTable.getSelectedRow(),0).toString();
		        	is_passenger_tree_focused = true;
		        }
				
				if(bookingDate.isEmpty()) {
					var += "Date Not Selected, ";
					
		        }else {
		        	is_date_selected = true;
		        }
				
				if(is_flight_tree_focused && is_passenger_tree_focused && is_date_selected) {
					
					flightTable.clearSelection();
	            	passengerTable.clearSelection();

					conn = SqliteConnection.ConnectDb();

				
				try {
					
					insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'BOOKING_LAST_COUNT'");
					ResultSet rs = insert.executeQuery();
					ResultSetMetaData rsmd = rs.getMetaData();
					
					 while (rs.next()) {    
						 id_count = rs.getInt(1)+1;  
				        }

					 DecimalFormat df = new DecimalFormat("000");
					 bookingId = df.format(id_count);
				} catch (SQLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
				
				try {
					insert = conn.prepareStatement("insert into booking(booking_id,flight_no,passenger_id,booking_date)values(?,?,?,?)");
					insert.setString(1, bookingId);
					insert.setString(2, flightNo);
					insert.setString(3, passengerId);
					insert.setString(4, bookingDate);
					
					insert.executeUpdate();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				try {
					
					insert = conn.prepareStatement("UPDATE system_setting "
			                + "SET value = ? "
			                + "WHERE key = ?");
					insert.setInt(1, id_count);
					insert.setString(2, key);
					insert.executeUpdate();
					conn.close();
				} catch (SQLException e3) {
					// TODO Auto-generated catch block
					e3.printStackTrace();
				}
				
				resetPage();
				var = "Ticket Booked Successfully ";
				JOptionPane.showMessageDialog(null, var);

				
			}else {
				JOptionPane.showMessageDialog(null, var);
			}
				
		}
});
		
		return bookFlightBtn;
		
	
}
	
	
	
	private JButton getCalenderBtn(int panelWidth, int panelHeight, int actionPanelWidth, int actionPanelHeight) {
		calanderBtn = new JButton(new ImageIcon(((new ImageIcon("images/button40.png")).getImage()).getScaledInstance(panelWidth*16/100, panelHeight*6/100, java.awt.Image.SCALE_SMOOTH)));
		calanderBtn.setContentAreaFilled(false);
		calanderBtn.setBorder(null);
		calanderBtn.setText("SELECT DATE");
		calanderBtn.setHorizontalTextPosition(JButton.CENTER);
		calanderBtn.setVerticalTextPosition(JButton.CENTER);
		calanderBtn.setBounds(actionPanelWidth*50/100 - panelWidth*8/100,actionPanelHeight*75/100, panelWidth*16/100, panelHeight*6/100); 
		calanderBtn.setFont(new java.awt.Font("Serif",1, 12));
		calanderBtn.setForeground(Color.white);

		calanderBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				seletedDateField.setText(new DatePicker(actionPanel,panelWidth, panelHeight).setPickedDate());
				
		}
});
		
		return calanderBtn;
		
	
}

	private void resetPage() {
	
		
		flightNameField.setText("");
		flightRouteField.setText("");
		deptTimeField.setText("");
		arrvTimeField.setText("");
		fareField.setText("");
		passengerNameField.setText("");
		passengerContactField.setText("");
		seletedDateField.setText("");
		
		
	}

	
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		FlightBooking awtsample = new FlightBooking();
//		awtsample.bookingFrame.setVisible(true);
//	}

	@Override
	public void tableChanged(TableModelEvent e) {
		// TODO Auto-generated method stub
		int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel)e.getSource();
        String columnName = model.getColumnName(column);
        Object data = model.getValueAt(row, column);
        

       System.out.println(columnName+"Wrong");
	}
	
	private void flight_table_update() {		
		
		int CC;
		
		conn = SqliteConnection.ConnectDb();
		try {
			insert = conn.prepareStatement("select * from flight_detail");
			ResultSet rs = insert.executeQuery();
			
			ResultSetMetaData rsmd = rs.getMetaData();
			CC = rsmd.getColumnCount();
			
			DefaultTableModel dft = (DefaultTableModel)flightTable.getModel();
			dft.setRowCount(0);
			
			while(rs.next()) {
				Vector v2 = new Vector();
				
				for(int ii = 1; ii<=CC; ii++) {
					
					v2.add(rs.getString("flight_no"));
					v2.add(rs.getString("flight_name"));
					v2.add(rs.getString("route"));
					v2.add(rs.getString("departure_timing"));
					v2.add(rs.getString("arrival_timing"));
					v2.add(rs.getString("fare"));
				}
				dft.addRow(v2);
				
			}
			
			conn.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}

	private void flight_table(int width, int height) {		
		
				// TODO Auto-generated method stub
				String[] columnNames = {"Flight No.",
		                "Flight Name",
		                "Route",
		                "Departure Time",
		                "Arrival_Time",
		                "Fare"};
				
				DefaultTableModel model = new DefaultTableModel();
				sorter = new TableRowSorter<>(model);
	
		        model.setColumnIdentifiers(columnNames); 
				
				flightTable = new JTable() {
					public boolean isCellEditable(int row, int column) {                
		                return false;               
					};
				};
				flightTable.setRowSorter(sorter);
				flightTable.setBounds(width*2/100,height*25/100, width*96/100,height*70/100);
				//table.setEnabled(true);
				flightPanel.add(flightTable);
				flightTable.setModel(model);
	
				JScrollPane scrollPane = new JScrollPane(flightTable);
				scrollPane.setBounds(width*2/100,height*25/100, width*96/100,height*70/100);
				flightTable.setFillsViewportHeight(true);
				flightPanel.add(scrollPane);
				
				DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
				centerRenderer.setHorizontalAlignment( JLabel.CENTER );
				flightTable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
				flightTable.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
				flightTable.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
				flightTable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
				flightTable.getColumnModel().getColumn(4).setCellRenderer( centerRenderer );
				flightTable.getColumnModel().getColumn(5).setCellRenderer( centerRenderer );
				
			
				flightTable.getTableHeader().setFont(new Font("SansSerif", 1, 13));
				flightTable.getTableHeader().setBackground(Color.decode("#f2410a"));
				flightTable.getTableHeader().setForeground(Color.decode("#FFFFFF"));
				flightTable.getTableHeader().setPreferredSize(new Dimension(760, 35));
				flightTable.setRowHeight(25);
				flightTable.getColumnModel().getColumn(0).setPreferredWidth(30);
				flightTable.getColumnModel().getColumn(5).setPreferredWidth(30);
				flightTable.getColumnModel().getColumn(2).setPreferredWidth(100);
				
				flightTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
			        public void valueChanged(ListSelectionEvent event) {
			            // do some actions here, for example
			            // print first column value from selected row
			            System.out.println(flightTable.getSelectedRow());
			            if(!flightTable.getSelectionModel().isSelectionEmpty()) {
				            flightNameField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),1).toString());
				            flightRouteField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),2).toString());
				            deptTimeField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),3).toString());
				            arrvTimeField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),4).toString());
				            fareField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),5).toString());
			            }
			            
			            
			        }
			    });

				flight_table_update();
			
		}

	public static void passenger_table_update() {		
		
		int CC;
		
		Connection conn = SqliteConnection.ConnectDb();
		try {
			PreparedStatement insert = conn.prepareStatement("select * from passenger_detail");
			ResultSet rs = insert.executeQuery();
			
			ResultSetMetaData rsmd = rs.getMetaData();
			CC = rsmd.getColumnCount();
			
			DefaultTableModel dft = (DefaultTableModel)passengerTable.getModel();
			dft.setRowCount(0);
			
			while(rs.next()) {
				Vector v2 = new Vector();
				
				//for(int ii = 1; ii<=CC; ii++) {
					
					v2.add(rs.getString("passenger_id"));
					v2.add(rs.getString("passenger_name"));
					v2.add(rs.getString("contact_number"));
					v2.add(rs.getString("email_id"));
				//}
				dft.addRow(v2);
				
			}
			
			conn.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
	
	private void passenger_table(int width, int height) {		
		
		// TODO Auto-generated method stub
		String[] columnNames = {"Passenger Id",
                "Passenger Name",
                "Contact",
                "Email Id"
               	};
		
		DefaultTableModel model1 = new DefaultTableModel();
		sorter2 = new TableRowSorter<>(model1);

        model1.setColumnIdentifiers(columnNames); 
		
        passengerTable = new JTable(){
			public boolean isCellEditable(int row, int column) {                
                return false;               
			};
		};
        passengerTable.setBounds(width*2/100,height*10/100, width*96/100,height*70/100);
        passengerTable.setRowSorter(sorter2);
		//table.setEnabled(true);
		passengerPanel.add(passengerTable);
		passengerTable.setModel(model1);

		JScrollPane scrollPane1 = new JScrollPane(passengerTable);
		scrollPane1.setBounds(width*2/100,height*10/100, width*96/100,height*70/100);
		passengerTable.setFillsViewportHeight(true);
		passengerPanel.add(scrollPane1);
		
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		passengerTable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
		passengerTable.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
		passengerTable.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
		passengerTable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
		
	
		passengerTable.getTableHeader().setFont(new Font("SansSerif", 1, 13));
		passengerTable.getTableHeader().setBackground(Color.decode("#f2410a"));
		passengerTable.getTableHeader().setForeground(Color.decode("#FFFFFF"));
		passengerTable.getTableHeader().setPreferredSize(new Dimension(760, 35));
		passengerTable.setRowHeight(25);
		passengerTable.getColumnModel().getColumn(0).setPreferredWidth(30);
		passengerTable.getColumnModel().getColumn(3).setPreferredWidth(100);
		
		passengerTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
	        public void valueChanged(ListSelectionEvent event) {
	            // do some actions here, for example
	            // print first column value from selected row
	            System.out.println(passengerTable.getSelectedRow());
	            if(!passengerTable.getSelectionModel().isSelectionEmpty()) {

	            	passengerNameField.setText(passengerTable.getValueAt(passengerTable.getSelectedRow(),1).toString());
	            	passengerContactField.setText(passengerTable.getValueAt(passengerTable.getSelectedRow(),2).toString());
	            }
	        }
	    });

		passenger_table_update();
	
	}

                   
	

Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 2021 by Programmer Mitra. All Rights Reserved.