// The small previous project of phonebook system but by using Map collection as storage in which a user can add, update, delete and search records. Here, the records are not stored permanently. You will have to add new records afresh each time you run the program.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
class MapDetails{
private String contact_no ;
private String email_address, address;
public MapDetails(){}
public MapDetails(String contact,String email,String address){
putContact(contact);
putEmail(email);
putAddress(address);
}
public void putContact(String contact){
contact_no = contact;
}
public String getContact(){
return contact_no;
}
public void putEmail(String email){
email_address = email;
}
public String getEmail(){
return email_address;
}
public void putAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
}
class MapTable{
class MyModel extends DefaultTableModel{
public MyModel(Object[] columnNames , int rowcount){
super(columnNames,rowcount);
}
public boolean isCellEditable(int row , int column){
if(column == 1)
return false;
else return true;
}
public void setValueAt(Object aValue,int row, int column){
int temp = 0;
if(column == 2){
for(int i = 0 ; i < aValue.toString().length() ; i++){
char c = aValue.toString().charAt(i);
if(i == 0){
if( !Character.isDigit(c) && ! (c == '+')){
temp++;
}
}
else{
if(!(Character.isDigit(c)) && !(c == ',') && !(c == '+')){
temp++;
}
}
}
if(temp == 0){
super.setValueAt(aValue,row,column);
}
}
else if(column == 3){
int atpos = aValue.toString().indexOf("@");
int dotpos = aValue.toString().indexOf(".");
boolean at = !(atpos > 0);
boolean dot = !(dotpos > (atpos+1));
boolean check = (dotpos == (aValue.toString().length()-1));
if( aValue.toString().trim().equals("") || at || dot || check){
temp++;
}
if(temp == 0){
super.setValueAt(aValue,row,column);
}
}
else{
super.setValueAt(aValue,row,column);
}
}
}
JTable table;
MyModel dm;
String[] column_names = {"S.No.","Name","Contact No","Email Address","Address"};
public MapTable(){
dm = new MyModel(column_names,0);
table = new JTable(dm);
table.setAutoCreateRowSorter(true);
}
public JTable getTable(){
return table;
}
public DefaultTableModel getDm(){
return dm;
}
public void tableData(JPanel pnl){
JScrollPane scroll = new JScrollPane(table);
pnl.add(scroll,BorderLayout.CENTER);
}
public void addRecordToTable(String name , MapDetails detail){
int row = dm.getRowCount();
String[] rowdata = {""+ (row+1) , name , detail.getContact() , detail.getEmail() , detail.getAddress() };
dm.insertRow(row, rowdata);
}
}
class MapPhonebook extends JFrame implements ActionListener{
private JTabbedPane tabbedpane;
private JPanel pnladdrecord,pnlsearch;
private JLabel lblname,lblcontact,lblemail,lbladdress,lblverification,lblstatus;
private JTextField txtname,txtcontact,txtemail,txtaddress,txtnamesearch;
private JButton btnadd,btnsearch,btndelete,btnSave;
MapDetails record;
String name;
private MapTable tblrecord,tblsearchrecord;
Map<String,MapDetails> detail = new TreeMap<>();
private Set<Map.Entry<String,MapDetails>> entries = detail.entrySet();
public MapPhonebook(String title){
super(title);
setLayout(new GridLayout(1,1));
setBounds(300,200,700,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
initialize();
// pack();
}
public void initialize(){
tabbedpane = new JTabbedPane();
pnladdrecord = new JPanel();
AddPanel();
tabbedpane.addTab("Add Record",pnladdrecord);
pnlsearch = new JPanel();
searchPanel();
tabbedpane.addTab("Search Record",pnlsearch);
add(tabbedpane);
}
public void AddPanel(){
pnladdrecord.setLayout(new BorderLayout());
JPanel pnladd = new JPanel();
pnladd.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//First row
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
c.gridx = 0;
lblname = new JLabel("Name :");
pnladd.add(lblname,c);
c.gridx++;
txtname = new JTextField("",10);
txtname.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent ke){
char c = ke.getKeyChar();
if(!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c == ' '))
ke.consume();
}
});
pnladd.add(txtname,c);
//Second row
c.gridy++;
c.gridx = 0;
lblcontact = new JLabel("Contact No:");
pnladd.add(lblcontact ,c);
c.gridx++;
c.gridwidth = 2;
txtcontact = new JTextField("",10);
txtcontact.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent ke){
char c = ke.getKeyChar();
if(txtcontact.getCaretPosition() == 0){
if((txtcontact.getText().indexOf('+') == -1)){
if(!(Character.isDigit(c)) && !(c == ',') && !(c == '+') )
ke.consume();
}
else{
ke.consume();
}
}
else{
int comma = txtcontact.getText().indexOf(',');
if((txtcontact.getCaretPosition() == (comma+1))){
if(!(Character.isDigit(c)) && !(c == ',') && !(c == '+') )
ke.consume();
}
else{
if(!(Character.isDigit(c)) && !(c == ','))
ke.consume();
}
}
}
});
pnladd.add(txtcontact,c);
//Third row
c.gridy++;
c.gridwidth = 1;
c.gridx = 0;
lblemail = new JLabel("Email Address :");
pnladd.add(lblemail ,c);
c.gridx++;
c.gridwidth = 2;
txtemail = new JTextField("",10);
txtemail.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent ke){
char c = ke.getKeyChar();
int at = txtemail.getText().indexOf('@');
int dot = txtemail.getText().indexOf('.');
if(txtemail.getCaretPosition() == 0){
if((c == '@') || (c == '.'))
ke.consume();
}
else{
if(at == -1){
if((c == '.'))
ke.consume();
}
else if(at != -1 && dot == -1 && txtemail.getCaretPosition() > (at+1)){
if((c == '@'))
ke.consume();
}
else if(at != -1 && dot == -1 && txtemail.getCaretPosition() == (at+1)){
if((c == '@')|| (c == '.'))
ke.consume();
}
else if(at != -1 && dot != -1){
if((c == '@') || (c == '.'))
ke.consume();
}
}
}
});
pnladd.add(txtemail,c);
//Fourth row
c.gridy++;
c.gridwidth = 1;
c.gridx = 0;
lbladdress = new JLabel("Address :");
pnladd.add(lbladdress ,c);
c.gridx++;
c.gridwidth = 2;
txtaddress = new JTextField("",10);
pnladd.add(txtaddress,c);
//Fifth row
c.gridy++;
c.gridwidth = 3;
c.gridx = 0;
lblstatus = new JLabel(" ");
lblstatus.setForeground(Color.RED);
pnladd.add(lblstatus ,c);
//Last row
c.gridy++;
btnadd = new JButton("Add record");
btnadd.addActionListener(this);
pnladd.add(btnadd , c);
pnladdrecord.add(pnladd,BorderLayout.WEST);
tblrecord = new MapTable();
tblrecord.tableData(pnladdrecord);
}
public void searchPanel(){
pnlsearch.setLayout(new BorderLayout());
JPanel pnl = new JPanel();
pnl.setLayout(new GridLayout(1,3));
lblname = new JLabel("Name :");
pnl.add(lblname);
txtnamesearch = new JTextField("",10);
pnl.add(txtnamesearch);
btnsearch = new JButton("Search");
btnsearch.addActionListener(this);
pnl.add(btnsearch);
pnlsearch.add(pnl,BorderLayout.NORTH);
tblsearchrecord = new MapTable();
tblsearchrecord.tableData(pnlsearch);
JPanel pnlDelete = new JPanel();
pnlDelete.setLayout(new BorderLayout());
btndelete = new JButton("Delete");
btndelete.addActionListener(this);
btnSave = new JButton("Save");
btnSave.addActionListener(this);
pnlDelete.add(btnSave,BorderLayout.WEST);
pnlDelete.add(btndelete,BorderLayout.EAST);
pnlsearch.add(pnlDelete,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent a){
String command = a.getActionCommand();
switch(command){
case "Add record":
MapDetails temp = new MapDetails();
boolean nameverify = true;
Collection<String> keys = detail.keySet();
Object[] key = keys.toArray();
for(int i = 0; i < key.length ; i++){
String temp1 = key[i].toString();
if((temp1.equalsIgnoreCase(txtname.getText().trim()))){
nameverify = false;
}
}
boolean namecheck = !(txtname.getText().trim().equals("")) && nameverify ;
boolean emailcheck = (!(txtemail.getText().indexOf('@') == -1) && !(txtemail.getText().indexOf('.') == -1)) || (txtemail.getText().equals(""));
boolean contactcheck = !(txtcontact.getText().trim().equals(""));
boolean addresscheck = !(txtaddress.getText().trim().equals(""));
if( namecheck && contactcheck && emailcheck && addresscheck ){
//Adding MapDetails
String tempName = txtname.getText().trim();
String tempContact = txtcontact.getText().trim();
String tempEmail = txtemail.getText().trim();
String tempAddress = txtaddress.getText().trim();
temp = new MapDetails(tempContact , tempEmail , tempAddress);
detail.put(tempName,temp);
//Adding the record to the table
tblrecord.addRecordToTable(tempName,temp);
lblstatus.setText(" Successful");
}
else{
// lblverification.setText(" Invalid Name");
String error;
error = "Unsuccessful!! ";
lblstatus.setText(error);
if(!namecheck){
lblstatus.setText(error + " Invalid Name");
}
else if(!contactcheck)
lblstatus.setText(error + " Enter Contact ");
else if(!addresscheck)
lblstatus.setText(error + " Enter Address ");
else if(!emailcheck)
lblstatus.setText(error + " Invalid email ");
}
break;
case "Search":
search();
break;
case "Delete":
int confirm = JOptionPane.showConfirmDialog(new JFrame(),"Are you sure you want to delete these records?",
"Confim Message",JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
int[] row = tblsearchrecord.getTable().getSelectedRows();
Object[] name = new String[row.length];
for(int i = 0 ; i < row.length ; i++){
int index = row[i];
name[i] = tblsearchrecord.getDm().getValueAt(index,1);
}
for(int i = 0 ; i < name.length ; i++){
MapDetails temp1;
Object name1 = name[i];
if(detail.containsKey(name1)){
for(int j = 0 ; j < tblrecord.getTable().getRowCount() ; j++){
if(tblrecord.getDm().getValueAt(j,1).toString().equals(name1))
tblrecord.getDm().removeRow(j);
}
detail.remove(name1);
search();
}
}
tblrecord.getDm().setRowCount(0);
for(Map.Entry<String,MapDetails> entry : entries){
tblrecord.addRecordToTable(entry.getKey(),entry.getValue());
}
}
break;
case "Save":
for(int i = 0 ; i < tblsearchrecord.getDm().getRowCount(); i++){
String name = tblsearchrecord.getDm().getValueAt(i,1).toString();
if(detail.containsKey(name)){
MapDetails temp1 = new MapDetails();
detail.remove(name);
temp1.putContact(tblsearchrecord.getDm().getValueAt(i,2).toString());
temp1.putEmail(tblsearchrecord.getDm().getValueAt(i,3).toString());
temp1.putAddress(tblsearchrecord.getDm().getValueAt(i,4).toString());
detail.put(name,temp1);
}
}
tblrecord.getDm().setRowCount(0);
for(Map.Entry<String,MapDetails> entry : entries){
tblrecord.addRecordToTable(entry.getKey(),entry.getValue());
}
search();
break;
}
}
public void search(){
tblsearchrecord.getDm().setRowCount(0);
String namesearch = txtnamesearch.getText();
MapDetails temp;
Collection<String> keys = detail.keySet();
Object[] key = keys.toArray();
for(int i = 0; i < key.length ; i++){
String temp1 = key[i].toString();
if(temp1.toLowerCase().contains(namesearch.toLowerCase())){
temp = detail.get(temp1);
int row = tblsearchrecord.getDm().getRowCount();
String[] rowdata = {""+ (row+1) , temp1, temp.getContact() , temp.getEmail() , temp.getAddress() };
tblsearchrecord.getDm().addRow(rowdata);
}
}
}
public static void main (String[] args) {
new MapPhonebook("Phonebook");
}
}
The default tab of the program
Adding Records in PhoneBook
Searching and Editing the existing record
Need to click on SAVE to save the updated record
Deleting some record from the PhoneBook
Validation applied in different Textfields(as shown for Email)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
class MapDetails{
private String contact_no ;
private String email_address, address;
public MapDetails(){}
public MapDetails(String contact,String email,String address){
putContact(contact);
putEmail(email);
putAddress(address);
}
public void putContact(String contact){
contact_no = contact;
}
public String getContact(){
return contact_no;
}
public void putEmail(String email){
email_address = email;
}
public String getEmail(){
return email_address;
}
public void putAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
}
class MapTable{
class MyModel extends DefaultTableModel{
public MyModel(Object[] columnNames , int rowcount){
super(columnNames,rowcount);
}
public boolean isCellEditable(int row , int column){
if(column == 1)
return false;
else return true;
}
public void setValueAt(Object aValue,int row, int column){
int temp = 0;
if(column == 2){
for(int i = 0 ; i < aValue.toString().length() ; i++){
char c = aValue.toString().charAt(i);
if(i == 0){
if( !Character.isDigit(c) && ! (c == '+')){
temp++;
}
}
else{
if(!(Character.isDigit(c)) && !(c == ',') && !(c == '+')){
temp++;
}
}
}
if(temp == 0){
super.setValueAt(aValue,row,column);
}
}
else if(column == 3){
int atpos = aValue.toString().indexOf("@");
int dotpos = aValue.toString().indexOf(".");
boolean at = !(atpos > 0);
boolean dot = !(dotpos > (atpos+1));
boolean check = (dotpos == (aValue.toString().length()-1));
if( aValue.toString().trim().equals("") || at || dot || check){
temp++;
}
if(temp == 0){
super.setValueAt(aValue,row,column);
}
}
else{
super.setValueAt(aValue,row,column);
}
}
}
JTable table;
MyModel dm;
String[] column_names = {"S.No.","Name","Contact No","Email Address","Address"};
public MapTable(){
dm = new MyModel(column_names,0);
table = new JTable(dm);
table.setAutoCreateRowSorter(true);
}
public JTable getTable(){
return table;
}
public DefaultTableModel getDm(){
return dm;
}
public void tableData(JPanel pnl){
JScrollPane scroll = new JScrollPane(table);
pnl.add(scroll,BorderLayout.CENTER);
}
public void addRecordToTable(String name , MapDetails detail){
int row = dm.getRowCount();
String[] rowdata = {""+ (row+1) , name , detail.getContact() , detail.getEmail() , detail.getAddress() };
dm.insertRow(row, rowdata);
}
}
class MapPhonebook extends JFrame implements ActionListener{
private JTabbedPane tabbedpane;
private JPanel pnladdrecord,pnlsearch;
private JLabel lblname,lblcontact,lblemail,lbladdress,lblverification,lblstatus;
private JTextField txtname,txtcontact,txtemail,txtaddress,txtnamesearch;
private JButton btnadd,btnsearch,btndelete,btnSave;
MapDetails record;
String name;
private MapTable tblrecord,tblsearchrecord;
Map<String,MapDetails> detail = new TreeMap<>();
private Set<Map.Entry<String,MapDetails>> entries = detail.entrySet();
public MapPhonebook(String title){
super(title);
setLayout(new GridLayout(1,1));
setBounds(300,200,700,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
initialize();
// pack();
}
public void initialize(){
tabbedpane = new JTabbedPane();
pnladdrecord = new JPanel();
AddPanel();
tabbedpane.addTab("Add Record",pnladdrecord);
pnlsearch = new JPanel();
searchPanel();
tabbedpane.addTab("Search Record",pnlsearch);
add(tabbedpane);
}
public void AddPanel(){
pnladdrecord.setLayout(new BorderLayout());
JPanel pnladd = new JPanel();
pnladd.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//First row
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
c.gridx = 0;
lblname = new JLabel("Name :");
pnladd.add(lblname,c);
c.gridx++;
txtname = new JTextField("",10);
txtname.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent ke){
char c = ke.getKeyChar();
if(!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c == ' '))
ke.consume();
}
});
pnladd.add(txtname,c);
//Second row
c.gridy++;
c.gridx = 0;
lblcontact = new JLabel("Contact No:");
pnladd.add(lblcontact ,c);
c.gridx++;
c.gridwidth = 2;
txtcontact = new JTextField("",10);
txtcontact.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent ke){
char c = ke.getKeyChar();
if(txtcontact.getCaretPosition() == 0){
if((txtcontact.getText().indexOf('+') == -1)){
if(!(Character.isDigit(c)) && !(c == ',') && !(c == '+') )
ke.consume();
}
else{
ke.consume();
}
}
else{
int comma = txtcontact.getText().indexOf(',');
if((txtcontact.getCaretPosition() == (comma+1))){
if(!(Character.isDigit(c)) && !(c == ',') && !(c == '+') )
ke.consume();
}
else{
if(!(Character.isDigit(c)) && !(c == ','))
ke.consume();
}
}
}
});
pnladd.add(txtcontact,c);
//Third row
c.gridy++;
c.gridwidth = 1;
c.gridx = 0;
lblemail = new JLabel("Email Address :");
pnladd.add(lblemail ,c);
c.gridx++;
c.gridwidth = 2;
txtemail = new JTextField("",10);
txtemail.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent ke){
char c = ke.getKeyChar();
int at = txtemail.getText().indexOf('@');
int dot = txtemail.getText().indexOf('.');
if(txtemail.getCaretPosition() == 0){
if((c == '@') || (c == '.'))
ke.consume();
}
else{
if(at == -1){
if((c == '.'))
ke.consume();
}
else if(at != -1 && dot == -1 && txtemail.getCaretPosition() > (at+1)){
if((c == '@'))
ke.consume();
}
else if(at != -1 && dot == -1 && txtemail.getCaretPosition() == (at+1)){
if((c == '@')|| (c == '.'))
ke.consume();
}
else if(at != -1 && dot != -1){
if((c == '@') || (c == '.'))
ke.consume();
}
}
}
});
pnladd.add(txtemail,c);
//Fourth row
c.gridy++;
c.gridwidth = 1;
c.gridx = 0;
lbladdress = new JLabel("Address :");
pnladd.add(lbladdress ,c);
c.gridx++;
c.gridwidth = 2;
txtaddress = new JTextField("",10);
pnladd.add(txtaddress,c);
//Fifth row
c.gridy++;
c.gridwidth = 3;
c.gridx = 0;
lblstatus = new JLabel(" ");
lblstatus.setForeground(Color.RED);
pnladd.add(lblstatus ,c);
//Last row
c.gridy++;
btnadd = new JButton("Add record");
btnadd.addActionListener(this);
pnladd.add(btnadd , c);
pnladdrecord.add(pnladd,BorderLayout.WEST);
tblrecord = new MapTable();
tblrecord.tableData(pnladdrecord);
}
public void searchPanel(){
pnlsearch.setLayout(new BorderLayout());
JPanel pnl = new JPanel();
pnl.setLayout(new GridLayout(1,3));
lblname = new JLabel("Name :");
pnl.add(lblname);
txtnamesearch = new JTextField("",10);
pnl.add(txtnamesearch);
btnsearch = new JButton("Search");
btnsearch.addActionListener(this);
pnl.add(btnsearch);
pnlsearch.add(pnl,BorderLayout.NORTH);
tblsearchrecord = new MapTable();
tblsearchrecord.tableData(pnlsearch);
JPanel pnlDelete = new JPanel();
pnlDelete.setLayout(new BorderLayout());
btndelete = new JButton("Delete");
btndelete.addActionListener(this);
btnSave = new JButton("Save");
btnSave.addActionListener(this);
pnlDelete.add(btnSave,BorderLayout.WEST);
pnlDelete.add(btndelete,BorderLayout.EAST);
pnlsearch.add(pnlDelete,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent a){
String command = a.getActionCommand();
switch(command){
case "Add record":
MapDetails temp = new MapDetails();
boolean nameverify = true;
Collection<String> keys = detail.keySet();
Object[] key = keys.toArray();
for(int i = 0; i < key.length ; i++){
String temp1 = key[i].toString();
if((temp1.equalsIgnoreCase(txtname.getText().trim()))){
nameverify = false;
}
}
boolean namecheck = !(txtname.getText().trim().equals("")) && nameverify ;
boolean emailcheck = (!(txtemail.getText().indexOf('@') == -1) && !(txtemail.getText().indexOf('.') == -1)) || (txtemail.getText().equals(""));
boolean contactcheck = !(txtcontact.getText().trim().equals(""));
boolean addresscheck = !(txtaddress.getText().trim().equals(""));
if( namecheck && contactcheck && emailcheck && addresscheck ){
//Adding MapDetails
String tempName = txtname.getText().trim();
String tempContact = txtcontact.getText().trim();
String tempEmail = txtemail.getText().trim();
String tempAddress = txtaddress.getText().trim();
temp = new MapDetails(tempContact , tempEmail , tempAddress);
detail.put(tempName,temp);
//Adding the record to the table
tblrecord.addRecordToTable(tempName,temp);
lblstatus.setText(" Successful");
}
else{
// lblverification.setText(" Invalid Name");
String error;
error = "Unsuccessful!! ";
lblstatus.setText(error);
if(!namecheck){
lblstatus.setText(error + " Invalid Name");
}
else if(!contactcheck)
lblstatus.setText(error + " Enter Contact ");
else if(!addresscheck)
lblstatus.setText(error + " Enter Address ");
else if(!emailcheck)
lblstatus.setText(error + " Invalid email ");
}
break;
case "Search":
search();
break;
case "Delete":
int confirm = JOptionPane.showConfirmDialog(new JFrame(),"Are you sure you want to delete these records?",
"Confim Message",JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
int[] row = tblsearchrecord.getTable().getSelectedRows();
Object[] name = new String[row.length];
for(int i = 0 ; i < row.length ; i++){
int index = row[i];
name[i] = tblsearchrecord.getDm().getValueAt(index,1);
}
for(int i = 0 ; i < name.length ; i++){
MapDetails temp1;
Object name1 = name[i];
if(detail.containsKey(name1)){
for(int j = 0 ; j < tblrecord.getTable().getRowCount() ; j++){
if(tblrecord.getDm().getValueAt(j,1).toString().equals(name1))
tblrecord.getDm().removeRow(j);
}
detail.remove(name1);
search();
}
}
tblrecord.getDm().setRowCount(0);
for(Map.Entry<String,MapDetails> entry : entries){
tblrecord.addRecordToTable(entry.getKey(),entry.getValue());
}
}
break;
case "Save":
for(int i = 0 ; i < tblsearchrecord.getDm().getRowCount(); i++){
String name = tblsearchrecord.getDm().getValueAt(i,1).toString();
if(detail.containsKey(name)){
MapDetails temp1 = new MapDetails();
detail.remove(name);
temp1.putContact(tblsearchrecord.getDm().getValueAt(i,2).toString());
temp1.putEmail(tblsearchrecord.getDm().getValueAt(i,3).toString());
temp1.putAddress(tblsearchrecord.getDm().getValueAt(i,4).toString());
detail.put(name,temp1);
}
}
tblrecord.getDm().setRowCount(0);
for(Map.Entry<String,MapDetails> entry : entries){
tblrecord.addRecordToTable(entry.getKey(),entry.getValue());
}
search();
break;
}
}
public void search(){
tblsearchrecord.getDm().setRowCount(0);
String namesearch = txtnamesearch.getText();
MapDetails temp;
Collection<String> keys = detail.keySet();
Object[] key = keys.toArray();
for(int i = 0; i < key.length ; i++){
String temp1 = key[i].toString();
if(temp1.toLowerCase().contains(namesearch.toLowerCase())){
temp = detail.get(temp1);
int row = tblsearchrecord.getDm().getRowCount();
String[] rowdata = {""+ (row+1) , temp1, temp.getContact() , temp.getEmail() , temp.getAddress() };
tblsearchrecord.getDm().addRow(rowdata);
}
}
}
public static void main (String[] args) {
new MapPhonebook("Phonebook");
}
}
No comments:
Post a Comment