Add/Edit Visitor

JFrame frame = new JFrame("Login");
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
frame.dispose();
dashboardFrame.setVisible(true);
}
});
frame.pack();
Util.centeredFrame(frame, winWidth,winHeight, "Add Product");
frame.setVisible(true);
frame.setResizable(false);
Container mainContainer=frame.getContentPane();
mainContainer.setBackground(Color.orange);
After creation of window, we will add a canvas background.
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setOpaque(false);
panel.setBackground(Color.BLACK);
panel.setSize(new Dimension(winWidth-winWidth/20,winHeight-winHeight/10));
panel.setLocation(winWidth/50,winHeight/40);
mainContainer.add(panel,BorderLayout.CENTER);
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);
RenderingHints qualityHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON );
qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints( qualityHints);
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);
//Draws the rounded panel with borders.
g2.setColor(Color.GRAY);
g2.setColor(getBackground());
g2.setColor(getForeground());
g2.drawRoundRect(setX, setY, width-1, height-1, 80, 80); //paint border
}
}
JPanel header = Util.createPanel(winWidth*91/100,winHeight/13,new Point(winWidth/50,winHeight/40), true,"");
panel.add(header,BorderLayout.NORTH);
header.setLayout(new BorderLayout());
JPanel action = Util.createPanel(winWidth*91/100,winHeight/8,new Point(winWidth/50,winHeight/8), true,"Action");
panel.add(action,BorderLayout.NORTH);
action.setLayout(null);
JPanel detailsvisitor = Util.createPanel(winWidth*91/100,winHeight*60/100,new Point(winWidth/50,winHeight*27/100), true,"Visitors Details");
panel.add(detailsvisitor,BorderLayout.WEST);
detailsvisitor.setLayout(null);
//head
heading=new JLabel();
heading.setText("Add/Edit Visitor");
heading.setForeground(Color.black);
heading.setFont(new Font("Lucida",Font.PLAIN,30));
heading.setSize(new Dimension(winWidth/3,winHeight/10));
heading.setHorizontalAlignment(JLabel.CENTER);
header.add(heading,BorderLayout.CENTER);
Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder usernametittle = BorderFactory.createTitledBorder(loweredetched, "Visitor Name");
usernametittle.setTitleJustification(TitledBorder.LEFT);
TitledBorder phoneTittle = BorderFactory.createTitledBorder(loweredetched, "Contact No");
phoneTittle.setTitleJustification(TitledBorder.LEFT);
TitledBorder searchTittle = BorderFactory.createTitledBorder(loweredetched, "Search");
searchTittle.setTitleJustification(TitledBorder.LEFT);
TitledBorder flat_no_border = BorderFactory.createTitledBorder(loweredetched, "Flat No");
flat_no_border.setTitleJustification(TitledBorder.LEFT);
int space=winWidth/15;
int posY=winHeight/22;
name=new JLabel();
name.setText("Enter Visitor Name:");
name.setForeground(Color.black);
name.setFont(new Font("Lucida",Font.PLAIN,12));
name.setSize(new Dimension(winWidth/5,winHeight/16));
name.setLocation(space/2, 2);
action.add(name);
myTextField = new JTextField();
myTextField.setSize(new Dimension(winWidth/5,winHeight/15));
myTextField.setLocation(space/2,posY);
myTextField.setBorder(usernametittle);
action.add(myTextField,BorderLayout.SOUTH);
phone=new JLabel();
phone.setText("Enter Contact No:");
phone.setForeground(Color.black);
phone.setFont(new Font("Lucida",Font.PLAIN,12));
phone.setSize(new Dimension(winWidth/5,winHeight/16));
phone.setLocation(winWidth/5+space, 2);
action.add(phone);
phonetextField = new JTextField();
phonetextField.setSize(new Dimension(winWidth/5,winHeight/15));
phonetextField.setLocation(winWidth/5+space,posY);
phonetextField.setBorder(phoneTittle);
action.add(phonetextField,BorderLayout.SOUTH);
flat_no=new JLabel();
flat_no.setText("Select Flat No:");
flat_no.setForeground(Color.black);
flat_no.setFont(new Font("Lucida",Font.PLAIN,12));
flat_no.setSize(new Dimension(winWidth/5,winHeight/16));
flat_no.setLocation((winWidth/5)*2+space*3/2, 2);
action.add(flat_no);
getTableFlatListData();
flat_status_choice_field = new JComboBox(flat_status_choice.toArray());
flat_status_choice_field.setSize(new Dimension(winWidth/5,winHeight/15));
flat_status_choice_field.setLocation((winWidth/7)*3+space*2,posY);
flat_status_choice_field.setBackground(Color.white);
flat_status_choice_field.setSelectedItem("");
flat_status_choice_field.setLocation((winWidth/5)*2+space*3/2,posY);
flat_status_choice_field.setBorder(flat_no_border);
flat_status_choice_field.setEditable(true);
action.add(flat_status_choice_field,BorderLayout.SOUTH);
JButton addvisitorButton = new JButton(new ImageIcon
(((new ImageIcon("button3.png")).getImage()).getScaledInstance(150,35, java.awt.Image.SCALE_SMOOTH)));
addvisitorButton.setContentAreaFilled(false);
addvisitorButton.setBorder(null);
addvisitorButton.setText("Add Visitor");
addvisitorButton.setForeground(Color.white);
addvisitorButton.setFont(new Font("Lucida",Font.PLAIN,16));
addvisitorButton.setHorizontalTextPosition(JButton.CENTER);
addvisitorButton.setVerticalTextPosition(JButton.CENTER);
addvisitorButton.setBounds((winWidth/5)*3+space*5/2-10,posY+4,150,35);
addvisitorButton.setVisible(true);
addvisitorButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
action.add(addvisitorButton);
Here is logic of to perfome action when click "Add Visitor" Button.
addvisitorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
String nameString="";
String contactString="";
String flat_no_String="";
String var="";
if(myTextField.getText().isEmpty()){
var=var+"Name should not be Empty, ";
}else{
nameString=myTextField.getText().toString();
}
if(phonetextField.getText().isEmpty()){
var=var+"Contact No. should not be Empty, ";
}else{
contactString=phonetextField.getText().toString();
}
if(flat_status_choice_field.getSelectedItem().equals("")){
var=var+"Flat No should not be Empty ";
}else{
flat_no_String=flat_status_choice_field.getSelectedItem().toString();
}
if(var==""){
boolean isSussessFullEntered=tableIntryField(nameString,contactString,flat_no_String);
if(isSussessFullEntered){
myTextField.setText("");
phonetextField.setText("");
flat_status_choice_field.setSelectedItem("");
displaylistToTable(table,getTableListData());
JOptionPane.showMessageDialog(frame, "Successfull Enterd");
}else{
JOptionPane.showMessageDialog(frame, "Not Enterd");
}
}else {
JOptionPane.showMessageDialog(frame, var);
}
}
});
Here we add "UPDATE" button in action layout,when click "EDIT" it is visible else it is invisible.
JButton updateButton = new JButton(new ImageIcon(((new ImageIcon("button3.png")).getImage()).
getScaledInstance(150,35, java.awt.Image.SCALE_SMOOTH)));
updateButton.setContentAreaFilled(false);
updateButton.setBorder(null);
updateButton.setText("Update");
updateButton.setForeground(Color.white);
updateButton.setFont(new Font("Lucida",Font.PLAIN,16));
updateButton.setHorizontalTextPosition(JButton.CENTER);
updateButton.setVerticalTextPosition(JButton.CENTER);
updateButton.setBounds((winWidth/5)*3+space*5/2-10,posY+4,150,35);
updateButton.setVisible(false);
updateButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
action.add(updateButton);
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String nameString="";
String contactString="";
String flat_no_String="";
String var="";
if(myTextField.getText().isEmpty()){
var=var+"Name should not be Empty, ";
}else{
nameString=myTextField.getText().toString();
}
if(phonetextField.getText().isEmpty()){
var=var+"Contact No. should not be Empty, ";
}else{
contactString=phonetextField.getText().toString();
}
if(flat_status_choice_field.getSelectedItem().equals("")){
var=var+"Flat No should not be Empty ";
}else{
flat_no_String=flat_status_choice_field.getSelectedItem().toString();
}
if(var==""){
addvisitorButton.setVisible(true);
String generatedID=table.getValueAt(table.getSelectedRow(), 0).toString();
boolean isSussessFullEntered=updatetableIntryField(generatedID,nameString,contactString,flat_no_String);
if(isSussessFullEntered){
myTextField.setText("");
phonetextField.setText("");
flat_status_choice_field.setSelectedItem("");
displaylistToTable(table,getTableListData());
JOptionPane.showMessageDialog(frame, "Successfull Updated");
}else{
JOptionPane.showMessageDialog(frame, "Not Update");
}
}else{
JOptionPane.showMessageDialog(frame, var);
}
}
});
Now we create Visitor details layout UI.
//cancle Button
cancleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addvisitorButton.setVisible(true);
editButton.setVisible(false);
exitButton.setVisible(false);
cancleButton.setVisible(false);
updateButton.setVisible(false);
myTextField.setText("");
phonetextField.setText("");
flat_status_choice_field.setSelectedItem("");
}
});
//Table for visitor history
table = new JTable();
displaylistToTable(table,getTableListData());
table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
JTableHeader tableHeader21 = table.getTableHeader();
tableHeader21.setDefaultRenderer(new KeepSortIconHeaderRenderer(tableHeader21.getDefaultRenderer()));
JScrollPane pane = new JScrollPane(table);
pane.setLocation(30, winHeight/8);
pane.setSize(new Dimension(winWidth*86/100,winHeight*45/100));
detailsvisitor.add(pane);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
if(null!=editButton && null!=exitButton){
editButton.setVisible(true);
exitButton.setVisible(true);
}
}
});
//Search Bar
search=new JLabel();
search.setText("Search visitor");
search.setForeground(Color.black);
search.setFont(new Font("Lucida",Font.PLAIN,12));
search.setSize(new Dimension(winWidth/4,winHeight/16));
search.setLocation(30, 4);
detailsvisitor.add(search);
searchTextField = new JTextField();
searchTextField.setSize(new Dimension(winWidth/4,winHeight/16));
searchTextField.setLocation(30,winHeight/20);
searchTextField.setBorder(searchTittle);
detailsvisitor.add(searchTextField);
searchTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
search(searchTextField.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
search(searchTextField.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
search(searchTextField.getText());
}
public void search(String str) {
if (str.length() == 0) {
displaylistToTable(table,getTableListData());
} else {
defaultdataModel.setRowCount(0);
displaylistToTable(table,getselectedTableListData(str));
}
}
});
//edit Button
editButton = new JButton(new ImageIcon(((new ImageIcon("button3.png")).getImage()).getScaledInstance(150, 35, java.awt.Image.SCALE_SMOOTH)));
editButton.setContentAreaFilled(false);
editButton.setBorder(null);
editButton.setText("Edit");
editButton.setForeground(Color.white);
editButton.setFont(new Font("Lucida",Font.PLAIN,16));
editButton.setHorizontalTextPosition(JButton.CENTER);
editButton.setVerticalTextPosition(JButton.CENTER);
editButton.setBounds(winWidth*60/100,40,150,35);
detailsvisitor.add(editButton);
editButton.setVisible(false);
editButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addvisitorButton.setVisible(false);
cancleButton.setVisible(true);
updateButton.setVisible(true);
myTextField.setText(table.getValueAt(table.getSelectedRow(), 1).toString());
phonetextField.setText(table.getValueAt(table.getSelectedRow(), 2).toString());
flat_status_choice_field.setSelectedItem(table.getValueAt(table.getSelectedRow(), 3).toString());
}
});
//Exit button
exitButton=new JButton(new ImageIcon(((new ImageIcon("button3.png")).getImage()).getScaledInstance(150, 35, java.awt.Image.SCALE_SMOOTH)));
exitButton.setContentAreaFilled(false);
exitButton.setBorder(null);
exitButton.setText("Exit Visitor");
exitButton.setForeground(Color.white);
exitButton.setFont(new Font("Lucida",Font.PLAIN,16));
exitButton.setHorizontalTextPosition(JButton.CENTER);
exitButton.setVerticalTextPosition(JButton.CENTER);
exitButton.setBounds(winWidth*60/100+200,40,150,35);
detailsvisitor.add(exitButton);
exitButton.setVisible(false);
exitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
editButton.setVisible(false);
exitButton.setVisible(false);
boolean isSuccessdelete=exit_updateDataFromTable(table.getValueAt(table.getSelectedRow(), 0).toString(),
table.getValueAt(table.getSelectedRow(), 6).toString());
if(isSuccessdelete){
JOptionPane.showMessageDialog(null, "Visitor Exited.");
displaylistToTable(table,getTableListData());
table.clearSelection();
editButton.setVisible(false);
exitButton.setVisible(false);
}else{
JOptionPane.showMessageDialog(null, "Could not Exit.");
}
}
});
panel.revalidate();
panel.repaint();
Here is logic for show visitor details table .
public void displaylistToTable(JTable table,ArrayList list){
defaultdataModel=new DefaultTableModel(){
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
Object []row=new Object[7];
defaultdataModel.setColumnIdentifiers(new Object[]{"ID","Name","Contact No","Flat No","Entry Time","Exit Time","Status"});
for (int i = 0; i < list.size(); i++) {
row[0]=list.get(i).getVisitor_id();
row[1]=list.get(i).getVisitor_name();
row[2]=list.get(i).getVisitor_contact_no();
row[3]=list.get(i).getVisiting_flat_no();
row[4]=list.get(i).getEntry_time();
row[5]=list.get(i).getExit_time();
row[6]=list.get(i).getStatus();
defaultdataModel.addRow(row);
}
int tableWidth=(winWidth*86/100)/7;
table.setModel(defaultdataModel);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
table.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
table.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
table.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
table.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
table.getColumnModel().getColumn(4).setCellRenderer( centerRenderer );
table.getColumnModel().getColumn(5).setCellRenderer( centerRenderer );
table.getColumnModel().getColumn(6).setCellRenderer( centerRenderer );
int[] columnsWidth = {tableWidth, tableWidth, tableWidth, tableWidth, tableWidth,tableWidth,tableWidth};
int i = 0;
for (int width : columnsWidth) {
TableColumn column = table.getColumnModel().getColumn(i++);
column.setMinWidth(width);
column.setMaxWidth(width);
column.setPreferredWidth(width);
}
table.setRowHeight(30);
}
public void getTableFlatListData(){
ResultSet rs;
String query = "SELECT flat_no FROM flat_detail;" ;
Connection conn = MY_DB.ConnectDb();
Statement stat = null;
try {
stat = conn.createStatement();
rs = stat.executeQuery(query);
while(rs.next()){
flat_status_choice.add(rs.getString("flat_no"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Here is logic of fetch visitor data from database and update visitor details table:
public ArrayList getTableListData(){
ArrayList list=new ArrayList();
ResultSet rs;
String query = "SELECT * FROM visitor_detail;" ;
Connection conn = MY_DB.ConnectDb();
Statement stat = null;
try {
stat = conn.createStatement();
rs = stat.executeQuery(query);
VisitorData visitorData;
while(rs.next()){
visitorData=new VisitorData(rs.getString("visitor_id"),
rs.getString("visitor_name"),
rs.getString("visitor_contact_no"),
rs.getString("visiting_flat_no"),
rs.getString("entry_time"),
rs.getString("exit_time"),
rs.getString("status"));
list.add(visitorData);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return list;
}
here's logic of get selected data from table and Visbile in Action layout visitor details field.
public ArrayList getselectedTableListData(String data){
ArrayList visitorList=new ArrayList();
ResultSet rs;
String query = "SELECT * FROM visitor_detail WHERE visitor_name LIKE '%" + data + "%' or visiting_flat_no LIKE '%" + data + "%' " ;
Connection conn = MY_DB.ConnectDb();
Statement stat = null;
try {
stat = conn.createStatement();
rs = stat.executeQuery(query);
VisitorData visitordata;
while(rs.next()){
visitordata=new VisitorData(rs.getString("visitor_id"), rs.getString("visitor_name"),
rs.getString("visitor_contact_no"), rs.getString("visiting_flat_no"),
rs.getString("entry_time"), rs.getString("exit_time"),
rs.getString("status"));
visitorList.add(visitordata);
}
rs.close();
stat.close();
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return visitorList;
}
Here is logic for Add visitor Details in database.
public boolean tableIntryField(String nameString,String contactString,String flatNoString) {
Connection conn=MY_DB.ConnectDb();
boolean issuccess=false;
Statement stmt = null;
int action;
String sql = "INSERT INTO visitor_detail (visitor_name, visitor_contact_no, visiting_flat_no, entry_time, exit_time, status) " +
"VALUES (?,?,?,?,?,?);";
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = conn.prepareStatement(sql);
pst.setString(1, nameString);
pst.setString(2, contactString);
pst.setString(3, flatNoString);
pst.setString(4, Util.getDateTime());
pst.setString(5, "-");
pst.setString(6, "IN");
action = pst.executeUpdate();
if(action>0){
issuccess= true;
}else{
issuccess= false;
}
} catch (Exception a) {
a.printStackTrace();
} finally {
if(rs != null){
try{
rs.close();
} catch(Exception e){
e.printStackTrace();
}
}
if(pst != null){
try{
pst.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
return issuccess;
}
Here is logic for exit visitor when selected visitor when visitor out and update in database.
protected boolean exit_updateDataFromTable(String id, String visitor_status) {
boolean issuccess=false;
if(visitor_status.equalsIgnoreCase("out")) {
JOptionPane.showMessageDialog(null, "Visitor already exited");
}
else {
Connection conn=MY_DB.ConnectDb();
String sql = "UPDATE visitor_detail SET exit_time = ?, status = ? WHERE visitor_id = ?";
try {
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString( 1, Util.getDateTime());
stmt.setString( 2, "OUT");
stmt.setString( 3, id);
int action =stmt.executeUpdate();
if(action>0){
issuccess= true;
}else{
issuccess= false;
}
stmt.close();
conn.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
}
return issuccess;
}
At last here is simple class of VisitorData
public class VisitorData {
private String visitor_id;
private String visitor_name;
private String visitor_contact_no;
private String visiting_flat_no;
private String entry_time;
private String exit_time;
private String status;
public VisitorData(String visitor_id, String visitor_name, String visitor_contact_no,
String visiting_flat_no, String entry_time, String exit_time, String status) {
super();
this.setVisitor_id(visitor_id);
this.setVisitor_name(visitor_name);
this.setVisitor_contact_no(visitor_contact_no);
this.setVisiting_flat_no(visiting_flat_no);
this.setEntry_time(entry_time);
this.setExit_time(exit_time);
this.setStatus(status);
}
public String getVisitor_id() {
return visitor_id;
}
public void setVisitor_id(String visitor_id) {
this.visitor_id = visitor_id;
}
public String getVisitor_name() {
return visitor_name;
}
public void setVisitor_name(String visitor_name) {
this.visitor_name = visitor_name;
}
public String getVisitor_contact_no() {
return visitor_contact_no;
}
public void setVisitor_contact_no(String visitor_contact_no) {
this.visitor_contact_no = visitor_contact_no;
}
public String getVisiting_flat_no() {
return visiting_flat_no;
}
public void setVisiting_flat_no(String visiting_flat_no) {
this.visiting_flat_no = visiting_flat_no;
}
public String getEntry_time() {
return entry_time;
}
public void setEntry_time(String entry_time) {
this.entry_time = entry_time;
}
public String getExit_time() {
return exit_time;
}
public void setExit_time(String exit_time) {
this.exit_time = exit_time;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
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.