Add Flight

Add Flight

Above UI we are going to create by Awt for Add new flight 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.

    
		addFlightFrame = new JFrame();
		Utils.centeredFrame(addFlightFrame, windowWidth, windowHeight, "Add/Edit Flight");
		addFlightFrame.setResizable(false);
		
		Container frameContainer = addFlightFrame.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 JLabel and JSeparator for heading.

    
		// for heading Layout
		posY = panelHeight * 2/100;
		Font headingFont = new Font("Serif", Font.PLAIN, 22);
		// header
		Border borderline = BorderFactory.createLineBorder(Color.black);
				
		JLabel heading_text = new JLabel("Add/Edit Flight");
		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 / 15);
		panel.add(heading_text);
				
		panel.add(Utils.getSeparator(0, panelHeight / 13, panelWidth, 5,"000000"));			
         

Here we create a Base JPanel and some entry fields like JLabel, JTextFields and adding buttons .

    
	// Action table Base panel
	
		int actionPanelWidth = panelWidth*35/100;
		int actionPanelHeight = panelHeight*86/100;
	    actionPanel = new JPanel();
	    actionPanel.setBorder(Utils.getTitledBorder(10, "Action"));
	    //controlPanel.add(new MyCanvas());
	    actionPanel.setBounds(0,panelHeight*10/100, actionPanelWidth, actionPanelHeight);
	    actionPanel.setLayout(null);
	    actionPanel.setName("Action");
	    panel.add(actionPanel);
	    
	    
	    Font textFont = new Font("Serif",0,12);
	    JLabel flightNameL = new JLabel("Flight Name");
	    flightNameL.setAlignmentX(flightNameL.LEFT_ALIGNMENT);
	    flightNameL.setAlignmentY(flightNameL.LEFT_ALIGNMENT);
	    flightNameL.setForeground(Color.black);
	    flightNameL.setFont(textFont);
	    flightNameL.setBounds(actionPanelWidth*15/100,actionPanelHeight*8/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    flightNameL.setBackground(Color.black);
	    actionPanel.add(flightNameL);
	    
	    flightNameField = new JTextField();
	    flightNameField.setForeground(Color.black);
	    flightNameField.setBounds(actionPanelWidth*15/100,actionPanelHeight*10/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    flightNameField.setOpaque(false);
	    flightNameField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    flightNameField.setBackground(null);
	    actionPanel.add(flightNameField);
	    
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*18/100,actionPanelWidth*70/100,5,"000000"));
	    
	    JLabel routeL = new JLabel("Flight Route");
	    routeL.setAlignmentX(routeL.LEFT_ALIGNMENT);
	    routeL.setAlignmentY(routeL.LEFT_ALIGNMENT);
	    routeL.setForeground(Color.black);
	    routeL.setFont(textFont);
	    routeL.setBounds(actionPanelWidth*15/100,actionPanelHeight*21/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    routeL.setBackground(Color.black);
	    actionPanel.add(routeL);
	    
	    routeField = new JTextField();
	    routeField.setForeground(Color.black);
	    routeField.setBounds(actionPanelWidth*15/100,actionPanelHeight*23/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    routeField.setOpaque(false);
	    routeField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    routeField.setBackground(null);
	    actionPanel.add(routeField);
	    
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*31/100,actionPanelWidth*70/100,5,"000000"));

	    
	    JLabel deptTimeL = new JLabel("Departure Time");
	    deptTimeL.setAlignmentX(deptTimeL.LEFT_ALIGNMENT);
	    deptTimeL.setAlignmentY(deptTimeL.LEFT_ALIGNMENT);
	    deptTimeL.setForeground(Color.black);
	    deptTimeL.setFont(textFont);
	    deptTimeL.setBounds(actionPanelWidth*15/100,actionPanelHeight*34/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    deptTimeL.setBackground(Color.black);
	    actionPanel.add(deptTimeL);
	    
	    deptTimeField = new JTextField();
	    deptTimeField.setForeground(Color.black);
	    deptTimeField.setBounds(actionPanelWidth*15/100,actionPanelHeight*36/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    deptTimeField.setOpaque(false);
	    deptTimeField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    deptTimeField.setBackground(null);
	    actionPanel.add(deptTimeField);
	    
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*44/100,actionPanelWidth*70/100,5,"000000"));
	    
	    
	    JLabel arrvTimeL = new JLabel("Arrival Time");
	    arrvTimeL.setAlignmentX(arrvTimeL.LEFT_ALIGNMENT);
	    arrvTimeL.setAlignmentY(arrvTimeL.LEFT_ALIGNMENT);
	    arrvTimeL.setForeground(Color.black);
	    arrvTimeL.setFont(textFont);
	    arrvTimeL.setBounds(actionPanelWidth*15/100,actionPanelHeight*47/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    arrvTimeL.setBackground(Color.black);
	    actionPanel.add(arrvTimeL);
	    
	    arrvTimeLField = new JTextField();
	    arrvTimeLField.setForeground(Color.black);
	    arrvTimeLField.setBounds(actionPanelWidth*15/100,actionPanelHeight*49/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    arrvTimeLField.setOpaque(false);
	    arrvTimeLField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    arrvTimeLField.setBackground(null);
	    actionPanel.add(arrvTimeLField);
	    
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*57/100,actionPanelWidth*70/100,5,"000000"));
	    
	    JLabel fareL = new JLabel("Fare");
	    fareL.setAlignmentX(fareL.LEFT_ALIGNMENT);
	    fareL.setAlignmentY(fareL.LEFT_ALIGNMENT);
	    fareL.setForeground(Color.black);
	    fareL.setFont(textFont);
	    fareL.setBounds(actionPanelWidth*15/100,actionPanelHeight*60/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    fareL.setBackground(Color.black);
	    actionPanel.add(fareL);
	    
	    fareLField = new JTextField();
	    fareLField.setForeground(Color.black);
	    fareLField.setBounds(actionPanelWidth*15/100,actionPanelHeight*62/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    fareLField.setOpaque(false);
	    fareLField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    fareLField.setBackground(null);
	    actionPanel.add(fareLField);
	    
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*70/100,actionPanelWidth*70/100,5,"000000"));

	    
	    addFlightBtn(actionPanelWidth, actionPanelHeight);
		
		
		actionPanel.add(getUpdateButton(actionPanelWidth, actionPanelHeight));
		
		actionPanel.add(getCancleButton(actionPanelWidth, actionPanelHeight));
	   
     

Here we define "AddFlight","Update","Cancle" button with click functionality

    
	private void addFlightBtn(int width, int height) {
		
		addFlightBtn = new JButton(new ImageIcon(((new ImageIcon("images/button40.png")).getImage()).getScaledInstance(width*60/100, 40, java.awt.Image.SCALE_SMOOTH)));
		addFlightBtn.setContentAreaFilled(false);
		addFlightBtn.setBorder(null);
		addFlightBtn.setText("ADD FLIGHT");
		addFlightBtn.setHorizontalTextPosition(JButton.CENTER);
		addFlightBtn.setVerticalTextPosition(JButton.CENTER);
		addFlightBtn.setBounds(width*20/100,height*80/100, width*60/100, height*10/100); 
		addFlightBtn.setOpaque(false);
		addFlightBtn.setContentAreaFilled(false);
		addFlightBtn.setBorderPainted(false);
		addFlightBtn.setFont(new java.awt.Font("Serif",1, 15));
		addFlightBtn.setForeground(Color.white);
		actionPanel.add(addFlightBtn);
	    
		
		addFlightBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String var = "";
				boolean is_name_entered = false;
				boolean is_route_entered = false;
				boolean is_dept_time_entered = false;
				boolean is_arrv_time_entered = false;
				boolean is_fare_entered = false;
				
				 String flightId = "F";
				String name = flightNameField.getText();
				String route = routeField.getText();
				String deptTime = deptTimeField.getText();
				String arrvTime = arrvTimeLField.getText();
				String fare = fareLField.getText();

				int id_count = 0;
				String key = "FLIGHT_LAST_COUNT";
				
				if(name.isEmpty()) {
					var += "Name Should not be empty, ";
		        }else {
		        	is_name_entered = true;
		        }
				
				if(route.isEmpty()) {
					var += "Route Should not be empty, ";
		        }else {
		        	is_route_entered = true;
		        }
				
				if(deptTime.isEmpty()) {
					var += "Departure Time Should not be empty, ";
		        }else {
		        	is_dept_time_entered = true;
		        }
				
				if(arrvTime.isEmpty()) {
					var += "Arrival Time Should not be empty, ";
		        }else {
		        	is_arrv_time_entered = true;
		        }
				
				if(fare.isEmpty()) {
					var += "Fare Should not be empty, ";
		        }else {
		        	is_fare_entered = true;
		        }
				
				
				
				if(is_name_entered && is_route_entered && is_dept_time_entered && is_arrv_time_entered && is_fare_entered) {
				conn = SqliteConnection.ConnectDb();
				
				try {
					
					insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'FLIGHT_LAST_COUNT'");
					ResultSet rs = insert.executeQuery();
					ResultSetMetaData rsmd = rs.getMetaData();
					 
					 while (rs.next()) {    
						 id_count = rs.getInt(1)+1;  
				        }
					 DecimalFormat df = new DecimalFormat("000");
					 flightId = flightId+df.format(id_count);
				} catch (SQLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
				
				
				try {
					insert = conn.prepareStatement("insert into flight_detail(flight_no,flight_name,route,departure_timing,arrival_timing,fare)values(?,?,?,?,?,?)");
					insert.setString(1, flightId);
					insert.setString(2, name);
					insert.setString(3, route);
					insert.setString(4, deptTime);
					insert.setString(5, arrvTime);
					insert.setString(6, fare);
					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();
				}
				
				flight_table_update();
				flightNameField.setText("");
	            routeField.setText("");
	            deptTimeField.setText("");
	            arrvTimeLField.setText("");
	            fareLField.setText("");
	            
	            var = "Flight has been added successfully";
				JOptionPane.showMessageDialog(null, var);
	            
			}else {
				JOptionPane.showMessageDialog(null, var);
			}
					
			}
		});
	}
	
	private JButton getUpdateButton(int width, int height) {
		
		updateBtn = new JButton(new ImageIcon(((new ImageIcon("images/button40.png")).getImage()).getScaledInstance(width*40/100, height*8/100, java.awt.Image.SCALE_SMOOTH)));
		updateBtn.setContentAreaFilled(false);
		updateBtn.setBorder(null);
		updateBtn.setText("UPDATE");
		updateBtn.setHorizontalTextPosition(JButton.CENTER);
		updateBtn.setVerticalTextPosition(JButton.CENTER);
		updateBtn.setBounds(width*5/100,height*80/100, width*40/100, height*8/100); 
		updateBtn.setFont(new java.awt.Font("Serif",1, 15));
		updateBtn.setOpaque(false);
		updateBtn.setContentAreaFilled(false);
		updateBtn.setForeground(Color.white);
		updateBtn.setVisible(false);
		
		updateBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				

				 	String flightId = flightTable.getValueAt(flightTable.getSelectedRow(),0).toString();
				 	String name = flightNameField.getText();
					String route = routeField.getText();
					String deptTime = deptTimeField.getText();
					String arrvTime = arrvTimeLField.getText();
					String fare = fareLField.getText();

					
					conn = SqliteConnection.ConnectDb();
					
					try {
						
						insert = conn.prepareStatement("UPDATE flight_detail SET flight_name = ?, route = ?, departure_timing = ?, arrival_timing = ?, fare = ?  WHERE flight_no = ?");
						insert.setString(1, name);
						insert.setString(2, route);
						insert.setString(3, deptTime);
						insert.setString(4, arrvTime);
						insert.setString(5, fare);
						insert.setString(6, flightId);
						insert.executeUpdate();
						conn.close();
						System.out.println(flightId );
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					flight_table_update();
					addFlightBtn.setVisible(true);
					editBtn.setVisible(true);
					updateBtn.setVisible(false);
					cancleBtn.setVisible(false);
					
					flightNameField.setText("");
		            routeField.setText("");
		            deptTimeField.setText("");
		            arrvTimeLField.setText("");
		            fareLField.setText("");
				
			}
		});
	    
		return updateBtn;		
	}
	
	private JButton getCancleButton(int width, int height) {
		
		cancleBtn = new JButton(new ImageIcon(((new ImageIcon("images/button40.png")).getImage()).getScaledInstance(width*40/100, height*8/100, java.awt.Image.SCALE_SMOOTH)));
		cancleBtn.setContentAreaFilled(false);
		cancleBtn.setBorder(null);
		cancleBtn.setText("CANCEL");
		cancleBtn.setHorizontalTextPosition(JButton.CENTER);
		cancleBtn.setVerticalTextPosition(JButton.CENTER);
		cancleBtn.setBounds(width*55/100,height*80/100, width*40/100, height*8/100); 
		cancleBtn.setFont(new java.awt.Font("Serif",1, 15));
		cancleBtn.setOpaque(false);
		cancleBtn.setContentAreaFilled(false);
		cancleBtn.setForeground(Color.white);
		cancleBtn.setVisible(false);
		
		cancleBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				addFlightBtn.setVisible(true);
				editBtn.setVisible(true);
				updateBtn.setVisible(false);
				cancleBtn.setVisible(false);
				
				flightNameField.setText("");
	            routeField.setText("");
	            deptTimeField.setText("");
	            arrvTimeLField.setText("");
	            fareLField.setText("");

			}
		});
	    
		return cancleBtn;		
	}
	

	

Here we create a flight detail base JPanel and some entry fields like JLabel, JTextFields and adding buttons .

    
		int flightPanelWidth = panelWidth*64/100;
		int flightPanelHeight = panelHeight*86/100;
	    flightPanel = new JPanel();
	    flightPanel.setLayout(new FlowLayout());
	    flightPanel.setLayout(null);
	    flightPanel.setBounds(panelWidth*36/100,panelHeight*10/100, flightPanelWidth, flightPanelHeight);
	    flightPanel.setBorder(Utils.getTitledBorder(10, "Search"));
	    panel.add(flightPanel);
	    
	    headingFont = new Font("Serif",Font.BOLD,16);
	    JLabel flight_search_L = new JLabel("Search Flight");
	    flight_search_L.setAlignmentX(JLabel.LEFT);
	    //searchLabel.setBackground(Color.decode("#80c2b2"));
	    flight_search_L.setForeground(Color.black);
	    flight_search_L.setFont(headingFont);
	    flight_search_L.setBounds(20,10, 200, 30);
	    flightPanel.add(flight_search_L);
	    
	    
	    
	    ImageIcon backround_img1 = new ImageIcon("images/ic_search.png");
		Image img1 = backround_img1.getImage();
		Image temp_img1 = img1.getScaledInstance(20, 20, Image.SCALE_SMOOTH);
		backround_img1 = new ImageIcon(temp_img1);
		JLabel background1 = new JLabel("", backround_img1, JLabel.CENTER);
		background1.setBounds(20,40, 20, 20);
		flightPanel.add(background1);
	    
	    JTextField flightSearch = new JTextField();
	    flightSearch.setBackground(Color.decode("#FFFFFF"));
	    flightSearch.setForeground(Color.black);
	    flightSearch.setBounds(40,40, 150, 20);
	    flightSearch.setOpaque(false);
	    flightSearch.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    flightSearch.setBackground(null);
	    flightPanel.add(flightSearch);
	    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) {
	               sorter2.setRowFilter(null);
	            } else {
	               sorter2.setRowFilter(RowFilter.regexFilter(str));
	            }
	         }
	      });
	    
	    flightPanel.add(Utils.getSeparator(20, 60, 170, 2,"000000"));	

		flight_table(flightPanelWidth, flightPanelHeight);
	    
	    flightPanel.add(getEditButton(flightPanelWidth, flightPanelHeight));
	    
	    flightPanel.add(getDeleteButton(flightPanelWidth, flightPanelHeight));

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

	

Here we define "Edit","Delete" button with click functionality

    
	private JButton getEditButton(int width, int height) {
		
		
		
		  
		editBtn = new JButton(new ImageIcon(((new ImageIcon("images/button40.png")).getImage()).getScaledInstance(width*22/100, height*8/100, java.awt.Image.SCALE_SMOOTH)));
		editBtn.setContentAreaFilled(false);
		editBtn.setBorder(null);
		editBtn.setText("EDIT");
		editBtn.setHorizontalTextPosition(JButton.CENTER);
		editBtn.setVerticalTextPosition(JButton.CENTER);
		editBtn.setBounds(width*50/100,height*3/100, width*22/100, height*8/100); 
		editBtn.setFont(new java.awt.Font("Serif",1, 12));
		editBtn.setForeground(Color.white);
		editBtn.setVisible(false);
		
		editBtn.addActionListener(new ActionListener() {
			 
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				addFlightBtn.setVisible(false);
				editBtn.setVisible(false);
				updateBtn.setVisible(true);
				cancleBtn.setVisible(true);
				
				flightNameField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),1).toString());
	            routeField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),2).toString());
	            deptTimeField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),3).toString());
	            arrvTimeLField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),4).toString());
	            fareLField.setText(flightTable.getValueAt(flightTable.getSelectedRow(),5).toString());

			}
		});
	    
		return editBtn;		
	}
	
	private JButton getDeleteButton(int width, int height) {
		
		deleteBtn = new JButton(new ImageIcon(((new ImageIcon("images/button40.png")).getImage()).getScaledInstance(width*22/100, height*8/100, java.awt.Image.SCALE_SMOOTH)));
		deleteBtn.setContentAreaFilled(false);
		deleteBtn.setBorder(null);
		deleteBtn.setText("DELETE");
		deleteBtn.setHorizontalTextPosition(JButton.CENTER);
		deleteBtn.setVerticalTextPosition(JButton.CENTER);
		deleteBtn.setBounds(width*75/100,height*3/100, width*22/100, height*8/100); 
		deleteBtn.setFont(new java.awt.Font("Serif",1, 12));
		deleteBtn.setForeground(Color.white);
		deleteBtn.setVisible(false);
		
		deleteBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String flight_id = flightTable.getValueAt(flightTable.getSelectedRow(),0).toString();
				String flight_name = flightTable.getValueAt(flightTable.getSelectedRow(),1).toString();
				System.out.println(flight_id + "- "+ flight_name);
				
				conn = SqliteConnection.ConnectDb();
				
	            try {
					
					insert = conn.prepareStatement("delete from flight_detail where flight_no= ?");
					insert.setString(1, flight_id);
					insert.executeUpdate();
					conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	            
	            flight_table_update();
	            editBtn.setVisible(false);
	            deleteBtn.setVisible(false);
			}
		});
	    
		return deleteBtn;		
	}
	
	

Here in this panel we creating a Tabel

    
	" public class AddFlight implements TableModelListener"

	@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+data+"Wrong");
	}

	private void flight_table(int width, int height) {		
		
		// TODO Auto-generated method stub
		String[] columnNames = {"Flight No.",
                "Flight Name",
                "Flight Route",
                "Departure Time",
                "Arrival Time",
                "Fare"
               	};
		
		DefaultTableModel model1 = new DefaultTableModel();
		sorter2 = new TableRowSorter<>(model1);

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

		JScrollPane scrollPane1 = new JScrollPane(flightTable);
		scrollPane1.setBounds(width*2/100,height*15/100, width*96/100,height*80/100);
		flightTable.setFillsViewportHeight(true);
		flightPanel.add(scrollPane1);
		
		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(2).setPreferredWidth(100);
		flightTable.getColumnModel().getColumn(5).setPreferredWidth(30);

		
		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());
	            editBtn.setVisible(true);
	            deleteBtn.setVisible(true);
	            
	            
	           
	        }
	    });

		flight_table_update();
	
	}
	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();
		}
		
	}
	

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.