This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewContacts.java
126 lines (102 loc) · 3.28 KB
/
ViewContacts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
class ArList{
// Components
String name;
String phone;
String email;
String address;
//. Components
// Constructor
public ArList(String name, String phone, String email, String address) {
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
}
//. Constructor
}
public class ViewContacts {
// Components
private JFrame f;
private JButton back;
private JPanel p1;
private JPanel p2;
private JScrollPane sp;
private JTable t;
//. Components
public ViewContacts(){
// Frame
f = new JFrame("View Contacts | Address Book");
f.setLayout(new FlowLayout());
f.setSize(550,600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//. Frame
// Back Button Functionality
back = new JButton("Back");
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AddressBook();
f.setVisible(false);
}
});
//. Back Button Functionality
// Array List Generate
ArrayList<ArList> data = new ArrayList<>();
// Table Column Names
String[] col = {"Name", "Phone", "Email", "Street Address"};
// Table Functionality
try{
FileReader fr = new FileReader("Contacts.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while( (line = br.readLine()) != null) {
String[] parts = line.split("\t");
String name = parts[0];
String phone = parts[1];
String email = parts[2];
String address = parts[3];
ArList details = new ArList(name, phone, email, address);
data.add(details);
}
String[][] info = new String[data.size()][col.length];
for(int i=0; i < data.size();i++){
ArList item = data.get(i);
info[i][0] = item.name;
info[i][1] = item.phone;
info[i][2] = item.email;
info[i][3] = item.address;
}
t = new JTable(info, col);
t.setAutoCreateColumnsFromModel(true);
t.setAutoCreateRowSorter(true);
t.setFillsViewportHeight(true);
br.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//. Table Functionality
// Adding Components to Frame
p1 = new JPanel();
p2 = new JPanel();
p1.add(back);
f.add(p1);
sp = new JScrollPane(t);
p2.add(sp);
f.add(p2);
f.setVisible(true);
//. Adding Components to Frame
}
public static void main(String[] args) {
new ViewContacts();
}
}