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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c82739
commit 4e52eb0
Showing
3 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class AddNew { | ||
// Components | ||
private JFrame f; | ||
private JPanel p1; | ||
private JPanel p2; | ||
private JPanel p3; | ||
private JPanel p4; | ||
private JPanel p5; | ||
|
||
private JLabel l1; | ||
private JLabel l2; | ||
private JLabel l3; | ||
private JLabel l4; | ||
|
||
private JTextField t1; | ||
private JTextField t2; | ||
private JTextField t3; | ||
private JTextField t4; | ||
|
||
private JButton back; | ||
private JButton submit; | ||
private JButton clear; | ||
//. Components | ||
|
||
public AddNew(){ | ||
f = new JFrame("Add New | Address Book"); | ||
f.setLayout(new FlowLayout(FlowLayout.RIGHT)); | ||
f.setSize(350, 500); | ||
f.setLocationRelativeTo(null); | ||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
// Panels | ||
p1 = new JPanel(); | ||
p2 = new JPanel(); | ||
p3 = new JPanel(); | ||
p4 = new JPanel(); | ||
p5 = new JPanel(); | ||
//. Panels | ||
|
||
// Labels | ||
l1 = new JLabel("Name:"); | ||
l2 = new JLabel("Phone:"); | ||
l3 = new JLabel("Email:"); | ||
l4 = new JLabel("Street Address:"); | ||
//. Labels | ||
|
||
// Text Fields | ||
t1 = new JTextField(20); | ||
t2 = new JTextField(20); | ||
t3 = new JTextField(20); | ||
t4 = new JTextField(20); | ||
//. Text Fields | ||
|
||
// Buttons | ||
back = new JButton("Back"); | ||
clear = new JButton("Clear"); | ||
submit = new JButton("Submit"); | ||
//. Buttons | ||
|
||
// Back Button Action | ||
back.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
new AddressBook(); | ||
f.setVisible(false); | ||
} | ||
}); | ||
//. Back Button Action | ||
|
||
// Clear Button Action | ||
clear.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
t1.setText(""); | ||
t2.setText(""); | ||
t3.setText(""); | ||
t4.setText(""); | ||
} | ||
}); | ||
//. Clear Button Action | ||
|
||
// Submit Button Action | ||
submit.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
try { | ||
FileWriter fw = new FileWriter("Contacts.txt", true); | ||
BufferedWriter bw = new BufferedWriter(fw); | ||
|
||
String name = t1.getText(); | ||
String phone = t2.getText(); | ||
String email = t3.getText(); | ||
String address = t4.getText(); | ||
|
||
bw.write(name+"\t"+phone+"\t"+email+"\t"+address+"\n"); | ||
bw.close(); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
finally { | ||
new AddressBook(); | ||
f.setVisible(false); | ||
} | ||
} | ||
}); | ||
//. Submit Button Action | ||
|
||
// Adding Components to Frame | ||
p1.add(l1); | ||
p1.add(t1); | ||
p2.add(l2); | ||
p2.add(t2); | ||
p3.add(l3); | ||
p3.add(t3); | ||
p4.add(l4); | ||
p4.add(t4); | ||
p5.add(back); | ||
p5.add(clear); | ||
p5.add(submit); | ||
|
||
f.add(p1); | ||
f.add(p2); | ||
f.add(p3); | ||
f.add(p4); | ||
f.add(p5); | ||
//. Adding Components to Frame | ||
|
||
f.setVisible(true); | ||
} | ||
|
||
public static void main(String[] args) { | ||
new AddNew(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
|
||
public class AddressBook { | ||
// Components | ||
private JFrame f; | ||
private JPanel p = new JPanel(); | ||
private JButton view; | ||
private JButton create; | ||
//. Components | ||
|
||
public AddressBook(){ | ||
// Frame Functionalities | ||
f = new JFrame("Address Book"); // Frame create & Name | ||
f.setLayout(new FlowLayout()); // Frame Layout | ||
f.setSize(300,100); // Frame Size | ||
f.setLocationRelativeTo(null); // Frame Location ==> set to middle of the screen | ||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Frame close operation | ||
//. Frame functionalities | ||
|
||
// panels | ||
//p = new JPanel(); | ||
|
||
// Buttons | ||
create = new JButton("Add New"); | ||
view = new JButton("View Contacts"); | ||
//. Buttons | ||
|
||
// Create Button Actions | ||
create.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
new AddNew(); | ||
f.setVisible(false); | ||
} | ||
}); | ||
//. Create Button Actions | ||
|
||
// View Button Actions | ||
view.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
new ViewContacts(); | ||
f.setVisible(false); | ||
} | ||
}); | ||
//. View Button Actions | ||
|
||
// Adding Buttons to panel | ||
p.add(create); | ||
p.add(view); | ||
//. Adding Buttons to panel | ||
|
||
// Adding panel to frame & Frame visibility | ||
f.add(p); | ||
f.setVisible(true); | ||
//. Adding panel to frame & Frame visibility | ||
} | ||
public static void main(String[] args) { | ||
new AddressBook(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
|
||
} |