Add/Edit Flat

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 Flat");
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 detailsflat = Util.createPanel(winWidth*91/100,winHeight*60/100,new Point(winWidth/50,winHeight*27/100), true,"Flat Details");
panel.add(detailsflat,BorderLayout.WEST);
detailsflat.setLayout(null);
heading=new JLabel();
heading.setText("Add/Edit Flat");
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, "Owner 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);
TitledBorder flat_status_border = BorderFactory.createTitledBorder(loweredetched, "Flat Status");
flat_no_border.setTitleJustification(TitledBorder.LEFT);
Here UI of action layout ffor add flat:
int space=winWidth/15;
int posY=winHeight/22;
name=new JLabel();
name.setText("Enter Owner Name:");
name.setForeground(Color.black);
name.setFont(new Font("Lucida",Font.PLAIN,12));
name.setSize(new Dimension(winWidth/7,winHeight/16));
name.setLocation(space/2, 2);
action.add(name);
myTextField = new JTextField();
myTextField.setSize(new Dimension(winWidth/7,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/7,winHeight/16));
phone.setLocation(winWidth/7+space, 2);
action.add(phone);
phonetextField = new JTextField();
phonetextField.setSize(new Dimension(winWidth/7,winHeight/15));
phonetextField.setLocation(winWidth/7+space,posY);
phonetextField.setBorder(phoneTittle);
action.add(phonetextField,BorderLayout.SOUTH);
flat_no=new JLabel();
flat_no.setText("Enter Flat No:");
flat_no.setForeground(Color.black);
flat_no.setFont(new Font("Lucida",Font.PLAIN,12));
flat_no.setSize(new Dimension(winWidth/7,winHeight/16));
flat_no.setLocation((winWidth/7)*2+space*3/2, 2);
action.add(flat_no);
flat_no_textField = new JTextField();
flat_no_textField.setSize(new Dimension(winWidth/7,winHeight/15));
flat_no_textField.setLocation((winWidth/7)*2+space*3/2,posY);
flat_no_textField.setBorder(flat_no_border);
action.add(flat_no_textField,BorderLayout.SOUTH);
JLabel status=new JLabel();
status.setText("Select Flat Status:");
status.setForeground(Color.black);
status.setFont(new Font("Lucida",Font.PLAIN,12));
status.setSize(new Dimension(winWidth/7,winHeight/16));
status.setLocation((winWidth/7)*3+space*2, 2);
action.add(status);
flat_status_choice_field = new JComboBox(flat_status_choice);
flat_status_choice_field.setSize(new Dimension(winWidth/7,winHeight/15));
flat_status_choice_field.setLocation((winWidth/7)*3+space*2,posY);
flat_status_choice_field.setBorder(flat_status_border);
flat_status_choice_field.setBackground(Color.white);
flat_status_choice_field.setSelectedItem(flat_status_choice[1]);
flat_status_choice_field.setEditable(true);
action.add(flat_status_choice_field,BorderLayout.SOUTH);
JButton addFlatButton = new JButton(new ImageIcon
(((new ImageIcon("button3.png")).getImage()).getScaledInstance(150,35, java.awt.Image.SCALE_SMOOTH)));
addFlatButton.setContentAreaFilled(false);
addFlatButton.setBorder(null);
addFlatButton.setText("Add Flat");
addFlatButton.setForeground(Color.black);
addFlatButton.setFont(new Font("Lucida",Font.PLAIN,16));
addFlatButton.setHorizontalTextPosition(JButton.CENTER);
addFlatButton.setVerticalTextPosition(JButton.CENTER);
addFlatButton.setBounds((winWidth/5)*3+space*5/2-10,posY+4,150,35);
addFlatButton.setVisible(true);
addFlatButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
action.add(addFlatButton);
Here is logic for add flat whwn click "Add Flat" Button.
addFlatButton.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_no_textField.getText().isEmpty()){
var=var+"Flat No should not be Empty ";
}else{
flat_no_String=flat_no_textField.getText().toString();
}
if(var==""){
String status= flat_status_choice_field.getSelectedItem().toString();
boolean isSussessFullEntered=tableIntryField(nameString,contactString,flat_no_String,status);
if(isSussessFullEntered){
myTextField.setText("");
phonetextField.setText("");
flat_no_textField.setText("");
displaylistToTable(table,getTableListData());
JOptionPane.showMessageDialog(frame, "Successfull Enterd");
}else{
JOptionPane.showMessageDialog(frame, "Not Enterd");
}
}else {
JOptionPane.showMessageDialog(frame, var);
}
}
});
Here is add Search UI In Flat Detais Layout :
search=new JLabel();
search.setText("Search Flat");
search.setForeground(Color.black);
flat_no.setFont(new Font("Lucida",Font.PLAIN,12));
search.setSize(new Dimension(winWidth/4,winHeight/16));
search.setLocation(30, 4);
detailsflat.add(search);
searchTextField = new JTextField();
searchTextField.setSize(new Dimension(winWidth/4,winHeight/16));
searchTextField.setLocation(30,winHeight/20);
searchTextField.setBorder(searchTittle);
detailsflat.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));
}
}
});
cancleButton = new JButton(new ImageIcon(((new ImageIcon("button3.png")).
getImage()).getScaledInstance(150,35, java.awt.Image.SCALE_SMOOTH)));
cancleButton.setContentAreaFilled(false);
cancleButton.setBorder(null);
cancleButton.setText("Cancel");
cancleButton.setForeground(Color.black);
cancleButton.setFont(new Font("Lucida",Font.PLAIN,16));
cancleButton.setHorizontalTextPosition(JButton.CENTER);
cancleButton.setVerticalTextPosition(JButton.CENTER);
cancleButton.setBounds(winWidth*44/100,40,150,35);
cancleButton.setVisible(false);
cancleButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
detailsflat.add(cancleButton);
cancleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addFlatButton.setVisible(true);
editButton.setVisible(false);
deleteButton.setVisible(false);
cancleButton.setVisible(false);
updateButton.setVisible(false);
myTextField.setText("");
phonetextField.setText("");
flat_no_textField.setText("");
}
});
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Here is logic for visible "EDIT" button in flat details layout when select any flat.
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.black);
editButton.setFont(new Font("Lucida",Font.PLAIN,16));
editButton.setHorizontalTextPosition(JButton.CENTER);
editButton.setVerticalTextPosition(JButton.CENTER);
editButton.setBounds(winWidth*60/100,40,150,35);
detailsflat.add(editButton);
editButton.setVisible(false);
editButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addFlatButton.setVisible(false);
cancleButton.setVisible(true);
updateButton.setVisible(true);
myTextField.setText(table.getValueAt(table.getSelectedRow(),2).toString());
phonetextField.setText(table.getValueAt(table.getSelectedRow(), 3).toString());
flat_no_textField.setText(table.getValueAt(table.getSelectedRow(), 1).toString());
flat_status_choice_field.setSelectedItem(table.getValueAt(table.getSelectedRow(), 4).toString());
}
});
Here's logic for visable "UPDATE" button in action layout when click "EDIT" Button.
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.black);
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_no_textField.getText().isEmpty()){
var=var+"Flat No should not be Empty ";
}else{
flat_no_String=flat_no_textField.getText().toString();
}
if(var==""){
addFlatButton.setVisible(true);
String status=flat_status_choice_field.getSelectedItem().toString();
String generatedID=table.getValueAt(table.getSelectedRow(), 0).toString();
boolean isSussessFullEntered=updatetableIntryField(generatedID,nameString,contactString,flat_no_String,status);
if(isSussessFullEntered){
displaylistToTable(table,getTableListData());
myTextField.setText("");
phonetextField.setText("");
flat_no_textField.setText("");
editButton.setVisible(false);
deleteButton.setVisible(false);
cancleButton.setVisible(false);
JOptionPane.showMessageDialog(frame, "Successfull Updated");
}else{
JOptionPane.showMessageDialog(frame, "Not Update");
}
}else{
JOptionPane.showMessageDialog(frame, var);
}
}
});
here is logic for visible "Delete" Button in flat details layout when select any flat from table.
deleteButton=new JButton(new ImageIcon(((new ImageIcon("button3.png")).getImage()).getScaledInstance(150, 35, java.awt.Image.SCALE_SMOOTH)));
deleteButton.setContentAreaFilled(false);
deleteButton.setBorder(null);
deleteButton.setText("Delete");
deleteButton.setForeground(Color.black);
deleteButton.setFont(new Font("Lucida",Font.PLAIN,16));
deleteButton.setHorizontalTextPosition(JButton.CENTER);
deleteButton.setVerticalTextPosition(JButton.CENTER);
deleteButton.setBounds(winWidth*60/100+200,40,150,35);
detailsflat.add(deleteButton);
deleteButton.setVisible(false);
deleteButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isSuccessdelete=deleteDataFromTable(table.getValueAt(table.getSelectedRow(), 0).toString());
if(isSuccessdelete){
JOptionPane.showMessageDialog(null, "Flat Deleted");
displaylistToTable(table,getTableListData());
table.clearSelection();
editButton.setVisible(false);
deleteButton.setVisible(false);
}else{
JOptionPane.showMessageDialog(null, "Could not delete flat");
}
}
});
here is UI for flat details in table form in flat details layout.
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));
detailsflat.add(pane);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
if(null!=editButton && null!=deleteButton){
editButton.setVisible(true);
deleteButton.setVisible(true);
cancleButton.setVisible(true);
}
}
});
here is logic for show in table in flat details layout.
public void displaylistToTable(JTable table,ArrayList list){
defaultdataModel=new DefaultTableModel(){
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
Object []row=new Object[5];
defaultdataModel.setColumnIdentifiers(new Object[]{"Flat ID","Flat No","Owner Name","Contact No","Flat Status"});
for (int i = 0; i < list.size(); i++) {
row[0]=list.get(i).getId();
row[1]=list.get(i).getFlat_no();
row[2]=list.get(i).getFlat_owner_name();
row[3]=list.get(i).getContact_no();
row[4]=list.get(i).getFlat_status();
defaultdataModel.addRow(row);
}
int tableWidth=(winWidth*86/100)/5;
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 );
int[] columnsWidth = {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 boolean tableIntryField(String nameString,String contactString,String flatNoString, String status) {
Connection conn=MY_DB.ConnectDb();
boolean issuccess=false;
Statement stmt = null;
int action;
String sql = "INSERT INTO flat_detail (flat_no, flat_owner_name, contact_no, flat_status)" +"VALUES (?,?,?,?);";
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = conn.prepareStatement(sql);
pst.setString(1,flatNoString );
pst.setString(2, nameString );
pst.setString(3, contactString);
pst.setString(4, status);
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 update data from database when click "UPDATE" Button.
protected boolean updatetableIntryField(String id, String nameString,
String contactString, String flatNo, String status) {
Connection conn=MY_DB.ConnectDb();
boolean issuccess=false;
Statement stmt = null;
int action;
PreparedStatement pst = null;
try {
pst = conn.prepareStatement("UPDATE flat_detail SET flat_no = ?, flat_owner_name = ?, contact_no = ?, flat_status = ? WHERE id = ?");
pst.setString(1,flatNo );
pst.setString(2, nameString );
pst.setString(3, contactString );
pst.setString(4, status);
pst.setString(5, id);
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 delete data from database when click "DELETE" Button.
protected boolean deleteDataFromTable(String id) {
boolean issuccess=false;
Connection conn=MY_DB.ConnectDb();
String sql = "delete from flat_detail WHERE id = ?";
try {
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString( 1, 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;
}
Here is logic get Data in table from database.
public ArrayList getTableListData(){
ArrayList list=new ArrayList();
ResultSet rs;
String query = "SELECT * FROM flat_detail;" ;
Connection conn = MY_DB.ConnectDb();
Statement stat = null;
try {
stat = conn.createStatement();
rs = stat.executeQuery(query);
FlatData visitorData;
while(rs.next()){
visitorData=new FlatData(rs.getString("id"),
rs.getString("flat_no"),
rs.getString("flat_owner_name"),
rs.getString("contact_no"),
rs.getString("flat_status"));
list.add(visitorData);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return list;
}
public ArrayList getselectedTableListData(String data){
ArrayList flatList=new ArrayList();
ResultSet rs;
String query = "SELECT * FROM flat_detail WHERE flat_owner_name LIKE '%" + data + "%' or flat_no LIKE '%" + data + "%' " ;
Connection conn = MY_DB.ConnectDb();
Statement stat = null;
try {
stat = conn.createStatement();
rs = stat.executeQuery(query);
FlatData flatdata;
while(rs.next()){
flatdata=new FlatData(rs.getString("id"), rs.getString("flat_no"),
rs.getString("flat_owner_name"), rs.getString("contact_no"),
rs.getString("flat_status"));
flatList.add(flatdata);
}
rs.close();
stat.close();
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return flatList;
}
At last here is FlatData Class.
public class FlatData {
private String id;
private String flat_no;
private String flat_owner_name;
private String contact_no;
private String flat_status;
public FlatData(String id, String flat_no, String flat_owner_name, String contact_no, String flat_status) {
super();
this.setId(id);
this.setFlat_no(flat_no);
this.setFlat_owner_name(flat_owner_name);
this.setContact_no(contact_no);
this.setFlat_status(flat_status);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFlat_no() {
return flat_no;
}
public void setFlat_no(String flat_no) {
this.flat_no = flat_no;
}
public String getFlat_owner_name() {
return flat_owner_name;
}
public void setFlat_owner_name(String flat_owner_name) {
this.flat_owner_name = flat_owner_name;
}
public String getContact_no() {
return contact_no;
}
public void setContact_no(String contact_no) {
this.contact_no = contact_no;
}
public String getFlat_status() {
return flat_status;
}
public void setFlat_status(String flat_status) {
this.flat_status = flat_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.