Search Customer

this.dashboardFrame = jframe;
dashboardFrame.setVisible(false);
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
windowWidth = (int) (screenSize.getWidth() *90 /100);
windowHeight = (int) (windowWidth * 56 / 100);
frameControllerSize = (int) (windowHeight * 5.7 / 100);
searchFrame = new JFrame();
Utils.centeredFrame(searchFrame, windowWidth, windowHeight, "Search Customer");
//loginFrame.setResizable(false);
Container frameContainer = searchFrame.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 is our class RoundedBackground:
public class RoundedBackground extends JComponent {
/**
*
*/
private static final long serialVersionUID = 1L;
int width, height,setX,setY;
public RoundedBackground (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.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.white);
//g2.setStroke(new BasicStroke(2.0f));
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);
}
}
Now Create a Search UI for already added Customer in customer table.
// 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("Search Customer");
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"));
// passenger table panel
// passenger table Base panel
Font textFont = new Font("Serif",Font.BOLD,12);
int bookedPanelWidth = panelWidth*96/100;
int bookedPanelHeight = panelHeight*86/100;
searchPanel = new JPanel();
searchPanel.setLayout(new FlowLayout());
searchPanel.setLayout(null);
searchPanel.setBounds(panelWidth*2/100,panelHeight*10/100, bookedPanelWidth, bookedPanelHeight);
searchPanel.setBorder(Utils.getTitledBorder(10, "Search"));
panel.add(searchPanel);
headingFont = new Font("Serif",Font.BOLD,15);
JLabel customer_search_L = new JLabel("Search Customer");
customer_search_L.setAlignmentX(JLabel.LEFT);
//searchLabel.setBackground(Color.decode("#80c2b2"));
customer_search_L.setForeground(Color.black);
customer_search_L.setFont(headingFont);
customer_search_L.setBounds(20,20, 200, 30);
searchPanel.add(customer_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,50, 20, 20);
searchPanel.add(background1);
JTextField customerSearch = new JTextField();
customerSearch.setBackground(Color.decode("#FFFFFF"));
customerSearch.setForeground(Color.black);
customerSearch.setBounds(40,50, 150, 20);
customerSearch.setOpaque(false);
customerSearch.setBorder(BorderFactory.createLineBorder(Color.white, 0));
customerSearch.setBackground(null);
searchPanel.add(customerSearch);
customerSearch.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
search(customerSearch.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
search(customerSearch.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
search(customerSearch.getText());
}
public void search(String str) {
if (str.length() == 0) {
sorter2.setRowFilter(null);
} else {
sorter2.setRowFilter(RowFilter.regexFilter(str));
}
}
});
searchPanel.add(Utils.getSeparator(20, 70, 170, 2,"000000"));
Here we Define Utils class separator method.
public static JSeparator getSeparator(int x, int y, int width, int height,String color) {
JSeparator jSeparator = new JSeparator();
jSeparator.setBackground(Color.decode("#"+color));
jSeparator.setBorder(null);
jSeparator.setOpaque(false);
jSeparator.setAlignmentX(JSeparator.CENTER_ALIGNMENT);
jSeparator.setBounds(x, y, width, height);
return jSeparator;
}
Now Create a Customer table UI and adding click funtionality.
// TODO Auto-generated method stub
String[] columnNames = {"Customer Id",
"Account No",
"Name",
"Contact No",
"Balance"
};
DefaultTableModel model1 = new DefaultTableModel();
sorter2 = new TableRowSorter<>(model1);
model1.setColumnIdentifiers(columnNames);
customerTable = new JTable(){
public boolean isCellEditable(int row, int column) {
return false;
};
};
customerTable.setBounds(width*2/100,height*17/100, width*96/100,height*80/100);
customerTable.setRowSorter(sorter2);
//table.setEnabled(true);
searchPanel.add(customerTable);
customerTable.setModel(model1);
JScrollPane scrollPane1 = new JScrollPane(customerTable);
scrollPane1.setBounds(width*2/100,height*17/100, width*96/100,height*80/100);
customerTable.setFillsViewportHeight(true);
searchPanel.add(scrollPane1);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
customerTable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(4).setCellRenderer( centerRenderer );
customerTable.getTableHeader().setFont(new Font("SansSerif", 1, 13));
customerTable.getTableHeader().setBackground(Color.decode("#4287f5"));
customerTable.getTableHeader().setForeground(Color.decode("#FFFFFF"));
customerTable.getTableHeader().setPreferredSize(new Dimension(760, 35));
customerTable.setRowHeight(25);
customerTable.getColumnModel().getColumn(0).setPreferredWidth(30);
customerTable.getColumnModel().getColumn(2).setPreferredWidth(100);
customerTable.getColumnModel().getColumn(4).setPreferredWidth(100);
customerTable.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(customerTable.getSelectedRow());
new Transaction(searchFrame, customerTable.getValueAt(customerTable.getSelectedRow(),1).toString(),
customerTable.getValueAt(customerTable.getSelectedRow(),2).toString());
}
});
table_update();
public class SQLiteJDBCDriverConnection {
/**
* Connect to a sample database
*/
public String rate=null;
public String date=null;
public static Connection connect1() {
try {
// SQLite connection string
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ex) {
Logger.getLogger(SQLiteJDBCDriverConnection.class.getName()).log(Level.SEVERE, null, ex);
}
// String url = "jdbc:sqlite:C:\\Users\\LWSSD\\eclipse-workspace\\HostelManagement\\hostelmanagement.db";
String url = "jdbc:sqlite:bankmanagement.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
}
Here's the logic to fetch data from database and show it in table.
private void table_update() {
conn = SQLiteJDBCDriverConnection.connect1();
try {
insert = conn.prepareStatement("SELECT customer_id, account_number, customer_name, contact_number FROM customer where status = 'Active'");
ResultSet rs = insert.executeQuery();
DefaultTableModel dft = (DefaultTableModel)customerTable.getModel();
dft.setRowCount(0);
while(rs.next()) {
int old_credit = 0;
int old_debit = 0;
int balance = 0;
insert = conn.prepareStatement("SELECT credit FROM statement where account_number IS ?");
insert.setString(1, rs.getString("account_number"));
ResultSet rs2 = insert.executeQuery();
while(rs2.next())
{
old_credit = old_credit + (rs2.getInt("credit"));
}
// System.out.println(rs2.getString("flight_name"));
//
insert = conn.prepareStatement("SELECT debit FROM statement where account_number IS ?");
insert.setString(1, rs.getString("account_number"));
ResultSet rs3 = insert.executeQuery();
while(rs3.next())
{
old_debit = old_debit + (rs3.getInt("debit"));
}
balance = old_credit - old_debit;
Vector v2 = new Vector();
v2.add(rs.getString("customer_id"));
v2.add(rs.getString("account_number"));
v2.add(rs.getString("customer_name"));
v2.add(rs.getString("contact_number"));
v2.add(String.valueOf(balance));
// v2.add(rs2.getString("flight_name"));
// v2.add(rs2.getString("route"));
// v2.add(rs2.getString("departure_timing"));
// v2.add(rs2.getString("arrival_timing"));
// v2.add(rs3.getString("passenger_name"));
// v2.add(rs3.getString("contact_number"));
// v2.add(rs.getString("booking_date"));
dft.addRow(v2);
}
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public class Transaction implements TableModelListener {
private Connection conn;
private PreparedStatement insert;
private JTable customerTable;
private int posX;
private int posY;
private JPanel panel, bookedPanel, actionPanel;
private TableRowSorter sorter2;
public static int totalWinWidth,totalWinHeight,winWidth,winHeight;
private JButton addFlightBtn, editBtn, deleteBtn, updateBtn, cancleBtn;
public JFrame bookedFrame;
int windowWidth;
int windowHeight;
public static Dimension screenSize;
int frameControllerSize;
JFrame dashboardFrame;
private String acc_no, cust_name;
public Transaction(JFrame jframe, String acc_no, String cust_name) {
this.dashboardFrame = jframe;
this.acc_no = acc_no;
this.cust_name = cust_name;
dashboardFrame.setVisible(false);
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
windowWidth = (int) (screenSize.getWidth() *90 /100);
windowHeight = (int) (windowWidth * 56 / 100);
frameControllerSize = (int) (windowHeight * 5.7 / 100);
initGUI();
}
private void initGUI(){
bookedFrame = new JFrame();
Utils.centeredFrame(bookedFrame, windowWidth, windowHeight, "Transaction");
//loginFrame.setResizable(false);
Container frameContainer = bookedFrame.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);
// Your code start from here
// 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("Transaction");
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"));
// passenger table panel
// passenger table Base panel
Font textFont = new Font("Serif",Font.BOLD,12);
int bookedPanelWidth = panelWidth*96/100;
int bookedPanelHeight = panelHeight*86/100;
bookedPanel = new JPanel();
bookedPanel.setLayout(new FlowLayout());
bookedPanel.setLayout(null);
bookedPanel.setBounds(panelWidth*2/100,panelHeight*10/100, bookedPanelWidth, bookedPanelHeight);
bookedPanel.setBorder(Utils.getTitledBorder(10, "Search"));
panel.add(bookedPanel);
headingFont = new Font("Serif",Font.PLAIN,15);
JLabel acc_no_label = new JLabel("Account No : " + acc_no);
acc_no_label.setAlignmentX(JLabel.LEFT);
//searchLabel.setBackground(Color.decode("#80c2b2"));
acc_no_label.setForeground(Color.black);
acc_no_label.setFont(headingFont);
acc_no_label.setBounds(20,20, panelWidth/2, 30);
bookedPanel.add(acc_no_label);
headingFont = new Font("Serif",Font.PLAIN,15);
JLabel cust_name_label = new JLabel("Customer Name : " + cust_name );
cust_name_label.setAlignmentX(JLabel.LEFT);
//searchLabel.setBackground(Color.decode("#80c2b2"));
cust_name_label.setForeground(Color.black);
cust_name_label.setFont(headingFont);
cust_name_label.setBounds(20,50, panelWidth/2, 30);
bookedPanel.add(cust_name_label);
customer_table(bookedPanelWidth, bookedPanelHeight);
bookedFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
bookedFrame.dispose();
dashboardFrame.setVisible(true);
}
});
bookedFrame.setVisible(true);
}
/*
* public static void main(String[] args) { // TODO Auto-generated method stub
* screenSize = Toolkit.getDefaultToolkit().getScreenSize(); totalWinWidth =
* screenSize.width*94/100; totalWinHeight = screenSize.height; BookedFlight
* awtsample = new BookedFlight(); awtsample.bookedFrame.setVisible(true);
* //DashboardNew window = new DashboardNew();
* //window.dashboardFrame.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+data+"Wrong");
}
private void table_update() {
conn = SQLiteJDBCDriverConnection.connect1();
try {
insert = conn.prepareStatement("SELECT transaction_id, credit, debit, balance, credit_date, debit_date FROM statement where account_number IS ?");
insert.setString(1, acc_no);
ResultSet rs = insert.executeQuery();
DefaultTableModel dft = (DefaultTableModel)customerTable.getModel();
dft.setRowCount(0);
while(rs.next()) {
Vector v2 = new Vector();
v2.add(rs.getString("transaction_id"));
v2.add(rs.getString("credit"));
v2.add(rs.getString("debit"));
v2.add(rs.getString("balance"));
v2.add(rs.getString("credit_date"));
v2.add(rs.getString("debit_date"));
dft.addRow(v2);
}
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private void customer_table(int width, int height) {
// TODO Auto-generated method stub
String[] columnNames = {"Transaction Id",
"Credit",
"Debit",
"Balance",
"Credit Date",
"Debit Date"
};
DefaultTableModel model1 = new DefaultTableModel();
sorter2 = new TableRowSorter<>(model1);
model1.setColumnIdentifiers(columnNames);
customerTable = new JTable(){
public boolean isCellEditable(int row, int column) {
return false;
};
};
customerTable.setBounds(width*2/100,height*17/100, width*96/100,height*80/100);
customerTable.setRowSorter(sorter2);
//table.setEnabled(true);
bookedPanel.add(customerTable);
customerTable.setModel(model1);
JScrollPane scrollPane1 = new JScrollPane(customerTable);
scrollPane1.setBounds(width*2/100,height*17/100, width*96/100,height*80/100);
customerTable.setFillsViewportHeight(true);
bookedPanel.add(scrollPane1);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
customerTable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(4).setCellRenderer( centerRenderer );
customerTable.getColumnModel().getColumn(5).setCellRenderer( centerRenderer );
customerTable.getTableHeader().setFont(new Font("SansSerif", 1, 13));
customerTable.getTableHeader().setBackground(Color.decode("#4287f5"));
customerTable.getTableHeader().setForeground(Color.decode("#FFFFFF"));
customerTable.getTableHeader().setPreferredSize(new Dimension(760, 35));
customerTable.setRowHeight(25);
customerTable.getColumnModel().getColumn(0).setPreferredWidth(30);
customerTable.getColumnModel().getColumn(5).setPreferredWidth(100);
customerTable.getColumnModel().getColumn(4).setPreferredWidth(100);
customerTable.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(customerTable.getSelectedRow());
}
});
table_update();
}
class MyCanvas extends Canvas {
public MyCanvas () {
}
public void paint (Graphics g) {
g.setColor(Color.black);
//g.drawRect(500, 200, 200, 200);
g.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getWidth()/4,getHeight());
//g2.drawString ("It is a custom canvas area", 70, 70);
}
}
}
At last we will manage closing of window
searchFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
searchFrame.dispose();
dashboardFrame.setVisible(true);
}
});
searchFrame.setVisible(true);
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.