@@ -78,6 +78,7 @@ private void initComponents() {
7878 send_msg = new javax .swing .JTextField ();
7979 send = new javax .swing .JButton ();
8080 subscribe = new javax .swing .JButton ();
81+ jButton1 = new javax .swing .JButton ();
8182
8283 setDefaultCloseOperation (javax .swing .WindowConstants .EXIT_ON_CLOSE );
8384 setTitle ("MQTT Viewer" );
@@ -146,6 +147,13 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
146147 }
147148 });
148149
150+ jButton1 .setText ("Unsubscribe" );
151+ jButton1 .addActionListener (new java .awt .event .ActionListener () {
152+ public void actionPerformed (java .awt .event .ActionEvent evt ) {
153+ jButton1ActionPerformed (evt );
154+ }
155+ });
156+
149157 javax .swing .GroupLayout layout = new javax .swing .GroupLayout (getContentPane ());
150158 getContentPane ().setLayout (layout );
151159 layout .setHorizontalGroup (
@@ -159,6 +167,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
159167 .addPreferredGap (javax .swing .LayoutStyle .ComponentPlacement .RELATED )
160168 .addComponent (topic )
161169 .addPreferredGap (javax .swing .LayoutStyle .ComponentPlacement .RELATED )
170+ .addComponent (jButton1 )
171+ .addPreferredGap (javax .swing .LayoutStyle .ComponentPlacement .RELATED )
162172 .addComponent (subscribe ))
163173 .addGroup (layout .createSequentialGroup ()
164174 .addComponent (jLabel1 )
@@ -212,7 +222,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
212222 .addGroup (layout .createParallelGroup (javax .swing .GroupLayout .Alignment .BASELINE )
213223 .addComponent (jLabel2 )
214224 .addComponent (topic , javax .swing .GroupLayout .PREFERRED_SIZE , javax .swing .GroupLayout .DEFAULT_SIZE , javax .swing .GroupLayout .PREFERRED_SIZE ))
215- .addComponent (subscribe , javax .swing .GroupLayout .Alignment .TRAILING ))
225+ .addGroup (javax .swing .GroupLayout .Alignment .TRAILING , layout .createParallelGroup (javax .swing .GroupLayout .Alignment .BASELINE )
226+ .addComponent (subscribe )
227+ .addComponent (jButton1 )))
216228 .addPreferredGap (javax .swing .LayoutStyle .ComponentPlacement .RELATED )
217229 .addComponent (jScrollPane1 , javax .swing .GroupLayout .DEFAULT_SIZE , 603 , Short .MAX_VALUE )
218230 .addPreferredGap (javax .swing .LayoutStyle .ComponentPlacement .RELATED )
@@ -244,6 +256,10 @@ private void subscribeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
244256 subscribe ();
245257 }//GEN-LAST:event_subscribeActionPerformed
246258
259+ private void jButton1ActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_jButton1ActionPerformed
260+ unsubscribe ();
261+ }//GEN-LAST:event_jButton1ActionPerformed
262+
247263 /**
248264 * @param args the command line arguments
249265 */
@@ -260,6 +276,7 @@ public void run() {
260276 // Variables declaration - do not modify//GEN-BEGIN:variables
261277 private javax .swing .JCheckBox authenticate ;
262278 private javax .swing .JButton clear ;
279+ private javax .swing .JButton jButton1 ;
263280 private javax .swing .JLabel jLabel1 ;
264281 private javax .swing .JLabel jLabel2 ;
265282 private javax .swing .JLabel jLabel3 ;
@@ -305,6 +322,10 @@ public void onSubscribe(String topic) {
305322 addText ("subscribe accepted!\r \n " );
306323 }
307324
325+ public void onUnsubscribe (String topic ) {
326+ addText ("unsubscribe accepted!\r \n " );
327+ }
328+
308329 public void onMessage (String topic , String msg ) {
309330 String ln = topic + "=" + msg + "\r \n " ;
310331 addText (ln );
@@ -378,6 +399,13 @@ public void subscribe() {
378399 addText ("Subscribed to : " + _topic + "\r \n " );
379400 }
380401
402+ public void unsubscribe () {
403+ if (client == null ) return ;
404+ String _topic = topic .getText ().trim ();
405+ client .unsubscribe (_topic );
406+ addText ("Unsubscribed to : " + _topic + "\r \n " );
407+ }
408+
381409 private void setState (boolean state ) {
382410 server .setEditable (state );
383411 authenticate .setEnabled (state );
0 commit comments