Search Student

Search Student

Above UI we are going to create by AWT for Search Student in our Bank management 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.

        
    JFrame frame = new JFrame("Search Student");
	Utils.centeredFrame(frame, winWidth, winHeight, "Search Student");
	frame.setResizable(false);
	Container mainContainer=frame.getContentPane();
	mainContainer.setBackground(Color.gray);

	int panelWid = winWidth-winWidth/20;
	int panelHig = winHeight-winHeight/10;

	JPanel panel = new JPanel();
	panel.setLayout(null);
	panel.setOpaque(false);
	panel.setBackground(Color.white);
	panel.setSize(new Dimension(winWidth-winWidth/20,winHeight-winHeight/10));
	panel.setLocation(winWidth/50,winHeight/40);
	mainContainer.add(panel,BorderLayout.CENTER);
    

After creation of window, we will add a canvas background.

    
    JComponent jcomponent=new Background(winWidth/50,winHeight/40,(winWidth-winWidth/20),winHeight-winHeight/10);// 
	mainContainer.add(jcomponent,BorderLayout.CENTER);
    

    public class Background extends JComponent {
	
		int width, height,setX,setY;

    	public Background (int setX,int setY,int width, int height) {
    		this.width=width;
    		this.height=height;
    		this.setX=setX;
    		this.setY=setY;
    	
       		setSize(width, height);
    	}
	
    	@Override
    	public void paint(Graphics g) {
        	Graphics2D g2 = (Graphics2D) g;

        	g2.setPaint(Color.white);

       

        	double x = setX;
        	double y = setY;
        	double w = width;
        	double h = height;
        	RoundRectangle2D round=new RoundRectangle2D.Double(x, y, w, h, 80, 80);
        	g2.fill(round);
        }    
	}

    

Once your window is created with custom background, we will add header.

    
    Font headingFont = new Font("Lucida",Font.BOLD,22);
	Font textFont = new Font("Lucida",0,10);
	Border borderline = BorderFactory.createLineBorder(Color.black);
	        
	        
	heading = new JLabel("Search Student");
	heading.setHorizontalAlignment(JLabel.CENTER);
	heading.setVerticalAlignment(JLabel.CENTER);
	heading.setBackground(Color.decode("#80c2b2"));
	heading.setForeground(Color.black);
	heading.setFont(headingFont);
	heading.setBounds(0,panelHig/30, panelWid, panelHig/15);
	        
	panel.add(heading);
	        
	        
	JSeparator jSeparator1 = new JSeparator();
	jSeparator1.setBackground(Color.black);
	jSeparator1.setAlignmentX(JSeparator.CENTER_ALIGNMENT);
	jSeparator1.setBounds(20, 80, winWidth-winWidth/12, panelHig/13);
	panel.add(jSeparator1);
    
    

Now create studentpanel layout and add Search box and Student Table UI.

    
    TitledBorder studentTittle = BorderFactory.createTitledBorder(borderline);
	studentTittle.setTitleJustification(TitledBorder.LEFT);
	studentTittle.setTitleFont(textFont);
	studentTittle.setTitle("Search Student");
	studentTittle.setTitleColor(Color.black);

	studentPanel = new JPanel();
	studentPanel.setBounds(50,120, winWidth-winWidth/10,550);
	studentPanel.setLayout(null);
	studentPanel.setName("Search Student");
	studentPanel.setBorder(studentTittle);
	studentPanel.setBackground(Color.white);
	panel.add(studentPanel);
	        
	        
	headingFont = new Font("Lucida",Font.BOLD,16);
	JLabel searchLabel = new JLabel("Search Student");
	searchLabel.setAlignmentX(JLabel.LEFT);
	searchLabel.setForeground(Color.black);
	searchLabel.setFont(headingFont);
	searchLabel.setBounds(30,40, 150, 30);
	studentPanel.add(searchLabel);
		    
	ImageIcon backround_img = new ImageIcon("images/ic_search.png");
	Image img = backround_img.getImage();
	Image temp_img = img.getScaledInstance(20, 20, Image.SCALE_SMOOTH);
	backround_img = new ImageIcon(temp_img);
	JLabel background = new JLabel("", backround_img, JLabel.CENTER);
	background.setBounds(30,70, 20, 20);
	studentPanel.add(background);
		    
	JTextField studentSearch = new JTextField(15);
	studentSearch.setBackground(Color.decode("#FFFFFF"));
	studentSearch.setForeground(Color.black);
	studentSearch.setBounds(50,70, 200, 20);
	studentPanel.add(studentSearch);
		   
	studentSearch.getDocument().addDocumentListener(new DocumentListener() {
		@Override
		public void insertUpdate(DocumentEvent e) {
			search(studentSearch.getText());
		}
		@Override
		public void removeUpdate(DocumentEvent e) {
		    search(studentSearch.getText());
		}
		@Override
		public void changedUpdate(DocumentEvent e) {
			search(studentSearch.getText());
		}
		public void search(String str) {
			if (str.length() == 0) {
		        sorter.setRowFilter(null);
		    } else {
		        sorter.setRowFilter(RowFilter.regexFilter(str));
		    }
		}
	});
		    
     
	student_table();
		    
	panel.revalidate();
	panel.repaint();
    

Here is student table .

    
	private void student_table() {	
		 
		int panelWid = winWidth-winWidth/20;
		int panelHig = winHeight-winHeight/10;
		   
		// TODO Auto-generated method stub
		String[] columnNames = {"Student Id",
				   "Name",
				   "Contact No",
				   "Email Id",
				   "Address",
				   "Room No",
				   "Alloted Date"
				   };
		   
		DefaultTableModel model = new DefaultTableModel();
		sorter = new TableRowSorter<>(model);

		model.setColumnIdentifiers(columnNames); 
		   
		stdentTable = new JTable();
		stdentTable.setRowSorter(sorter);
		stdentTable.setBounds(30,120, winWidth-winWidth/7, panelHig*50/100);
		
		studentPanel.add(stdentTable);
		stdentTable.setModel(model);

		JScrollPane scrollPane = new JScrollPane(stdentTable);
		scrollPane.setBounds(30,120, winWidth-winWidth/7,panelHig*50/100);
		stdentTable.setFillsViewportHeight(true);
		studentPanel.add(scrollPane);
		   
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		stdentTable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
		stdentTable.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
		stdentTable.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
		stdentTable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
		stdentTable.getColumnModel().getColumn(4).setCellRenderer( centerRenderer );
		stdentTable.getColumnModel().getColumn(5).setCellRenderer( centerRenderer );
		stdentTable.getColumnModel().getColumn(6).setCellRenderer( centerRenderer );
		   
	   
		stdentTable.getTableHeader().setFont(new Font("SansSerif", 1, 13));
		stdentTable.getTableHeader().setBackground(Color.decode("#4287f5"));
		stdentTable.getTableHeader().setForeground(Color.decode("#FFFFFF"));
		   
		stdentTable.getColumnModel().getColumn(0).setPreferredWidth(30);
		stdentTable.getColumnModel().getColumn(1).setPreferredWidth(30);
		stdentTable.getColumnModel().getColumn(4).setPreferredWidth(30);
		stdentTable.getColumnModel().getColumn(6).setPreferredWidth(30);
		stdentTable.setRowHeight(30);

		   
		student_table_update();
	   
    }
        

Here's the logic to fetch data from database and show in table.

    
	private void student_table_update() {		
			
		int CC;
		String room_no = null, std_id = null;
		String date = "";
			
			
		Connection conn = SQLiteJDBCDriverConnection.connect1();
		try {
				
			PreparedStatement insert = conn.prepareStatement("select student_id, student_name, contact_no, email_id, address  from student");
			ResultSet rs = insert.executeQuery();
				
			
			DefaultTableModel dft = (DefaultTableModel)stdentTable.getModel();
			dft.setRowCount(0);
				
			while(rs.next()) {
		
				Vector v2 = new Vector();
					
					
				v2.add(rs.getString("student_id"));
				v2.add(rs.getString("student_name"));
				v2.add(rs.getString("contact_no"));
				v2.add(rs.getString("email_id"));
				v2.add(rs.getString("address"));
					
				std_id = rs.getString("student_id");
					
				insert = conn.prepareStatement("select room_number, allotment_date from room_alloted where alloted_to LIKE'%"+std_id+"%'");
				ResultSet rs1 = insert.executeQuery();
					
				while(rs1.next()) {
					v2.add(rs1.getString("room_number"));
					v2.add(rs1.getString("allotment_date"));
				}
					
				dft.addRow(v2);
					
			}
				
			conn.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
			
	}

Here is logic dashboard visibility.


	frame.getContentPane().setBackground(Color.gray);
	        
	frame.addWindowListener(new WindowAdapter(){  
	    public void windowClosing(WindowEvent e) {  
	        frame.dispose();
	        dashboardFrame.setVisible(true);
	    }  
	});  
			
	frame.setVisible(true);
	
	

At last we add logic to table rows change their position.


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");
}

				

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.