Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bbe5ee6

Browse files
author
Ashish
committed
Files updated
0 parents commit bbe5ee6

File tree

11 files changed

+345
-0
lines changed

11 files changed

+345
-0
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
out/

‎JavaSwingMVC.iml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

‎src/App.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import View.MainFrame;
2+
3+
import javax.swing.*;
4+
5+
public class App {
6+
public static void main(String[] args) {
7+
// runs in AWT thread
8+
SwingUtilities.invokeLater(MainFrame::new);
9+
}
10+
}

‎src/Controller/UserController.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package Controller;
2+
3+
import Model.Database;
4+
import Model.User;
5+
import View.Form;
6+
import View.UserDetails;
7+
8+
import javax.swing.*;
9+
import java.io.File;
10+
11+
public class UserController {
12+
// database file
13+
private String databaseFile = "src\\data\\database.txt";
14+
private Database database;
15+
private Form form;
16+
private UserDetails userDetails;
17+
18+
public UserController(Form form, UserDetails userDetails) {
19+
this.database = new Database();
20+
this.form = form;
21+
this.userDetails = userDetails;
22+
23+
// submit user
24+
this.form.submitUsers(e -> {
25+
String firstname = this.form.getFirstname().trim();
26+
String lastname = this.form.getLastname().trim();
27+
28+
// simple validations
29+
if(firstname.isEmpty()) {
30+
JOptionPane.showMessageDialog(this.form, "First Name Required.", "Error",
31+
JOptionPane.ERROR_MESSAGE);
32+
return;
33+
} else if(lastname.isEmpty()) {
34+
JOptionPane.showMessageDialog(this.form, "Last Name Required.", "Error",
35+
JOptionPane.ERROR_MESSAGE);
36+
return;
37+
}
38+
this.database.addUser(new User(firstname, lastname));
39+
this.database.saveUser(new File(databaseFile));
40+
this.form.reset(true);
41+
});
42+
43+
// load users
44+
this.form.viewUsers(e -> {
45+
this.userDetails.getUsers(this.database.loadUsers(new File(databaseFile)));
46+
});
47+
}
48+
}

‎src/Model/Database.java‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package Model;
2+
3+
import java.io.*;
4+
import java.util.ArrayList;
5+
6+
public class Database {
7+
8+
private ArrayList<User> userArrayList;
9+
10+
public Database() {
11+
userArrayList = new ArrayList<>();
12+
}
13+
14+
// adds user to our collection
15+
public void addUser(User user) {
16+
userArrayList.add(user);
17+
}
18+
19+
// saves user to database file
20+
public void saveUser(File file) {
21+
try {
22+
// user model
23+
User user;
24+
String save_data = "";
25+
26+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
27+
int i = 0;
28+
while( i < userArrayList.size()) {
29+
user = userArrayList.get(i);
30+
save_data = user.getFirstname() + ", " + user.getLastname();
31+
i++;
32+
}
33+
bufferedWriter.write(save_data);
34+
bufferedWriter.newLine();
35+
// prevents memory leak
36+
bufferedWriter.close();
37+
} catch (IOException e) {
38+
e.printStackTrace();
39+
}
40+
}
41+
42+
// reads user from database file
43+
public Object[] loadUsers(File file) {
44+
Object[] objects;
45+
try {
46+
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
47+
// each lines to array
48+
objects = bufferedReader.lines().toArray();
49+
bufferedReader.close();
50+
return objects;
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
return null;
55+
}
56+
57+
58+
}

‎src/Model/User.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Model;
2+
3+
public class User {
4+
private String firstname;
5+
private String lastname;
6+
7+
public User() {
8+
// empty constructor
9+
}
10+
11+
public User(String firstname, String lastname) {
12+
this.firstname = firstname;
13+
this.lastname = lastname;
14+
}
15+
16+
// getters
17+
public String getFirstname() {
18+
return firstname;
19+
}
20+
21+
public String getLastname() {
22+
return lastname;
23+
}
24+
}

‎src/View/Form.java‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package View;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.awt.event.ActionListener;
6+
7+
public class Form extends JPanel {
8+
9+
private JTextField firstnameField;
10+
private JTextField lastNameField;
11+
12+
private JButton addButton;
13+
private JButton viewButton;
14+
15+
public Form() {
16+
17+
JLabel firstnameLabel = new JLabel("First Name: ");
18+
JLabel lastnameLabel = new JLabel("Last Name: ");
19+
20+
firstnameField = new JTextField(25);
21+
lastNameField = new JTextField(25);
22+
23+
addButton = new JButton("Add User");
24+
addButton.setPreferredSize(new Dimension(278, 40));
25+
viewButton = new JButton("View All Users");
26+
viewButton.setPreferredSize(new Dimension(278, 40));
27+
28+
// space between fields
29+
Insets fieldsInset = new Insets(0, 0, 10, 0);
30+
// space between buttons
31+
Insets buttonInset = new Insets(20,0,0,0);
32+
33+
// uses Grid Bag Layout
34+
setLayout(new GridBagLayout());
35+
GridBagConstraints gridBagConstraints = new GridBagConstraints();
36+
gridBagConstraints.insets = fieldsInset;
37+
gridBagConstraints.fill = GridBagConstraints.NONE;
38+
39+
gridBagConstraints.gridx = 0;
40+
gridBagConstraints.gridy = 0;
41+
gridBagConstraints.anchor = GridBagConstraints.WEST;
42+
43+
add(firstnameLabel, gridBagConstraints);
44+
45+
gridBagConstraints.gridx = 0;
46+
gridBagConstraints.gridy = 1;
47+
48+
add(firstnameField, gridBagConstraints);
49+
50+
gridBagConstraints.gridx = 0;
51+
gridBagConstraints.gridy = 2;
52+
gridBagConstraints.anchor = GridBagConstraints.WEST;
53+
54+
add(lastnameLabel, gridBagConstraints);
55+
56+
gridBagConstraints.gridx = 0;
57+
gridBagConstraints.gridy = 3;
58+
59+
add(lastNameField, gridBagConstraints);
60+
61+
gridBagConstraints.gridx = 0;
62+
gridBagConstraints.gridy = 4;
63+
gridBagConstraints.insets = buttonInset;
64+
65+
add(addButton, gridBagConstraints);
66+
67+
gridBagConstraints.gridx = 0;
68+
gridBagConstraints.gridy = 5;
69+
gridBagConstraints.insets = buttonInset;
70+
71+
add(viewButton, gridBagConstraints);
72+
}
73+
74+
// getters
75+
public String getFirstname() {
76+
return firstnameField.getText();
77+
}
78+
79+
public String getLastname() {
80+
return lastNameField.getText();
81+
}
82+
83+
public void submitUsers(ActionListener actionListener) {
84+
addButton.addActionListener(actionListener);
85+
}
86+
87+
public void viewUsers(ActionListener actionListener) {
88+
viewButton.addActionListener(actionListener);
89+
}
90+
91+
// reset fields
92+
public void reset(boolean bln) {
93+
if(bln) {
94+
firstnameField.setText("");
95+
lastNameField.setText("");
96+
}
97+
}
98+
}

‎src/View/MainFrame.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package View;
2+
3+
import Controller.UserController;
4+
5+
import javax.swing.*;
6+
import java.awt.*;
7+
8+
public class MainFrame extends JFrame {
9+
10+
// Card layout for switching view
11+
private CardLayout cardLayout;
12+
13+
public MainFrame() {
14+
super("Java Swing MVC");
15+
cardLayout = new CardLayout();
16+
Form form = new Form();
17+
UserDetails userDetails = new UserDetails();
18+
// sets our layout as a card layout
19+
setLayout(cardLayout);
20+
21+
// initialize user controller
22+
new UserController(form, userDetails);
23+
24+
// adds view to card layout with unique constraints
25+
add(form, "form");
26+
add(userDetails, "user details");
27+
// switch view according to its constraints on click
28+
form.viewUsers(e -> cardLayout.show(MainFrame.this.getContentPane(), "user details"));
29+
userDetails.backButton(e -> cardLayout.show(MainFrame.this.getContentPane(), "form"));
30+
31+
// icon for our application
32+
ImageIcon imageIcon = new ImageIcon("src/assets/appicon.png");
33+
setIconImage(imageIcon.getImage());
34+
// frame width & height
35+
int FRAME_WIDTH = 1200;
36+
int FRAME_HEIGHT = 700;
37+
// size of our application frame
38+
setSize(FRAME_WIDTH, FRAME_HEIGHT);
39+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40+
setVisible(true);
41+
}
42+
}

‎src/View/UserDetails.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package View;
2+
3+
import javax.swing.*;
4+
import javax.swing.table.DefaultTableModel;
5+
import java.awt.*;
6+
import java.awt.event.ActionListener;
7+
8+
public class UserDetails extends JPanel {
9+
10+
// Table for user data
11+
private JTable userTable;
12+
// table column
13+
private String[] userTableColumn = {"FIRST NAME", "LAST NAME"};
14+
15+
// back button
16+
private JButton backButton;
17+
18+
public UserDetails() {
19+
// uses box layout
20+
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
21+
// toolbar for buttons
22+
JToolBar toolBar = new JToolBar();
23+
userTable = new JTable();
24+
// scroll bar for table
25+
JScrollPane userTableScroll = new JScrollPane(userTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
26+
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
27+
backButton = new JButton("Go Back");
28+
add(toolBar);
29+
toolBar.add(backButton);
30+
toolBar.setMaximumSize(new Dimension(Integer.MAX_VALUE, toolBar.getMinimumSize().height));
31+
add(userTableScroll);
32+
33+
}
34+
35+
// gets users from database and loads to table
36+
public void getUsers(Object[] objects) {
37+
DefaultTableModel defaultTableModel = (DefaultTableModel) userTable.getModel();
38+
defaultTableModel.setColumnIdentifiers(userTableColumn);
39+
int i = 0;
40+
while(i < objects.length) {
41+
String row = objects[i].toString().trim();
42+
String[] rows = row.split(",");
43+
defaultTableModel.addRow(rows);
44+
i++;
45+
}
46+
}
47+
48+
// event listener for back button
49+
public void backButton(ActionListener actionListener) {
50+
backButton.addActionListener(actionListener);
51+
}
52+
}

‎src/assets/appicon.png‎

2.01 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /