-
Notifications
You must be signed in to change notification settings - Fork 1
/
AllMessages.java
269 lines (235 loc) · 10.8 KB
/
AllMessages.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.util.Set;
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* AllMessages.java
*
* Created on 12 Nov, 2019, 1:52:52 PM
*/
/**
*
* @author India
*/
public class AllMessages extends javax.swing.JFrame {
String UserName=LogIn.jTextField1.getText();
ResultSet r;
public AllMessages() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton6 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Arial", 3, 14));
jLabel1.setText("Sender");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 80, -1, -1));
jLabel2.setFont(new java.awt.Font("Arial", 3, 14));
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 80, 220, 30));
jLabel3.setFont(new java.awt.Font("Arial", 3, 14));
jLabel3.setText("Sent time");
getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 150, -1, -1));
getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 140, 190, 30));
jButton6.setText("Show all Messages");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
getContentPane().add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 80, -1, -1));
jButton1.setText("l<");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 390, 100, -1));
jButton2.setText("<");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 390, 90, -1));
jButton3.setText(">");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(321, 390, 80, -1));
jButton4.setText(">l");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(423, 390, 90, -1));
jButton5.setText("Read all messages");
jButton5.setEnabled(false);
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(191, 440, 200, -1));
jButton7.setText("Go back ");
jButton7.setEnabled(false);
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
getContentPane().add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 490, -1, -1));
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 210, 440, 150));
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG-20191111-WA0071.jpg"))); // NOI18N
getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 610, -1));
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
jButton6.setEnabled(false);
int NumberOfMessages=0;
try {
//For establishing connection between program and database
Class.forName("java.sql.DriverManager");
Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/LabChat","root","zeaan");
Statement stmt=(Statement)con.createStatement();
//For getting the records
String q="Select * from ChattingSystem where Reciever='"+UserName+"';";
r=stmt.executeQuery(q);
//For checking whether User has recieved any messages
while(r.next()) {
NumberOfMessages++;
}
if(NumberOfMessages!=0) {
//For displaying Messages
r.first();
jTextArea1.setText(r.getString("Message"));
jButton5.setEnabled(true);
jLabel2.setText(r.getString("Sender"));
jLabel4.setText(r.getString("SentTime"));
} else {
JOptionPane.showMessageDialog(null,"You have no messages");
jButton7.setEnabled(true);
}
} catch(Exception o) {
JOptionPane.showMessageDialog(null,o.getMessage());
}
}//GEN-LAST:event_jButton6ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try {
r.first();
jTextArea1.setText(r.getString("Message"));
jLabel2.setText(r.getString("Sender"));
jLabel4.setText(r.getString("SentTime"));
} catch(Exception o) {
JOptionPane.showMessageDialog(null,"No more messages");
}
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
try {
r.previous();
jTextArea1.setText(r.getString("Message"));
jLabel2.setText(r.getString("Sender"));
jLabel4.setText(r.getString("SentTime"));
} catch(Exception o) {
JOptionPane.showMessageDialog(null,"No more messages");
}
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
try {
r.next();
jTextArea1.setText(r.getString("Message"));
jLabel2.setText(r.getString("Sender"));
jLabel4.setText(r.getString("SentTime"));
} catch(Exception o) {
JOptionPane.showMessageDialog(null,"No more messages");
}
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
try {
r.last();
jTextArea1.setText(r.getString("Message"));
jLabel2.setText(r.getString("Sender"));
jLabel4.setText(r.getString("SentTime"));
} catch(Exception o) {
JOptionPane.showMessageDialog(null,"No more messages");
}
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
try {
jButton7.setEnabled(true);
//For establishing connection between program and database
Class.forName("java.sql.DriverManager");
Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/LabChat","root","sgmsgm");
Statement stmt=(Statement)con.createStatement();
//This ensures that all Unread messages are Read by the User
String q="Update ChattingSystem set status='Read' where Reciever='"+UserName+"' and status is null;";
stmt.executeUpdate(q);
} catch(Exception o) {
JOptionPane.showMessageDialog(null,o.getMessage());
}
}//GEN-LAST:event_jButton5ActionPerformed
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
//For moving to ShowMessage window
Messages m=new Messages();
m.setVisible(true);
this.dispose();
}//GEN-LAST:event_jButton7ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AllMessages().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables
}